Use Kubernetes primitives to implement common deployment strategies (e.g. blue/green or canary)

Context: CKAD certification command exampoles

Use Kubernetes primitives to implement common deployment strategies (e.g. blue/green or canary)

Implementing common deployment strategies like blue/green or canary deployments is an important skill for the Certified Kubernetes Application Developer (CKAD) certification. Below are command examples that demonstrate how to use Kubernetes primitives to implement these deployment strategies:

  1. Blue/Green Deployment:
  • Create the Blue Deployment:
kubectl create deployment myapp-blue --image=myapp:blue
  • Expose the Blue Deployment with a Service:
kubectl expose deployment myapp-blue --port=80 --target-port=8080 --type=ClusterIP
  • Verify that the Blue Deployment is running successfully:
kubectl get deployments kubectl get pods kubectl get services
  • Update the Deployment to the Green version:
kubectl set image deployment/myapp-blue myapp=myapp:green
  • Verify the rollout status:
kubectl rollout status deployment/myapp-blue
  • Optionally, update the Service to point to the Green Deployment:
kubectl patch service myapp-blue -p '{"spec":{"selector":{"app":"myapp-green"}}}'
  • Verify that the Green Deployment is running successfully:
kubectl get deployments kubectl get pods kubectl get services
  • Perform additional testing and validation on the Green Deployment.

  • If the Green Deployment is successful, you can switch traffic to the Green Deployment by updating the Service or using an Ingress controller.

  1. Canary Deployment:
  • Create the Primary Deployment:
kubectl create deployment myapp-primary --image=myapp:primary
  • Expose the Primary Deployment with a Service:
kubectl expose deployment myapp-primary --port=80 --target-port=8080 --type=ClusterIP
  • Verify that the Primary Deployment is running successfully: 
kubectl get deployments
kubectl get pods
kubectl get services
  • Create the Canary Deployment:
kubectl create deployment myapp-canary --image=myapp:canary
  • Expose the Canary Deployment with a Service:
kubectl expose deployment myapp-canary --port=80 --target-port=8080 --type=ClusterIP
  • Verify that the Canary Deployment is running successfully:
kubectl get deployments
kubectl get pods
kubectl get services
  • Route a portion of the traffic to the Canary Deployment using Kubernetes Ingress or Service mesh configurations.

  • Monitor and analyze the behavior of the Canary Deployment.

  • If the Canary Deployment performs well, gradually increase the traffic to the Canary Deployment.

  • Optionally, roll back or promote the Canary Deployment to the Primary Deployment based on the test results.

These examples provide a starting point for implementing blue/green and canary deployments in Kubernetes. Remember to adapt the commands and configurations to match your application's specific requirements and environment.

You should also read: