Sub topic 13
Rolling back through version-controlled manifests
This chapter assumes you know Deployments, ReplicaSets, kubectl apply, and basic Git. You have already used kubectl rollout undo. Here you will see why many teams prefer to roll back through Git instead.
The problem with imperative rollback
kubectl rollout undo changes the live Deployment. It does not change your manifest file. After an undo, the cluster runs the old Pod template, but web.yaml in your repository still describes the new, broken one.
That mismatch causes two common errors:
- The next
kubectl apply -f web.yamlredeploys the broken version. - Someone reading the repository believes the cluster runs something it does not.
The same drift happens with kubectl set image. The live object and the file diverge, and the file is the one people trust.
Treat Git as the source of truth
In a declarative workflow, the manifest in Git is the desired state. Every change to the cluster is a commit. A rollback is then a new commit that restores the earlier content, followed by an apply.
git log --oneline -- web.yaml
git revert 3f9a1c2
kubectl apply -f web.yaml
kubectl rollout status deployment/webgit revert creates a new commit that undoes the chosen commit. History stays intact, so you can see who changed what and when. Avoid git reset --hard plus a force push on a shared branch. It rewrites history and hides the incident.
Applying the reverted file changes the Pod template back to its earlier content. Kubernetes handles that as an ordinary rollout, so everything you configured earlier still applies: maxSurge, maxUnavailable, readiness probes, minReadySeconds, and progressDeadlineSeconds.
What happens to ReplicaSets
Kubernetes compares the restored template with the retained old ReplicaSets. If one matches, the Deployment scales that ReplicaSet back up instead of creating a new one. This is the same mechanism rollout undo uses. The revision number moves forward, and the old revision number disappears from the history because that ReplicaSet is now current.
This only works while the old ReplicaSet still exists. The revisionHistoryLimit: 5 setting in the earlier manifest keeps five old ones. A very old state may have been pruned. In that case Kubernetes simply creates a fresh ReplicaSet from the manifest, which still works.
The flow
Imperative or declarative?
| Situation | Better choice |
|---|---|
| Production is failing now and you need the fastest fix | `kubectl rollout undo`, then fix the repository right after |
| The cluster is managed from Git | Revert the commit and apply |
| Several people or pipelines deploy | Revert the commit, so the record stays complete |
| Only the image tag changed | Either, but keep Git in step |
If you use rollout undo in an emergency, commit the matching revert immediately. Otherwise the drift problem returns.
Automating it
GitOps tools such as Argo CD and Flux watch a repository and apply it to the cluster for you. With them, a rollback is just the revert commit, and the tool does the apply. They can also correct manual changes, which means a hand-run rollout undo may be overwritten. Check how your tool is configured before relying on it.
You can also record the reason for a change with the change-cause annotation, so rollout history shows why each revision exists. Putting the annotation in the manifest keeps it in Git too:
metadata:
name: web
annotations:
kubernetes.io/change-cause: "revert nginx to 1.26 after failed 1.27 rollout"Exercise
You ran kubectl rollout undo deployment/web during an incident. A teammate then runs kubectl apply -f web.yaml from the main branch. What happens, and how do you prevent it?
Show answer
The file still contains the broken Pod template, so the apply starts a new rollout to the broken version. Prevent it by committing a git revert of the bad change to the main branch, so the file matches what the cluster runs.