Sub topic 9
Rollback: how each strategy undoes a bad release
This chapter assumes you know what rolling, blue-green, and canary deployments are, and that you have basic Kubernetes experience with Deployments and Services. It covers how each strategy reverses a bad release, how fast that reversal is, and what can still go wrong afterward.
What rollback means
A rollback returns users to the last known good version. Each strategy gives you a different mechanism for this, and the mechanism decides how quickly you can recover. Two questions matter in every case:
- How much traffic is hit by the bad version before you notice?
- How many steps does the undo take?
A rollback only restores code. It does not undo side effects such as data written by the bad version. The last section of this chapter returns to that limit.
Rolling deployment rollback
A rolling update replaces pods in batches, so a rollback is another rolling update in the opposite direction. Kubernetes keeps previous ReplicaSets, and each one records a revision of the pod template.
Watch the update, and undo it if it stalls or the new version misbehaves:
kubectl rollout status deployment/web
kubectl rollout undo deployment/webrollout undo returns the Deployment to its previous revision. It follows the same maxSurge and maxUnavailable settings as the forward update. With maxUnavailable: 0, capacity stays at four ready pods during the undo as well.
If the new pods never pass their readiness probe, the rollout stalls instead of failing. The old pods keep serving, so users are mostly unaffected. You still have to run the undo yourself, because a stalled rollout does not revert on its own.
To see which revisions exist, run kubectl rollout history deployment/web. You can also target a specific one with kubectl rollout undo deployment/web --to-revision=2.
The weakness of rolling rollback is time. If the bad version was already on half the pods, half of your users have been exposed, and the undo takes as long as an update does.
Blue-green rollback
Blue-green keeps the old environment running after cutover, so rollback is a second switch. The cutover changed the Service selector from blue to green. To roll back, change it back:
kubectl patch service web -p '{"spec":{"selector":{"app":"web","version":"blue"}}}'This takes effect in seconds and moves all traffic at once. That is its strength, and also its cost. You pay for two full environments for as long as you want this fast path available. Once you scale blue down or delete it, the rollback is no longer a switch. It becomes a new deployment.
Decide how long to keep the old environment. A common choice is to hold it until the new version has run through a full traffic cycle, such as a daily peak.
Canary rollback
A canary exposes only a small share of traffic, so rollback is mostly about removing the canary. With the NGINX Ingress controller, delete the canary Ingress, or set the weight to zero:
kubectl annotate ingress web-canary \
nginx.ingress.kubernetes.io/canary-weight=0 --overwriteThe primary Ingress already serves the stable version, so nothing else has to move. The damage is limited to the canary's share of requests, for example 10%, for the time it ran. Canary rollback is the cheapest of the three because the stable version never stopped serving.
Comparing the three
| Strategy | Undo mechanism | Speed | Users exposed to the bad version | Extra cost |
|---|---|---|---|---|
| Rolling | Reverse rolling update | Minutes | Share of pods already replaced | Low |
| Blue-green | Switch selector back | Seconds | All users, until the switch | High (two environments) |
| Canary | Remove canary routing | Seconds | Canary share only | Low to medium |
Common errors
- Deleting the old environment too early. Blue-green rollback depends on blue still running.
- Rolling back code but not data. If the new version changed the schema or wrote data in a new format, the old version may fail on it. Keep schema changes backward compatible so that both versions can run against the same database.
- Assuming a stalled rollout reverted. A rollout that is stuck on failing readiness probes stays stuck until you run the undo.
- Rolling back by redeploying the old image through the full pipeline. This is slower than the built-in undo or switch. Use the pipeline only when no faster path exists.
- Skipping the check afterward. After any rollback, confirm that error rate and latency have returned to normal.
Exercise
A blue-green release was cut over 20 minutes ago and error rates are climbing. The team scaled the blue environment to zero replicas 10 minutes ago to save cost. What is the fastest safe rollback, and why is it slower than expected?
Show answer
Scale blue back up, wait for its pods to pass readiness probes, and then patch the Service selector back to blue. It is slower than a normal blue-green rollback because the instant switch depended on blue still running. With blue scaled down, the rollback needs a scale-up and a readiness wait before the switch.
Exercise
A rolling update of the web Deployment is stuck, and the new pods keep failing their readiness probe. Do users see errors, and what command do you run?
Show answer
Users are mostly unaffected, because the old pods keep serving and the new pods receive no traffic until they are ready. The rollout will not revert by itself, so run kubectl rollout undo deployment/web.