Sub topic 8
Revision history and recording the cause of a change
This chapter assumes you can apply manifests with kubectl, and that you know a Deployment creates one ReplicaSet per Pod template version. Here you will see how those ReplicaSets become a revision history, how to read it, and how to label each revision with the reason for the change.
How revisions are stored
A Deployment does not keep a separate history record. Each revision is an old ReplicaSet that the Deployment keeps around, scaled to zero Pods. The Deployment stores a revision number on each ReplicaSet in the annotation deployment.kubernetes.io/revision.
Two settings matter:
- Only changes to the Pod template create a new revision. Scaling the replica count does not.
spec.revisionHistoryLimitsets how many old ReplicaSets are retained. The default is 10. The example Deployment sets it to 5.
Once the limit is exceeded, the oldest ReplicaSets are deleted and you can no longer roll back to them. Setting the limit to 0 removes all history, so kubectl rollout undo stops working.
Creating revisions and recording the cause
Changing the image edits the Pod template and creates a new revision. The kubernetes.io/change-cause annotation on the Deployment is copied into the revision, so the history shows why it was made.
kubectl apply -f web.yaml
kubectl set image deployment/web web=nginx:1.27
kubectl annotate deployment/web kubernetes.io/change-cause="upgrade nginx to 1.27" --overwrite
kubectl rollout status deployment/web
kubectl get replicasets -l app=webThe kubectl get replicasets output shows the new ReplicaSet with the desired Pod count and the previous one at 0.
Annotations on the Deployment's own metadata are not part of the Pod template. Setting change-cause therefore does not trigger a rollout by itself.
Reading the history
kubectl rollout history deployment/web
kubectl rollout history deployment/web --revision=2The first command lists each revision number with its change cause. A revision without an annotation shows <none>. The second command prints the full Pod template of one revision, which is how you check what a revision actually contained before you restore it.
Revision numbers always increase. If you roll back to revision 1, that template is re-used, and the history lists it under a new, higher number. The old number disappears from the list.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| History shows `<none>` for every revision | No `change-cause` annotation was set | Annotate the Deployment after each change, or put the annotation in the manifest |
| Annotation describes the wrong change | Annotation was set before the template edit and kept from the last revision | Set the annotation in the same step as the change, and use `--overwrite` |
| `rollout undo` reports no revision to roll back to | Only one revision exists, or `revisionHistoryLimit` is 0 | Keep a limit of at least 1 |
| An old revision is missing | It was pruned by `revisionHistoryLimit` | Raise the limit, or redeploy from a manifest in version control |
The annotation is copied when the revision is created. Annotating after a rollout that has already created its revision can leave that revision with the earlier text. For repeatable results, put the annotation in the manifest next to the template change, so both reach the cluster in one kubectl apply.
metadata:
name: web
annotations:
kubernetes.io/change-cause: "upgrade nginx to 1.27"Choosing a history limit
Each retained ReplicaSet is a small object in etcd, but it costs no running Pods. A limit of 5 to 10 covers most teams. Lower it only if you have very many Deployments and object count is a concern.
Exercise
You run kubectl scale deployment/web --replicas=6. Does this add a revision to the history? Then you run kubectl set image to change the image. What happens to the history, and why does kubectl rollout history show <none> as the cause for that new revision if you never annotated?
Show answer
Scaling does not add a revision, because the Pod template is unchanged. The image change edits the Pod template, so a new ReplicaSet is created and becomes the next revision number. The cause shows <none> because the kubernetes.io/change-cause annotation was never set on the Deployment. Annotating it, ideally in the same apply as the change, records the reason.
Exercise
A Deployment has revisionHistoryLimit: 2. After many updates, you try to roll back to revision 3 and it is gone. What two options do you have?
Show answer
Raise revisionHistoryLimit so more old ReplicaSets are retained in future, and redeploy the old Pod template from a manifest kept in version control, since the pruned ReplicaSet cannot be recovered.