Sub topic 14
When to go beyond rolling updates: canary and blue-green
This chapter assumes you can read a Deployment manifest, know how a Deployment uses ReplicaSets, and have used kubectl rollout and kubectl rollout undo. It covers what the built-in rolling update cannot do, and two common patterns that fill the gap.
What a rolling update cannot do
A rolling update replaces Pods gradually, but it has fixed limits:
- No traffic control. Traffic is split by Pod count. With 4 replicas and one new Pod, roughly a quarter of requests reach the new version. You cannot send exactly 5% to it.
- No metric-based decisions. Readiness probes and
minReadySecondscheck whether a Pod is up. They do not check error rates or latency. - No automatic rollback. If
progressDeadlineSecondsexpires, the Deployment is marked as failed. It does not undo itself. A new version that is ready but returns wrong answers will roll out to every Pod. - No instant switch back.
kubectl rollout undois a new rollout. It takes as long as the forward update did.
If these limits are acceptable for a service, keep the built-in rolling update. It needs no extra tools.
Canary releases
A canary release sends a small share of traffic to the new version first. You watch it, then widen the share or abort.
You can build a basic canary with plain Kubernetes. Run two Deployments with the same app: web label, and point one Service at that label. Traffic splits by Pod count.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-canary
spec:
replicas: 1
selector:
matchLabels:
app: web
track: canary
template:
metadata:
labels:
app: web
track: canary
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80If the stable Deployment web has 9 Pods labeled app: web and track: stable, this canary receives about 10% of requests. A Service that selects only app: web covers both. To abort, delete web-canary. To promote, update the image in web and then delete the canary.
The limit is precision. Finer splits need more Pods, and you do the metric checks yourself.
Blue-green deployments
A blue-green deployment runs two full copies of the application. Blue is live. Green runs the new version and receives no user traffic until you switch.
With plain Kubernetes, you create a second Deployment and change the Service selector to move traffic.
kubectl patch service web -p '{"spec":{"selector":{"app":"web","version":"green"}}}'Rollback is the same command with blue. The switch is nearly instant, and you can test green before it goes live. The cost is double the capacity while both run.
Comparing the options
| Approach | Traffic control | Rollback speed | Extra capacity | Extra tooling |
|---|---|---|---|---|
| Rolling update | By Pod count, gradual | Slow (a new rollout) | Up to `maxSurge` | None |
| Canary | Small share, set by Pod ratio | Fast (delete canary) | Small | None for basic form |
| Blue-green | All or nothing | Fast (switch selector) | Full second copy | None for basic form |
Tools for automation
Tools such as Argo Rollouts and Flagger automate these patterns. Service meshes and ingress controllers that support weighted routing allow exact splits such as 5%, independent of Pod count. These tools can also read metrics and promote or abort without a person watching.
Decision criteria
- Use the built-in rolling update when brief exposure of all users to a bad version is tolerable and rollback by
undois fast enough. - Use a basic canary when you want early warning from a small share of traffic and can watch metrics manually.
- Use blue-green when you need an instant switch back, or must test the full new stack before release, and can pay for double capacity.
- Adopt an automation tool when releases are frequent or risky enough that manual watching does not scale.
Exercise
A service has 10 replicas. You want 5% of traffic to reach a new version before wider release, using only plain Deployments and a Service. Why is that hard, and what would you use instead?
Show answer
A Service splits traffic roughly evenly across Pods, so 5% of traffic needs about 5% of Pods. With 10 replicas, one canary Pod gives about 9%, and a fraction of a Pod is impossible. You would need around 20 Pods in total for one canary Pod, or use a weighted-routing tool such as a service mesh, an ingress controller with traffic weights, or Argo Rollouts or Flagger.
Exercise
After switching a Service selector from blue to green, error rates rise. What is the fastest rollback, and what must still be running for it to work?
Show answer
Patch the Service selector back to blue. The blue Deployment must still be running with its Pods ready, so do not scale it down until green is confirmed healthy.