Sign In

Curriculum 25: Production Workflows

Blue-Green Deployments

15 min · 35 XP

Blue-Green Deployments with kubectl

Blue-green deployment maintains two identical environments. One (blue) serves production traffic while the other (green) hosts the new version. Traffic switches instantly by updating the service selector.

Setting Up Blue and Green

Deploy both versions with distinct labels to differentiate them:

# Blue deployment (current production)
kubectl create deployment myapp-blue \
  --image=myregistry/myapp:v1.0.0 --replicas=3

kubectl label deployment myapp-blue version=blue

# Green deployment (new version)
kubectl create deployment myapp-green \
  --image=myregistry/myapp:v2.0.0 --replicas=3

kubectl label deployment myapp-green version=green

Switching Traffic

The service selector determines which deployment receives traffic. Patch it to switch:

# Service currently pointing to blue
kubectl get svc myapp -o jsonpath='{.spec.selector}'

# Switch traffic to green
kubectl patch svc myapp -p '{"spec":{"selector":{"version":"green"}}}'

# Verify the switch
kubectl get endpoints myapp

Validating Before the Switch

Test the green deployment internally before exposing it to users:

# Port-forward to green pods for local testing
kubectl port-forward deployment/myapp-green 8080:80

# Run a smoke test pod inside the cluster
kubectl run smoke-test --rm -it --image=curlimages/curl \
  -- curl http://myapp-green-svc:80/health

Instant Rollback

If the new version has issues, switch the service back to blue:

# Rollback to blue
kubectl patch svc myapp -p '{"spec":{"selector":{"version":"blue"}}}'

# Confirm endpoints point to blue pods
kubectl get endpoints myapp

Once the green version is validated, delete the old blue deployment to free resources. Blue-green gives you zero-downtime deployments with instant rollback capability.