Version note: examples use the Kubernetes v1.35 documentation snapshot, not necessarily the newest release. Check current support and component compatibility. These commands were not tested on a live cluster.
What this guide covers
A blue-green deployment runs two separate versions. One Service sends live traffic to the selected version. You test the new version first, then change the Service selector.
Before you start
Create bluegreen-lab and reserve capacity for both versions. Use compatible application and database changes. This is a test setup, not a guarantee of instant traffic switching.
Step 1: Create both versions
Save this as bluegreen.yaml, choose the intended images for blue and green, then apply it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-blue
namespace: bluegreen-lab
spec:
replicas: 2
selector:
matchLabels:
app: web
track: blue
template:
metadata:
labels:
app: web
track: blue
spec:
containers:
- name: web
image: nginx:1.28.0
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: http
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-green
namespace: bluegreen-lab
spec:
replicas: 2
selector:
matchLabels:
app: web
track: green
template:
metadata:
labels:
app: web
track: green
spec:
containers:
- name: web
image: nginx:1.28.0
ports:
- name: http
containerPort: 80
readinessProbe:
httpGet:
path: /
port: http
resources:
requests:
cpu: 100m
memory: 64Mi
limits:
memory: 128Mi
---
apiVersion: v1
kind: Service
metadata:
name: web-live
namespace: bluegreen-lab
spec:
selector:
app: web
track: blue
ports:
- port: 80
targetPort: http
Run kubectl apply -f bluegreen.yaml. The live Service initially selects one version using its track label.
Step 2: Test green before switching
Run:
kubectl -n bluegreen-lab rollout status deployment/web-green --timeout=180s
kubectl -n bluegreen-lab port-forward deployment/web-green 8081:80
While port forwarding is running, request http://127.0.0.1:8081 in another terminal. Check real application functions, not only the readiness probe.
Step 3: Switch the Service selector
Run:
kubectl -n bluegreen-lab patch service web-live --type=merge \
-p '{"spec":{"selector":{"app":"web","track":"green"}}}'
kubectl -n bluegreen-lab get endpointslices -l kubernetes.io/service-name=web-live
Verify EndpointSlices and requests through the live Service. Existing connections may continue using the old version for a while.
Step 4: Keep a tested rollback path
Keep blue available until the observation period ends. To switch back, restore the previous selector and check responses again. Database writes made by green remain; switching traffic does not reverse them.
Check the result
The live Service should reach the intended version. Keep the desired selector in your saved deployment configuration so automated sync does not undo the change.
Conversation
0 commentsNo comments yet. Start the conversation below.