Table of Contents
Introduction
Canary deployments are a powerful strategy for rolling out new application versions with minimal risk. By gradually shifting traffic to the new version, you can test and monitor its performance before fully committing.
Prerequisites
- Access to a command line/terminal
- Docker installed on the system
- Kubernetes or Minikube
- A fully configured
kubectl
command-line tool on your local machine
What is a Canary Deployment?
A canary deployment is a method for releasing new software versions to a small subset of users before making it available to the broader audience. Named after the practice of using canaries in coal mines to detect toxic gases, this strategy helps identify potential issues with the new version without affecting all users. By directing a small portion of traffic to the new version, developers can monitor its performance and gather feedback, allowing for a safe and controlled rollout.
Step-by-Step Guide to Canary Deployments
Step 1: Pull the Docker Image
Retrieve your base Docker image using:
docker pull <image-name>
Step 2: Create Deployment
Define your Kubernetes deployment in a YAML file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: <image-name>
ports:
- containerPort: 80
Apply the deployment with:
kubectl apply -f deployment.yaml
Step 3: Create Service
Set up a service to route traffic to your pods:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Apply the service with:
kubectl apply -f service.yaml
Step 4: Deploy Initial Version
Ensure your initial deployment is functioning correctly. You can verify the status of your pods and services using:
kubectl get pods
kubectl get services
Step 5: Create Canary Deployment
Create a new deployment for the canary version:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-canary
spec:
replicas: 1
selector:
matchLabels:
app: my-app-canary
template:
metadata:
labels:
app: my-app-canary
spec:
containers:
- name: my-app
image: <new-image-name>
ports:
- containerPort: 80
Apply the canary deployment with:
kubectl apply -f canary-deployment.yaml
Step 6: Update Service
Modify your service to route some traffic to the Canary version. This can be done using Istio or any other service mesh tool. For example, using Istio:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-app
spec:
hosts:
- "*"
gateways:
- my-app-gateway
http:
- route:
- destination:
host: my-app
subset: v1
weight: 90
- destination:
host: my-app-canary
subset: v2
weight: 10
Step 7: Monitor and Adjust
Monitor the performance and behavior of the canary deployment. Use tools like Prometheus, Grafana, or Kubernetes’ built-in monitoring. Adjust the traffic split as necessary until you are confident in the new version’s stability.
Conclusion
Implementing a canary deployment strategy on K8s allows for safer, incremental updates to your applications. By carefully monitoring the new version and adjusting traffic as needed, you can ensure a smooth transition with minimal risk. This approach helps maintain application stability while delivering new features to users. I hope will this your helpful. Thank you for reading the DevopsRoles page!