Blue/Green Deployment in Kubernetes


Hereโs an enhanced and corrected version of your Blue/Green deployment guide with clearer commands and structure:
Blue/Green Deployment in Kubernetes ๐
What is Blue/Green Deployment? ๐ต๐ข
Blue/Green deployment minimizes downtime and risk by maintaining two identical environments:
Blue: Current production environment (live)
Green: New version (staging). Once validated, traffic switches from Blue to Green.
Advantages โ
Zero downtime during deployment
Instant rollback to Blue if issues arise
Safe testing in production-like environment
Prerequisites โ๏ธ
Kubernetes cluster (
kubectl
configured)Basic understanding of Deployments and Services
Step 1: Deploy Blue Environment (v1)
1.1 Blue Deployment (v1)
blue-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
version: v1
template:
metadata:
labels:
app: my-app
version: v1
spec:
containers:
- name: nginx
image: linuxacademycontent/ckad-nginx:1.0.0
ports:
- containerPort: 80
Apply configuration:
kubectl apply -f blue-deployment.yaml
1.2 Blue Service
blue-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: ClusterIP
selector:
app: my-app
version: v1 # Points to Blue initially
ports:
- protocol: TCP
port: 80
targetPort: 80
Create service:
kubectl apply -f blue-service.yaml
Verify:
kubectl get pods -l version=v1
kubectl get svc my-app-service
Step 2: Deploy Green Environment (v2)
2.1 Green Deployment (v2)
green-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: green-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
version: v2
template:
metadata:
labels:
app: my-app
version: v2
spec:
containers:
- name: nginx
image: linuxacademycontent/ckad-nginx:canary
ports:
- containerPort: 80
Deploy Green:
kubectl apply -f green-deployment.yaml
Step 3: Validate Green Environment ๐งช
Check pods:
kubectl get pods -l version=v2
Test internally:
# Get test pod
kubectl run curl-test --image=radial/busyboxplus:curl -i --tty --rm
# Test Green deployment directly (not through service)
curl green-deployment-svc:80
Step 4: Traffic Switch ๐
Update service selector:
kubectl patch svc my-app-service -p '{"spec":{"selector":{"version":"v2"}}}'
Alternative method (edit service):
kubectl edit svc my-app-service
# Change selector: version: v1 โ v2
Step 5: Verify Traffic Shift ๐
Check endpoints:
kubectl describe svc my-app-service | grep Endpoints
Test from worker node:
# Get service ClusterIP
SVC_IP=$(kubectl get svc my-app-service -o jsonpath='{.spec.clusterIP}')
# Access from pod
kubectl exec -it <blue-pod> -- curl -s $SVC_IP
Rollback Procedure โฎ๏ธ
Revert service selector:
kubectl patch svc my-app-service -p '{"spec":{"selector":{"version":"v1"}}}'
Delete Green environment (if needed):
kubectl delete deployment green-deployment
Key Commands Cheatsheet ๐
Command | Description |
kubectl get pods -l version=v1 | List Blue pods |
kubectl rollout status deployment/green-deployment | Check Green deployment status |
kubectl get endpoints my-app-service | Verify active endpoints |
kubectl logs <pod-name> | Check container logs |
Best Practices ๐ก
Use readiness probes for health checks
Implement auto-scaling for both environments
Use versioned tags in container images
Monitor metrics during switchover
This version provides:
Consistent labeling (
version
instead ofenvironment
)Clear separation of Blue/Green environments
Proper service selector updates
Validation steps for each phase
Rollback instructions
Standardized kubectl commands without aliases
Improved YAML structure with version labels
Follow me on Linkedin
Subscribe to my newsletter
Read articles from Hamza Shaukat directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hamza Shaukat
Hamza Shaukat
Greetings, This is Hamza. Linux Administration hands on experience of configuring different server ,Bash Scipting , Docker , Ansible , Python Automation ,AWS Solution Architect and other Devops tools