Sub topic 11
Watching a rollout and rolling back
This chapter assumes you can apply manifests with kubectl, know how Deployments roll out, and have read the earlier chapter on update strategies. You will change the example DaemonSet, watch the rollout, and undo it.
What a rollout is for a DaemonSet
A rollout starts when you change .spec.template. Changes to labels on the DaemonSet itself or to updateStrategy do not create a new revision. With the RollingUpdate strategy, the controller replaces Pods node by node, and never has more than maxUnavailable nodes without a ready Pod.
DaemonSet revisions are stored as ControllerRevision objects. Each one holds a snapshot of the Pod template. This is what rollout undo restores.
Triggering a change
Apply the manifest from the first chapter, then change the image. Setting a new image with kubectl set image also records no change cause, so add an annotation to make the history readable.
kubectl apply -f node-log-agent.yaml
kubectl set image daemonset/node-log-agent agent=busybox:1.37 -n kube-system
kubectl annotate daemonset/node-log-agent -n kube-system \
kubernetes.io/change-cause="upgrade busybox to 1.37" --overwriteThe container is named agent, so agent=busybox:1.37 targets it.
Watching progress
Use rollout status to block until the rollout finishes.
kubectl rollout status daemonset/node-log-agent -n kube-systemIt prints a line such as Waiting for daemon set "node-log-agent" rollout to finish: 1 out of 3 new pods have been updated... and ends with successfully rolled out. The command exits with a non-zero status if you add --timeout=120s and the rollout does not finish in time. That makes it usable in CI.
For a closer look, read the status counters.
kubectl get daemonset node-log-agent -n kube-system
kubectl get pods -n kube-system -l app=node-log-agent -o wide| Column | Meaning |
|---|---|
| DESIRED | Nodes that should run a Pod |
| CURRENT | Nodes running a Pod, old or new |
| READY | Pods that pass readiness checks |
| UP-TO-DATE | Pods matching the latest template |
| AVAILABLE | Ready Pods that have stayed ready for `minReadySeconds` |
During a healthy rollout, UP-TO-DATE climbs to DESIRED while READY dips by at most maxUnavailable.
Reading history
kubectl rollout history daemonset/node-log-agent -n kube-system
kubectl rollout history daemonset/node-log-agent -n kube-system --revision=2The first command lists revisions with their change causes. The second prints the Pod template stored for revision 2. The cluster keeps .spec.revisionHistoryLimit revisions, 10 by default. Set it too low and you lose the revision you want to return to.
Rolling back
kubectl rollout undo daemonset/node-log-agent -n kube-system
kubectl rollout undo daemonset/node-log-agent -n kube-system --to-revision=1Without --to-revision, undo returns to the previous revision. A rollback is itself a rollout: it creates a new revision number with the old template, and it follows the same update strategy. If the strategy is OnDelete, undo only changes the stored template. Pods change when you delete them, so you must delete them yourself.
Common errors
- Rollout never finishes with a bad image. New Pods sit in
ImagePullBackOff. WithmaxUnavailable: 1, the controller stops after the first node and leaves the rest untouched. This limits damage, but the rollout hangs until you runrollout undo. - Status shows no progress. Check the strategy. With
OnDelete,rollout statusdoes not wait for you and nothing updates by itself. - Undo restores a template you did not expect. Check
--revision=Nbefore you undo, because the previous revision may not be the last good one. - Pods updated but still failing. Run
kubectl describe daemonset node-log-agent -n kube-systemand read the Events. Then runkubectl describe podon a failing Pod.
Putting it together
- Apply the DaemonSet and confirm DESIRED equals READY.
- Change the image to a tag that does not exist.
- Run
rollout status --timeout=60sand watch it fail. - Run
rollout undo, thenrollout statusagain. - Confirm with
rollout historythat a new revision was added.
Exercise
You run kubectl rollout undo on a DaemonSet that uses the OnDelete strategy. The command succeeds, but all Pods still run the bad image. Why, and what do you do next?
Show answer
With OnDelete, the controller only creates Pods from the new template when an old Pod is deleted. Undo restored the earlier template but did not replace any running Pods. Delete the Pods on the affected nodes, for example with kubectl delete pod -n kube-system -l app=node-log-agent, or one node at a time. The controller then recreates them from the restored template.
Exercise
During a rollout with maxUnavailable: 1, READY shows 2 of 3 and UP-TO-DATE shows 1, and neither value changes for ten minutes. Name two commands you would run to find the cause.
Show answer
Run kubectl get pods -n kube-system -l app=node-log-agent -o wide to find the Pod that is not ready and its node. Then run kubectl describe pod <name> -n kube-system, or kubectl describe daemonset node-log-agent -n kube-system, to read the Events. These typically show an image pull failure, a failing probe, or a scheduling problem.