Blue-Green Deployment in Kubernetes
Implement blue-green deployments in Kubernetes for instant rollback. Covers Service selector switching, Argo Rollouts blue-green, and comparison with canary
π‘ Quick Answer: deployments
The Problem
This is a fundamental Kubernetes topic that engineers search for frequently. A comprehensive reference with production-ready examples saves hours of trial and error.
The Solution
Native Blue-Green with Service Selector
# Blue deployment (current production)
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-blue
spec:
replicas: 3
selector:
matchLabels:
app: web
version: blue
template:
metadata:
labels:
app: web
version: blue
spec:
containers:
- name: web
image: my-app:v1
---
# Green deployment (new version)
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-green
spec:
replicas: 3
selector:
matchLabels:
app: web
version: green
template:
metadata:
labels:
app: web
version: green
spec:
containers:
- name: web
image: my-app:v2
---
# Switch traffic by updating selector
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
version: blue # β Change to "green" to switch
ports:
- port: 80# Deploy green alongside blue
kubectl apply -f web-green.yaml
# Test green (port-forward to green directly)
kubectl port-forward deployment/web-green 8080:80
# Switch traffic: blue β green
kubectl patch svc web -p '{"spec":{"selector":{"version":"green"}}}'
# Instant rollback: green β blue
kubectl patch svc web -p '{"spec":{"selector":{"version":"blue"}}}'
# Clean up old version after confirming
kubectl delete deployment web-blueBlue-Green vs Canary vs Rolling
| Strategy | Rollback Speed | Resource Cost | Risk |
|---|---|---|---|
| Blue-Green | Instant (switch selector) | 2x (both versions running) | Low |
| Canary | Fast (scale down canary) | ~10% extra | Very low |
| Rolling Update | Slow (rollout undo) | ~25% extra | Medium |
graph TD
A[Service: web] -->|selector: version=blue| B[Blue: v1 - 3 pods]
C[Green: v2 - 3 pods] -->|Ready and tested| D[Switch selector]
D --> E[Service: web β version=green]
E --> C
F[Problem?] --> G[Switch back to blue - instant]Frequently Asked Questions
Blue-green vs rolling update?
Blue-green: run both versions fully, switch traffic instantly, instant rollback. Costs 2x resources during deployment. Rolling update: gradually replace pods, lower resource cost, slower rollback.
Best Practices
- Start with the simplest configuration that meets your needs
- Test changes in staging before production
- Use
kubectl describeand events for troubleshooting - Document your decisions for the team
Key Takeaways
- This is essential Kubernetes knowledge for production operations
- Follow the principle of least privilege and minimal configuration
- Monitor and iterate based on real-world behavior
- Automation reduces human error and improves consistency

Recommended
Kubernetes Recipes β The Complete Book100+ production-ready patterns with detailed explanations, best practices, and copy-paste YAML. Everything in one place.
Get the Book βLearn by Doing
CopyPasteLearn β Hands-on Cloud & DevOps CoursesMaster Kubernetes, Ansible, Terraform, and MLOps with interactive, copy-paste-run lessons. Start free.
Browse Courses βπ Deepen Your Skills β Hands-on Courses
Courses by CopyPasteLearn.com β Learn IT by Doing
