Sub topic 9
Rolling back with kubectl rollout undo
This chapter assumes you can apply YAML manifests, use kubectl get and kubectl describe, and know that a Deployment owns ReplicaSets, which own Pods. It covers how to inspect past revisions and return to one when a rollout goes wrong.
How a rollback works
A Deployment keeps old ReplicaSets, scaled to zero, after each rollout. The number it keeps is set by revisionHistoryLimit. Each old ReplicaSet stores the Pod template of one revision.
A rollback does not restore a snapshot of the whole Deployment. It copies the Pod template from an old ReplicaSet back into the Deployment. That edit counts as a Pod template change, so it starts a normal rolling update, using the same maxSurge, maxUnavailable, and readiness rules as any other rollout.
Inspect the history
List the revisions, then look at the Pod template of one of them before you roll back to it.
kubectl rollout history deployment/web
kubectl rollout history deployment/web --revision=2The first command prints a table of revision numbers and CHANGE-CAUSE values. The cause comes from the kubernetes.io/change-cause annotation, so a blank column means nobody set it. The second command prints the image, ports, and other fields of that revision's template. Check the image tag here so you know what you are going back to.
Undo a rollout
Without arguments, undo goes back one revision. With --to-revision, it goes to the revision you name.
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=1
kubectl rollout status deployment/webAlways follow the undo with rollout status. The rollback is a rolling update, so it can also stall if the old template no longer works. For example, the old image may have been deleted from the registry.
Revision numbers after a rollback
Suppose the Deployment has revisions 1 and 2, and you undo to revision 1. Revision 1 does not become current under its old number. Kubernetes takes that ReplicaSet, gives it the next number, and removes the old entry.
| Step | Revisions shown in history |
|---|---|
| Create the Deployment | 1 |
| Update the image | 1, 2 |
| `rollout undo` | 2, 3 (revision 3 has the template that was revision 1) |
This is why rollout undo twice in a row toggles between the two latest templates. The second undo returns to revision 2, which is now the previous one.
Common errors
- History is empty or short.
revisionHistoryLimit: 0deletes old ReplicaSets, so there is nothing to roll back to. The manifest in this tutorial keeps five. - Undo during a paused rollout. A paused Deployment cannot be rolled back. Resume it first, then undo.
- Wrong target.
--to-revisionwith a number that has been pruned returns an error. Runrollout historyfirst. - Rollback does not fix the cause. If the failure came from a ConfigMap, Secret, or database change, the old Pod template does not undo that.
Try it
Run these commands against the web Deployment from earlier chapters. They push a bad image, watch the rollout fail, and recover.
kubectl set image deployment/web web=nginx:does-not-exist
kubectl annotate deployment/web kubernetes.io/change-cause="try bad tag" --overwrite
kubectl rollout status deployment/web --timeout=60s
kubectl get pods -l app=web
kubectl rollout undo deployment/web
kubectl rollout status deployment/webWith maxUnavailable: 0, the old Pods keep serving while the new Pod sits in ErrImagePull. The undo then removes the bad ReplicaSet's Pod.
Exercise
A Deployment has revisions 1, 2, and 3, and revision 3 is live. You run kubectl rollout undo deployment/web --to-revision=1. Which revisions appear in the history afterwards, and what does a further plain kubectl rollout undo do?
Show answer
The history shows 2, 3, and 4. Revision 4 holds the template that was revision 1. A plain undo then goes to the previous revision, which is 3, so it returns to the template that was live before your rollback.
Exercise
You run kubectl rollout undo and the Deployment's replica count does not change from 6 back to the earlier 3. Why?
Show answer
Rollback restores only the Pod template. The replica count is not part of the template, so scaling changes are kept.