Sub topic 14
Revision history and change-cause annotations
This chapter assumes you can apply a Deployment, read kubectl get rs output, and know that a template change creates a new ReplicaSet. Here you will see how Kubernetes stores past revisions, how to label them, and how to roll back.
How revisions are stored
A Deployment keeps its history as ReplicaSets. Each time you change the Pod template, the Deployment creates a new ReplicaSet and records a revision number on it in the deployment.kubernetes.io/revision annotation. The old ReplicaSets stay in the cluster, scaled to zero Pods. They are the rollback targets.
Changes that do not touch the Pod template, such as scaling, do not create a revision.
Limiting history with revisionHistoryLimit
The field spec.revisionHistoryLimit sets how many old ReplicaSets the Deployment keeps. The default is 10. In our manifest it is set explicitly:
spec:
revisionHistoryLimit: 10Kubernetes deletes the oldest ReplicaSets beyond this limit. You can no longer roll back to a deleted revision. Setting the value to 0 removes all history, so kubectl rollout undo stops working.
Recording a change cause
By default, kubectl rollout history shows <none> in the CHANGE-CAUSE column, which makes revisions hard to tell apart. Kubernetes fills that column from the kubernetes.io/change-cause annotation on the Deployment. When a new revision is created, the annotation's value is copied to it.
Set the annotation after you make a change:
kubectl set image deployment/web web=nginx:1.26
kubectl annotate deployment/web kubernetes.io/change-cause="upgrade nginx to 1.26"
kubectl rollout history deployment/webAnnotating changes only the Deployment's metadata, not its Pod template, so it does not start a new rollout. Because the annotation is copied to the current revision, set it after the change that created that revision.
Inspecting and rolling back
List all revisions, then look at one in detail:
kubectl rollout history deployment/web
kubectl rollout history deployment/web --revision=2The detailed view prints the Pod template for that revision, so you can check the image before you restore it.
To roll back, use undo:
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=1Without --to-revision, the Deployment returns to the previous revision. Rollback is a normal rolling update. The Deployment copies the old ReplicaSet's template back into its own spec, and the same maxSurge and maxUnavailable rules apply.
Revision numbers after a rollback
Rolling back does not move you "back" in the list. If you are at revision 3 and undo to revision 1, the old ReplicaSet becomes the newest revision and is renumbered 4. Revision 1 disappears from the list, because its template is now revision 4. This is a common source of confusion when you read history after a rollback.
Common errors
unable to find specified revision: the revision was pruned byrevisionHistoryLimit, or the number is wrong. Check the history first.no rollout history found: the limit is 0, or no template change has happened.- Rolling back to an identical template does nothing, because no new ReplicaSet is needed.
- Editing only labels or annotations on the Deployment is not a template change, so it adds no revision.
Exercise
A Deployment is at revision 4. You run kubectl rollout undo deployment/web --to-revision=2. What revision number does the Deployment have afterwards, and does revision 2 still appear in kubectl rollout history?
Show answer
The Deployment becomes revision 5. The ReplicaSet that held revision 2 is renumbered to 5, so revision 2 no longer appears in the history list.