Blue-Green Deployments on Kubernetes
Implement zero-downtime blue-green releases with Kubernetes Services, Ingress, and CI/CD automation.
Blue-green deployment keeps two identical environments and switches traffic atomically. On Kubernetes, you achieve this with two Deployments and a Service selector flip.
Architecture
Deployment Steps
Database migrations
Blue-green with schema changes requires backward-compatible migrations or expand-contract patterns. Both versions may run simultaneously during the switch.
Kubernetes Manifest Pattern
# green deployment (currently active)
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-green
spec:
replicas: 3
selector:
matchLabels:
app: api
version: green
template:
metadata:
labels:
app: api
version: green
---
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector:
app: api
version: green # flip to blue after validation
ports:
- port: 80CI/CD Integration
deploy:blue:
stage: deploy
script:
- kubectl apply -f k8s/blue/
- ./scripts/smoke-test.sh https://blue.internal.example.com
environment:
name: blue
switch:production:
stage: deploy
when: manual
script:
- kubectl patch service api -p '{"spec":{"selector":{"version":"blue"}}}'When to Use Blue-Green
| Scenario | Fit |
|---|---|
| Stateless APIs | Excellent |
| Instant rollback needed | Excellent |
| Stateful apps with sticky sessions | Requires care |
| Cost-sensitive | Higher (2x resources during switch) |
Conclusion
Blue-green trades resource cost for simplicity and instant rollback. Pair it with automated smoke tests in CI before flipping the Service selector.
Related Articles
GitLab CI to Kubernetes: End-to-End Deploy Pipeline
Design a complete CI/CD pipeline from GitLab merge request to Kubernetes production with security gates and GitOps handoff.
Trunk-Based Development with Modern CI/CD
Ship faster with short-lived branches, feature flags, and continuous integration on the main trunk.
Docker Multi-Stage Builds in CI/CD Pipelines
Shrink images, speed up pipelines, and improve security with multi-stage Dockerfiles in GitLab CI.