Overview and What You Will Learn
Kubernetes has three main ways to release a new version without downtime: a rolling update replaces pods gradually, blue-green runs two full copies and switches traffic at once, and a canary sends a small share of traffic to the new version first. Pick by how much risk you can tolerate and how much spare capacity you can afford.
In this lab, you will:
- Run a rolling update, break it on purpose, and watch Kubernetes keep the old version serving
- Switch a Service between blue and green versions and roll back in seconds
- Split traffic between stable and canary pods and measure the split
- Clean up the namespace
Why This Matters in Production
Every release puts users at risk if it is done carelessly. Picture a food delivery platform like Swiggy shipping during dinner peak: a naive release means thousands of requests hitting errors or half-started pods. The strategy decides how many users a bad version can reach, and how fast you can undo it.
Core Principles
Which Strategy Fits
Each strategy trades blast radius, cost and rollback speed differently.
| Strategy | Extra capacity | Rollback |
|---|---|---|
| Rolling update | Small, set by maxSurge |
kubectl rollout undo, gradual |
| Blue-green | A full second copy | Flip the selector, near instant |
| Canary | A few extra pods | Scale the canary to zero |
A fourth built-in strategy, Recreate, stops every old pod before starting new ones. It causes downtime, so use it only when two versions must never run together.
How a Rolling Update Works
Kubernetes starts new pods up to maxSurge, waits until each passes its readiness probe, and only then terminates old pods, never dropping below the count maxUnavailable allows.
within maxSurge] B --> C{Readiness
probe passes?} C -->|Yes| D[Terminate one old pod] C -->|No| E[Rollout stalls
old pods keep serving] D --> F{More old pods?} F -->|Yes| B F -->|No| G([Rollout complete]) classDef start fill:#475569,color:#FFFFFF classDef step fill:#326CE5,color:#FFFFFF classDef decision fill:#F59E0B,color:#111827 classDef bad fill:#EF4444,color:#FFFFFF classDef good fill:#10B981,color:#FFFFFF class A start class B,D step class C,F decision class E bad class G good
RememberThe readiness probe is what makes a rolling update safe. Without it Kubernetes treats a pod as ready the moment it starts, and users reach a pod that has not finished booting.
How Blue-Green Works
Two Deployments run side by side, distinguished by a slot label. A Service selects one slot, and changing that selector moves all traffic at once.
selector slot}} S -->|slot=blue| B[Blue v1
live] S -.->|slot=green| G[Green v2
idle until switch] classDef user fill:#475569,color:#FFFFFF classDef svc fill:#F59E0B,color:#111827 classDef blue fill:#326CE5,color:#FFFFFF classDef green fill:#10B981,color:#FFFFFF class U user class S svc class B blue class G green
TipBlue-green helps with database changes only if both versions work with the same schema at the same time. Use expand-and-contract migrations: add new columns first, ship the code, and remove old columns in a later release.
How a Replica-Based Canary Works
A Service that selects on app alone spreads connections across every matching pod, stable and canary. With 9 stable pods and 1 canary pod, roughly 10% of new connections reach the canary.
selector app}} S -->|About 90%| ST[Stable v1
9 pods] S -->|About 10%| CA[Canary v2
1 pod] classDef user fill:#475569,color:#FFFFFF classDef svc fill:#F59E0B,color:#111827 classDef stable fill:#326CE5,color:#FFFFFF classDef canary fill:#F97316,color:#FFFFFF class U user class S svc class ST stable class CA canary
The split is approximate and tied to pod counts, and you cannot route by header or exact percentage. For that, use ingress canary annotations, Gateway API traffic weights, a service mesh, or Argo Rollouts, which automates the steps below with metric checks.
Detailed Step-by-Step Practical Lab
The lab uses a throwaway namespace and small public images, so it is safe on any cluster. Demo services answer with their version name, which lets you see which pods served a request.
- Create the namespace:
kubectl create namespace strategies-lab- Deploy a rolling update configuration. With
maxUnavailable: 0andmaxSurge: 2, capacity never drops below 6 pods:
apiVersion: apps/v1kind: Deploymentmetadata: name: order-api namespace: strategies-labspec: replicas: 6 strategy: type: RollingUpdate rollingUpdate: maxSurge: 2 maxUnavailable: 0 selector: matchLabels: app: order-api template: metadata: labels: app: order-api spec: containers: - name: order-api image: nginx:1.26 ports: - containerPort: 80 readinessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 5kubectl apply -f order-api.yamlkubectl rollout status deployment/order-api -n strategies-lab- Roll out a new version, then check the history. In a GitOps workflow you would edit the manifest instead of using
set image, so the change lives in Git:
kubectl set image deployment/order-api order-api=nginx:1.27 -n strategies-labkubectl rollout status deployment/order-api -n strategies-labkubectl rollout history deployment/order-api -n strategies-labkubectl get rs -n strategies-lab- Break a release on purpose. The new pods cannot pull their image, so they never become ready, and the old pods keep serving. Then undo:
kubectl set image deployment/order-api order-api=nginx:does-not-exist -n strategies-labkubectl rollout status deployment/order-api -n strategies-lab --timeout=60s # times out kubectl get pods -n strategies-lab # 6 healthy old pods plus pods stuck in ErrImagePullkubectl rollout undo deployment/order-api -n strategies-labkubectl rollout status deployment/order-api -n strategies-lab- Set up blue-green. Create both slots from one template, and point the Service at blue:
for pair in blue:v1 green:v2; do SLOT=${pair%%:*} VERSION=${pair##*:} cat << EOF | kubectl apply -f -apiVersion: apps/v1kind: Deploymentmetadata: name: payment-api-$SLOT namespace: strategies-labspec: replicas: 3 selector: matchLabels: app: payment-api slot: $SLOT template: metadata: labels: app: payment-api slot: $SLOT spec: containers: - name: payment-api image: hashicorp/http-echo:1.0 args: ["-text=payment-api $SLOT $VERSION", "-listen=:8080"] ports: - containerPort: 8080EOFdone cat << 'EOF' | kubectl apply -f -apiVersion: v1kind: Servicemetadata: name: payment-api namespace: strategies-labspec: selector: app: payment-api slot: blue ports: - port: 80 targetPort: 8080EOF kubectl get pods -n strategies-lab -l app=payment-api- Test green directly, switch traffic, then roll back. A short-lived curl pod shows which slot answers:
kubectl port-forward deployment/payment-api-green 8081:8080 -n strategies-lab &sleep 2 && curl -s localhost:8081 && kill %1 # payment-api green v2, before any user sees it curl_pod() { kubectl run curl --rm -i --restart=Never --image=curlimages/curl \ -n strategies-lab --command -- curl -s payment-api 2>/dev/null} curl_pod # blue v1 kubectl patch service payment-api -n strategies-lab \ -p '{"spec":{"selector":{"slot":"green"}}}'curl_pod # green v2 kubectl patch service payment-api -n strategies-lab \ -p '{"spec":{"selector":{"slot":"blue"}}}'curl_pod # blue v1 again, rollback done- After green is confirmed stable, scale blue to zero rather than deleting it, so you can still switch back quickly:
kubectl patch service payment-api -n strategies-lab \ -p '{"spec":{"selector":{"slot":"green"}}}'kubectl scale deployment payment-api-blue --replicas=0 -n strategies-lab- Set up a canary with 9 stable pods and 1 canary pod behind one Service:
for pair in stable:9:v1 canary:1:v2; do IFS=: read TRACK REPLICAS VERSION <<< "$pair" cat << EOF | kubectl apply -f -apiVersion: apps/v1kind: Deploymentmetadata: name: notification-api-$TRACK namespace: strategies-labspec: replicas: $REPLICAS selector: matchLabels: app: notification-api track: $TRACK template: metadata: labels: app: notification-api track: $TRACK spec: containers: - name: notification-api image: hashicorp/http-echo:1.0 args: ["-text=notification-api $VERSION", "-listen=:8080"] ports: - containerPort: 8080EOFdone cat << 'EOF' | kubectl apply -f -apiVersion: v1kind: Servicemetadata: name: notification-api namespace: strategies-labspec: selector: app: notification-api ports: - port: 80 targetPort: 8080EOF- Measure the split with 50 requests from inside the cluster. Expect roughly 45 stable and 5 canary, with some random variation:
measure() { kubectl run curl-loop --rm -i --restart=Never --image=curlimages/curl \ -n strategies-lab --command -- sh -c \ 'for i in $(seq 1 50); do curl -s notification-api; done' 2>/dev/null | sort | uniq -c} measure- Increase the canary. Scale the canary up first, so total capacity never dips, then scale stable down. Re-measure at each step, and in a real release watch error rate and latency in your metrics before moving on:
kubectl scale deployment notification-api-canary --replicas=3 -n strategies-labkubectl scale deployment notification-api-stable --replicas=7 -n strategies-labmeasure kubectl scale deployment notification-api-canary --replicas=0 -n strategies-labkubectl scale deployment notification-api-stable --replicas=9 -n strategies-labIn a real release, you finish by updating the stable Deployment to the new image and scaling the canary to zero, which keeps one Deployment as the source of truth.
- Clean up:
kubectl delete namespace strategies-labProduction Best Practices & Common Pitfalls
Common MistakeUsing
maxSurge: 0withmaxUnavailable: 1. Kubernetes then removes an old pod before a new one is ready, so capacity dips during every release and a traffic spike drops requests. UsemaxUnavailable: 0for production. Setting both to zero is rejected.
TipAdd a PodDisruptionBudget and a sensible
terminationGracePeriodSecondswith apreStophook, so in-flight requests finish when old pods are removed.
- Define a readiness probe on every Deployment. It is the gate that stops traffic reaching unready pods during any strategy.
- Keep the old blue Deployment at zero replicas for at least a day. A bug found the next morning can then be undone by flipping the selector.
- Start canaries small and watch real signals. Begin at 5 to 10% for at least 15 minutes and check error rate, latency and memory, because a Running pod only proves it started.
kubectl topshows resource use, not errors. - Mind autoscalers. An HPA on both canary and stable Deployments fights manual scaling, so pause it or use a tool that manages traffic by weight.
- Make sure both versions can run together. All three strategies briefly run old and new code at once, so APIs and database schemas must stay compatible.
- Keep revision history.
revisionHistoryLimitcontrols how many ReplicaSets remain forrollout undo.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
kubectl rollout status deployment/NAME |
Watch a rolling update |
kubectl rollout undo deployment/NAME |
Roll back to the previous revision |
kubectl rollout history deployment/NAME |
List past revisions |
kubectl rollout pause deployment/NAME |
Pause a rollout midway |
kubectl rollout resume deployment/NAME |
Resume a paused rollout |
kubectl patch service NAME -p '{"spec":{"selector":{"slot":"green"}}}' |
Switch blue-green traffic |
kubectl scale deployment NAME --replicas=N |
Change the canary share |
| Problem | Likely cause | Fix |
|---|---|---|
| Rollout stuck | New pods fail readiness or image pull | Run kubectl describe pod and fix or undo |
| Errors during a rolling update | No readiness probe | Add one before the next release |
| Service has no endpoints after a switch | Selector matches no pods | Compare Service selector and pod labels |
| Canary gets the wrong share | Pod counts or keep-alive connections | Check replicas, or use weight-based routing |
rollout undo does nothing useful |
Revision history was pruned | Raise revisionHistoryLimit |