Lesson 45 · Section 5: Application Lifecycle Management
Lesson 45: Rolling updates and rollbacks: ship a new version, and get the old one back
How a rollout is recorded as revisions, how to read and steer one with kubectl rollout, what a stuck rollout looks like, and what an undo really changes, for Deployments, DaemonSets and StatefulSets. · 20 min read · 5 min video
Official competency this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why this gets its own lesson
In the Deployments lesson you saw the mechanics: a Deployment owns one ReplicaSet for each
version of its Pod template, and an update moves Pods from the old ReplicaSet to a new one within the limits of
maxSurge and maxUnavailable. That is enough to start an update.
Running an application is mostly about what happens next. Did the update finish? If it did not, why, and are users affected? Which version ran before, and how do you get it back in thirty seconds? Those are the questions an administrator answers every week, and the CKA curriculum names them directly: "understand application deployments and how to perform rolling update and rollbacks".
Revisions: how a rollout is recorded
Each change to spec.template gives the Deployment a new revision. A revision is not a separate kind of object.
It is simply a ReplicaSet, kept by the Deployment, that holds the complete Pod template of that version:
- The current revision number is stored on the Deployment and on each ReplicaSet in the annotation
deployment.kubernetes.io/revision. - When a rollout finishes, the previous ReplicaSet is left at zero replicas, not deleted.
spec.revisionHistoryLimit(10 by default) says how many of those empty ReplicaSets are kept. Older ones are removed, and with them the chance to roll back to that version.
So kubectl get replicasets is the history, and kubectl rollout history is a friendlier view of it:
kubectl -n shop rollout history deployment/checkout
deployment.apps/checkout
REVISION CHANGE-CAUSE
1 <none>
2 <none>
Add --revision=<n> to see the whole Pod template of one revision. That is how you find out which image, command
or environment a revision ran, without guessing:
kubectl -n shop rollout history deployment/checkout --revision=2
deployment.apps/checkout with revision #2
Pod Template:
Labels: app=checkout
pod-template-hash=7c9b6f5d48
Containers:
nginx:
Image: nginx:1.29
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Only template changes are recorded. Scaling the Deployment, or changing its strategy, does not create a revision, and a rollback does not touch those settings.
Under the hood · Where CHANGE-CAUSE comes from
The CHANGE-CAUSE column shows the annotation kubernetes.io/change-cause. The Deployment controller copies it from
the Deployment to the ReplicaSet of the current revision. Nothing fills it in for you: the old --record flag that
used to do so is deprecated. Set it yourself after a change:
kubectl -n shop annotate deployment/checkout kubernetes.io/change-cause="update to nginx 1.29"
The annotation stays on the Deployment, so the next revision starts with the same text until you annotate again. Make it a habit: change, annotate, check the status.
Watching a rollout
There are three ways to see how a rollout is going, from quickest to most detailed.
| Command | What it tells you |
|---|---|
kubectl rollout status deployment/<name> | Waits and prints progress. Ends with successfully rolled out, or with an error if the rollout is stuck |
kubectl get deployment <name> | UP-TO-DATE is the number of Pods on the new template; AVAILABLE is how many Pods can serve |
kubectl describe deployment <name> | The conditions, the strategy, OldReplicaSets and NewReplicaSet, and the scaling events in order |
The Deployment reports its state in two conditions that are worth recognising in kubectl describe:
| Condition | Reason | Meaning |
|---|---|---|
Progressing=True | ReplicaSetUpdated | A rollout is running and making progress |
Progressing=True | NewReplicaSetAvailable | The rollout is complete |
Progressing=False | ProgressDeadlineExceeded | No progress for progressDeadlineSeconds (600 by default): the rollout is stuck |
Available=True | MinimumReplicasAvailable | Enough Pods are serving, whatever the rollout is doing |
Available=True together with Progressing=False is the typical picture of a bad update that was caught in time:
users are still served by the old Pods, and the new version is going nowhere.
When a rollout gets stuck
Parts:
- Deployment checkout (revision 3, 4 replicas)
- ReplicaSet rev 1 (nginx:1.28 · 0 Pods)
- ReplicaSet rev 2 (nginx:1.29 · 3 Pods)
- ReplicaSet rev 3 (nginx:1.299 · 2 Pods)
- 3 Pods (Running, Ready)
- 2 Pods (ImagePullBackOff)
Connections:
- Deployment checkout to ReplicaSet rev 1
- Deployment checkout to ReplicaSet rev 2: 4 → 3
- Deployment checkout to ReplicaSet rev 3
- ReplicaSet rev 2 to 3 Pods
- ReplicaSet rev 3 to 2 Pods
Take a Deployment checkout with four replicas and default settings. With 25% each, maxSurge is 1 and
maxUnavailable is 1, so during an update there may be at most five Pods, and at least three must be available.
Someone sets the image to nginx:1.299, a tag that does not exist. The controller does exactly what it always does:
- It creates the ReplicaSet for revision 3 and scales it to 1 (five Pods in total, the surge limit).
- It scales the old ReplicaSet down to 3 (three available, the unavailable limit).
- That frees a slot, so it scales the new ReplicaSet to 2 (five Pods again).
- Now it has to wait for a new Pod to become ready before it may remove another old one. The new Pods are in
ImagePullBackOff, so that never happens.
The rollout stops there by itself. This is the protection that maxUnavailable gives you: a broken version can never
take away more than that many Pods. The same thing happens when the new Pods crash on start, fail their readiness
probe, do not fit on any node, or are refused by a quota.
To find the cause, follow the chain down: kubectl describe deployment shows which ReplicaSet is new,
kubectl get pods shows the state of its Pods, and kubectl describe pod or kubectl logs shows why they are not
ready.
Under the hood · Changing the template again in the middle of a rollout
You do not have to wait for a rollout to end, or undo it, before you change the template again. If you correct the image while the broken rollout is still running, the Deployment creates (or reuses) the ReplicaSet for the newest template and starts scaling it up at once. The half-finished ReplicaSet is treated like any other old one and is scaled down. The docs call this a rollover. The controller always works towards the latest template; it never finishes an older rollout first.
Rolling back
Parts:
- You (rollout undo)
- Deployment checkout (template: nginx:1.29)
- ReplicaSet rev 1 (nginx:1.28 · 0 Pods)
- ReplicaSet rev 4 (was rev 2 · 3 → 4 Pods)
- ReplicaSet rev 3 (nginx:1.299 · 2 → 0)
Connections:
- You to Deployment checkout: old template
- Deployment checkout to ReplicaSet rev 4: scales up
- Deployment checkout to ReplicaSet rev 3: scales down
- Deployment checkout to ReplicaSet rev 1
kubectl rollout undo sounds like a special operation, but it is small: it copies the Pod template of an older
revision back into the Deployment. From there everything is an ordinary rollout. The template's hash matches a
ReplicaSet that already exists, so the Deployment scales that one up and the current one down, with the same surge
and unavailable limits as any update.
kubectl -n shop rollout undo deployment/checkout # back to the previous revision
kubectl -n shop rollout undo deployment/checkout --to-revision=1 # or to a revision you choose
Three details decide whether you read the result correctly:
- Revisions only move forward. When you go back to revision 2, that ReplicaSet becomes the newest revision. If
the broken one was revision 3, the history afterwards shows 1, 3 and 4, and revision 2 is gone from the list. A
second
rollout undotherefore takes you back to the broken revision 3, not further into the past. - Only the Pod template is restored. The replica count, the strategy, and anything else outside
spec.templatestay as they are now. - Setting the old values by hand is the same thing.
kubectl set image deployment/checkout nginx=nginx:1.29produces the template of revision 2 again, so the same ReplicaSet is reused. Undo is quicker and cannot contain a typo.
Because a rollback is a rollout, finish it the same way: kubectl rollout status deployment/checkout.
Pause and resume
Parts:
- rollout pause (rollouts on hold)
- set image (nginx:1.29)
- set resources (cpu=100m)
- rollout resume (rollouts allowed)
- One new ReplicaSet (one new revision)
Connections:
- rollout pause to set image
- set image to set resources
- set resources to rollout resume
- rollout resume to One new ReplicaSet: starts
Every change to the template starts a rollout at once. If a task asks for a new image and new resource
requests, two kubectl set commands mean two rollouts and two revisions. Pausing avoids that:
kubectl -n shop rollout pause deployment/checkout
kubectl -n shop set image deployment/checkout nginx=nginx:1.29
kubectl -n shop set resources deployment/checkout -c nginx --requests=cpu=100m,memory=64Mi
kubectl -n shop rollout resume deployment/checkout
kubectl -n shop rollout status deployment/checkout
Pausing sets spec.paused: true. While it is set, the controller still keeps the replica count right (you can
scale a paused Deployment, and lost Pods are replaced), but template changes do not reach any Pod. Pausing in the
middle of a rollout freezes it where it is, with both versions running, which is a simple way to try a new version
on part of the traffic.
Setting the pace
All of the tuning lives in the Deployment's spec, outside the template, so changing it never starts a rollout.
| Field | What it controls | Default |
|---|---|---|
strategy.type | RollingUpdate swaps Pods in steps; Recreate stops all old Pods first (downtime) | RollingUpdate |
strategy.rollingUpdate.maxSurge | Extra Pods allowed above replicas. Percentages round up | 25% |
strategy.rollingUpdate.maxUnavailable | Pods allowed to be unavailable. Percentages round down | 25% |
minReadySeconds | How long a new Pod must stay ready before it counts as available | 0 |
progressDeadlineSeconds | How long without progress before the rollout is reported as stuck | 600 |
revisionHistoryLimit | How many old ReplicaSets are kept for rollbacks | 10 |
Some combinations you will be asked for by description rather than by name:
| The task says | Set |
|---|---|
| "Never fewer than the desired number of Pods" | maxUnavailable: 0, maxSurge: 1 or more |
| "Never more than the desired number of Pods" | maxSurge: 0, maxUnavailable: 1 or more |
| "As fast as possible, capacity may drop by half" | maxUnavailable: 50% |
| "The two versions must never run together" | strategy.type: Recreate |
maxSurge and maxUnavailable cannot both be zero. With type: Recreate the rollingUpdate block must be removed,
or the API server rejects the Deployment.
DaemonSets and StatefulSets roll out too
kubectl rollout status, history, undo and restart work on DaemonSets and StatefulSets as well. (pause and
resume are for Deployments only.) What differs is the field name, spec.updateStrategy, and the order in which
Pods are replaced.
| Deployment | DaemonSet | StatefulSet | |
|---|---|---|---|
| Field | spec.strategy | spec.updateStrategy | spec.updateStrategy |
| Types | RollingUpdate, Recreate | RollingUpdate, OnDelete | RollingUpdate, OnDelete |
| Default pace | 25% surge, 25% unavailable | maxUnavailable: 1, maxSurge: 0: one node at a time, old Pod removed first | One Pod at a time, highest ordinal first |
| History is kept in | ReplicaSets | ControllerRevisions | ControllerRevisions |
With OnDelete, changing the template does nothing until you delete a Pod yourself; its replacement then uses the new template. It is the choice when you want to decide the moment for each node or each database member.
A StatefulSet's rollingUpdate.partition holds part of the set back: only Pods with an ordinal greater than or
equal to the partition are updated. With five replicas and partition: 3, only db-4 and db-3 move to the new
template.
Under the hood · Why undoing a broken StatefulSet update may not be enough
A StatefulSet with the default Pod management waits for each Pod to be running and ready before it touches the next
one. If an update leaves db-4 crashing, the controller waits for db-4 forever, and that includes waiting to apply
your rollback. After kubectl rollout undo statefulset/db you also have to delete the broken Pod; it is then
created again from the restored template and the set carries on. Deployments do not have this problem, because their
Pods are interchangeable.
Using it
Example · Update, record the reason, and confirm
The Deployment checkout in the namespace shop runs four replicas of nginx:1.28. Move it to nginx:1.29.
kubectl -n shop get deployment checkout -o wide # CONTAINERS column: the container is called nginx
kubectl -n shop set image deployment/checkout nginx=nginx:1.29
kubectl -n shop annotate deployment/checkout kubernetes.io/change-cause="update to nginx 1.29"
kubectl -n shop rollout status deployment/checkout
kubectl -n shop rollout history deployment/checkout
deployment.apps/checkout
REVISION CHANGE-CAUSE
1 <none>
2 update to nginx 1.29
Example · Break it, read it, undo it
Set a tag that does not exist and see what the cluster tells you.
kubectl -n shop set image deployment/checkout nginx=nginx:1.299
kubectl -n shop rollout status deployment/checkout --timeout=30s
Waiting for deployment "checkout" rollout to finish: 2 out of 4 new replicas have been updated...
error: timed out waiting for the condition
kubectl -n shop get deployment checkout
kubectl -n shop get pods -l app=checkout
NAME READY UP-TO-DATE AVAILABLE AGE
checkout 3/4 2 3 20m
NAME READY STATUS RESTARTS AGE
checkout-7c9b6f5d48-8xk2p 1/1 Running 0 12m
checkout-7c9b6f5d48-mz4qd 1/1 Running 0 12m
checkout-7c9b6f5d48-w7rjn 1/1 Running 0 12m
checkout-84b6d9f6c5-5hvbt 0/1 ImagePullBackOff 0 2m
checkout-84b6d9f6c5-q9d6l 0/1 ImagePullBackOff 0 2m
Three of four ready, two Pods on the new template, none of them serving. Check where an undo would take you, then do it:
kubectl -n shop rollout history deployment/checkout --revision=2 # Image: nginx:1.29
kubectl -n shop rollout undo deployment/checkout
kubectl -n shop rollout status deployment/checkout
kubectl -n shop rollout history deployment/checkout # revisions 1, 3 and 4
Example · Change the strategy without starting a rollout
The task: "during updates of checkout, the number of available Pods must never drop below four". Edit the
Deployment, or patch it in one line:
kubectl -n shop patch deployment checkout \
-p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}'
kubectl -n shop get deployment checkout -o jsonpath='{.spec.strategy}{"\n"}'
{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0},"type":"RollingUpdate"}
No Pod is replaced and the history does not grow, because the Pod template did not change. The new pace applies from the next rollout.
Example · Roll out and roll back a DaemonSet
The same commands, with a different resource type:
kubectl -n kube-system set image daemonset/node-agent agent=registry.example.com/node-agent:2.4
kubectl -n kube-system rollout status daemonset/node-agent
kubectl -n kube-system rollout history daemonset/node-agent
kubectl -n kube-system rollout undo daemonset/node-agent
kubectl -n kube-system get controllerrevisions -l app=node-agent # where the history is stored
Tricks
Common mistakes
- Not checking the result.
deployment.apps/checkout image updatedonly means the API server accepted the change. Whether the new Pods run is a separate question, answered bykubectl rollout status. - Waiting for an automatic rollback. There is none. A rollout past its deadline stays broken until you act.
- Deleting the failing Pods. The new ReplicaSet creates them again from the same broken template. Fix the template or undo.
- Undoing twice to go back two versions. The second undo returns to the revision you just left. Use
--to-revisionto go further back. - Expecting undo to restore the replica count or the strategy. It restores
spec.templateand nothing else. - Leaving a Deployment paused. Later updates silently do nothing. Always pair
pausewithresume. - Setting
revisionHistoryLimit: 0. No old ReplicaSets are kept, so there is nothing to roll back to. - Writing
strategyon a DaemonSet or StatefulSet. Their field isupdateStrategy, and they have noRecreatetype.
Recap
- Every change to the Pod template is a revision, kept as a ReplicaSet at zero replicas (ControllerRevisions for
DaemonSets and StatefulSets), up to
revisionHistoryLimit. kubectl rollout statusis the check after every change;rollout history --revision=<n>shows what a revision ran.- A rollout that cannot finish stops inside the
maxUnavailablelimit and waits. AfterprogressDeadlineSecondsit is reported as failed, but nothing is rolled back for you. kubectl rollout undocopies an old template back and scales its ReplicaSet up again. Revisions only move forward, and only the template is restored.rollout pauseandrollout resumeturn several changes into one rollout; a paused Deployment cannot be undone.- The pace is set by
maxSurge,maxUnavailable,minReadySecondsand the strategy type, none of which start a rollout when you change them.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
3 hands-on labs for this lesson. You get goals, not steps: any solution that reaches them passes.
Read a stuck rollout and its history, find what went wrong, and get the app back on the last version that worked.
Fix · Level 2 · standard · target 12 min
Set the pace of a rolling update so that no ready pod is taken away early, then roll out a new version and record why.
Build · Level 2 · standard · target 15 min
Section capstone. One Deployment, five jobs: get it past a broken init container, feed it from a ConfigMap and a Secret, add probes, release a new version with no gap, and prepare it for autoscaling.
Fix · Level 3 · challenge · target 30 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.