Sub topic 13
Checking Rollout Status and Rolling Back
This chapter assumes you can apply manifests with kubectl, and that you know how a Deployment creates a new ReplicaSet when its Pod template changes. Here you will watch a rollout, then undo one.
Watching a rollout
Changing the Pod template starts a rollout. Changing the image is the simplest way to do that.
kubectl set image deployment/web web=nginx:1.26
kubectl rollout status deployment/web
kubectl get rs -l app=webkubectl rollout status blocks and prints progress until the rollout completes or fails. It exits with code 0 on success and a non-zero code on failure, such as when the progress deadline is exceeded. This makes it useful in CI scripts that must stop when a deploy fails.
After completion, kubectl get rs shows the new ReplicaSet with all replicas and the old one scaled to zero. The old ReplicaSet is kept so you can return to it.
Recording why a change happened
History is more useful with a reason attached. Add a kubernetes.io/change-cause annotation, then list revisions.
kubectl annotate deployment/web kubernetes.io/change-cause="upgrade nginx to 1.26"
kubectl rollout history deployment/webThe annotation is copied to the current revision, so set it after the change you want to describe. Without it, the CHANGE-CAUSE column shows <none>.
Rolling back
To return to the previous revision, run:
kubectl rollout undo deployment/webTo return to a specific revision, name it:
kubectl rollout undo deployment/web --to-revision=1To see what a revision contains before you choose it, run kubectl rollout history deployment/web --revision=1.
A rollback is a new rollout. Kubernetes copies the Pod template from the old revision back into the Deployment. The old ReplicaSet matches that template, so the Deployment scales it up again instead of creating a new one. It uses the same maxSurge and maxUnavailable limits as any update.
Revision numbers also change. Rolling back to revision 1 makes that ReplicaSet the newest revision, and it appears in history under a new number. Revision 1 no longer appears as a separate entry.
Common mistakes
- Running
kubectl rollout undowhile the Deployment is paused. A paused Deployment ignores rollbacks until you resume it. - Editing the Pod template directly on a ReplicaSet. The Deployment reverts the edit, so change the Deployment instead.
- Setting
revisionHistoryLimit: 0. This removes your rollback targets.
Exercise
You ran kubectl rollout undo deployment/web and the Deployment is now on the old image. Did Kubernetes create a new ReplicaSet for it, and how did the replica count change in the ReplicaSets?
Show answer
No. The old ReplicaSet already had a Pod template matching the restored one, so the Deployment reused it. It scaled that ReplicaSet up and scaled the newer one down to zero, following maxSurge and maxUnavailable.