Pods and workloadsLesson 5 of 7
Deployments and rolling updates
A Deployment turns each change to its pod template into a new ReplicaSet, then trades pods from old to new at a pace set by maxSurge, maxUnavailable and readiness.
Shows the lesson one block at a time. Arrow keys step, number keys jump to a scene, and Escape returns to the page where you were.
A pod on its own is gone when its node fails, and changing a running pod’s image means replacing the pod. A Deployment takes both problems off your hands: you declare how many copies of which pod template you want, and its controller keeps that many running and replaces them gradually when the template changes. This lesson follows that machinery from the objects it creates to the conditions it reports when a release goes wrong.
Reconcile, in 7 scenes
Deployment, ReplicaSet, Pod
A Deployment does not create pods. It creates a ReplicaSetA controller object that keeps a set number of pods matching its selector running, creating pods from its template when there are too few and deleting them when there are too many., and the ReplicaSet controller creates the pods. Each pod carries an owner reference to its ReplicaSet and each ReplicaSet one to its Deployment, so deleting the Deployment garbage-collects everything below it.
Every distinct pod template gets its own ReplicaSet. When you change the template, the Deployment controller creates a second ReplicaSet and shifts replicas from the old one to the new one. Both sets of pods carry the Deployment’s labels, so both match its selector. Something has to keep the two ReplicaSets from claiming each other’s pods, and that is the pod-template-hash label. The controller hashes the pod template, adds the hash to the ReplicaSet’s selector, to its pod template labels and to any pods it adopts, and names the ReplicaSet <deployment>-<hash>. Pods then get a random suffix after that name.
Manifestweb.yaml, the Deployment this lesson follows
- deployment.apps/web
The preview runs once this page's cluster starts.$ kubectl diff -f web.yamlThe strategy, progress deadline and history limit above are the defaults written out; minReadySeconds defaults to 0.
kubectl apply -f web.yaml
deployment.apps/web created
kubectl get deployment,replicaset -l app=web
NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/web 0/4 4 0 0s NAME DESIRED CURRENT READY AGE replicaset.apps/web-78c7dc78cb 4 4 0 0s
kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS web-78c7dc78cb-crwtn 0/1 Pending 0 0s app=web,pod-template-hash=78c7dc78cb web-78c7dc78cb-dq87d 0/1 Pending 0 0s app=web,pod-template-hash=78c7dc78cb web-78c7dc78cb-nshlv 0/1 Pending 0 0s app=web,pod-template-hash=78c7dc78cb web-78c7dc78cb-pggpj 0/1 Pending 0 0s app=web,pod-template-hash=78c7dc78cb
The columns of kubectl get deployment are worth reading precisely. READY is ready pods over desired replicas, UP-TO-DATE counts pods running the newest template, and AVAILABLE counts pods that have been ready for at least minReadySeconds. During a rollout the three drift apart, and the gaps between them say where the rollout is.
The ReplicaSets belong to the Deployment. If you scale an owned ReplicaSet directly, the Deployment controller scales it back on its next pass. A ReplicaSet also adopts any pod that matches its selector and has no controller, but because the hash is part of that selector, a bare pod labelled only app: web matches none of them and stays unowned.
Apply web.yaml above, then take pods away from it. Deleting a pod leaves the ReplicaSet one short, and it creates a pod with a new name; draining a node evicts every web pod there, and the ReplicaSet places the replacements on the other workers.
Take pods from web
- Runs kubectl delete pod <pod>. Its ReplicaSet notices one replica missing and makes a new pod.
- Runs kubectl drain <worker> --ignore-daemonsets --delete-emptydir-data. The node is cordoned and its pods evicted; their controllers start them elsewhere.
Each button runs its command on the cluster. This line follows what happens next.
What triggers a rollout
A rollout starts when, and only when, spec.template changes: an image, an environment variable, a label or annotation on the template, a probe. Each such change creates a new revisionA numbered version of a Deployment’s pod template, recorded in the deployment.kubernetes.io/revision annotation on the Deployment and on the ReplicaSet that runs it.. Changing replicas, the strategy or the progress deadline edits the Deployment without touching the template, so no new ReplicaSet appears.
Each transcript from here on starts by applying its manifest and waiting for the rollout, so it runs on its own.
kubectl apply -f web.yaml
deployment.apps/web created
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl scale deployment web --replicas=6
deployment.apps/web scaled
kubectl get rs
NAME DESIRED CURRENT READY AGE web-78c7dc78cb 6 6 4 15s
kubectl rollout history deployment/web
deployment.apps/web REVISION CHANGE-CAUSE 1 <none>
kubectl rollout restart relies on the same rule. It writes a kubectl.kubernetes.io/restartedAt timestamp into the template’s annotations, which is a template change, so every pod is replaced through a normal rolling update. If a change brings the template back to one an older ReplicaSet already has, the controller scales that ReplicaSet up again instead of creating a new one, and gives it the next revision number.
RollingUpdate math
The default strategy, RollingUpdate, keeps the old ReplicaSet serving while the new one grows. Two numbers bound each step:
maxSurgeis how many pods may exist abovereplicas. A percentage rounds up.maxUnavailableis how many of the desired pods may be unavailable. A percentage rounds down.
Both default to 25%, and they cannot both be 0. With 10 replicas, 25% surge is 2.5 rounded up to 3, so there are never more than 13 pods; 25% unavailable is 2.5 rounded down to 2, so at least 8 stay available. There is no range in either number.
The controller does not wait for a new pod before removing an old one when maxUnavailable allows it. As the rollout starts, it scales the new ReplicaSet up by the surge and, straight after, scales the old one down by the unavailable budget, before any new pod is ready.
kubectl apply -f web.yaml
deployment.apps/web created
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl scale deployment web --replicas=10
deployment.apps/web scaled
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 4 of 10 updated replicas are available... deployment "web" successfully rolled out
kubectl set image deployment/web web=ghcr.io/kx-labs/web:1.1
deployment.apps/web image updated
kubectl get rs
NAME DESIRED CURRENT READY AGE web-5dc4c76945 5 5 0 0s web-78c7dc78cb 8 8 8 27s
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 5 out of 10 new replicas have been updated... Waiting for deployment "web" rollout to finish: 3 old replicas are pending termination... deployment "web" successfully rolled out
kubectl events --for deployment/web
LAST SEEN TYPE REASON OBJECT MESSAGE 55s Normal ScalingReplicaSet Deployment/web Scaled up replica set web-78c7dc78cb from 0 to 4 40s Normal ScalingReplicaSet Deployment/web Scaled up replica set web-78c7dc78cb from 4 to 10 28s Normal ScalingReplicaSet Deployment/web Scaled up replica set web-5dc4c76945 from 0 to 3 28s Normal ScalingReplicaSet Deployment/web Scaled down replica set web-78c7dc78cb from 10 to 8 28s Normal ScalingReplicaSet Deployment/web Scaled up replica set web-5dc4c76945 from 3 to 5 13s Normal ScalingReplicaSet Deployment/web Scaled down replica set web-78c7dc78cb from 8 to 3 12s Normal ScalingReplicaSet Deployment/web Scaled up replica set web-5dc4c76945 from 5 to 10 1s Normal ScalingReplicaSet Deployment/web Scaled down replica set web-78c7dc78cb from 3 to 0
Scroll the same rollout one controller pass at a time. The stage holds the two ReplicaSets, the 13-pod ceiling and the floor of 8, and each step names the event that pass recorded.
kubectl set image deployment/web web=nginx:1.28
web-d764c49b4 revision 110 ready
web-6d7c488dd5 revision 20 pods
Pods10 of 13
Available10, floor 8
Scaled up replica set web-d764c49b4 from 0 to 10Normal ScalingReplicaSet deployment/web
10 replicas of
webrun revision 1, all available.maxSurge: 25%allows 3 pods above the 10 replicas (25% of 10 is 2.5, rounded up to 3), so there are never more than 13.maxUnavailable: 25%allows 2 to be unavailable (25% of 10 is 2.5, rounded down to 2), so at least 8 stay available.kubectl set image deployment/web web=nginx:1.28changes the pod template, so the Deployment gets a second ReplicaSet. The controller scales it from 0 to 3: 13 pods, the most there may be. The new pods are wanted, not ready.Before any new pod is ready, the controller scales the old ReplicaSet down from 10 to 8: 8 available, exactly the floor.
Old pods are gone, so there is room under the ceiling again. The new ReplicaSet goes from 3 to 5: 13 pods, the most there may be.
5 new pods become ready: 13 available, 5 above the floor of 8, so the controller may remove old pods.
With 5 pods from the new template available, the old ReplicaSet can fall from 8 to 3, because 5 + 3 still meets the floor of 8.
Old pods are gone, so there is room under the ceiling again. The new ReplicaSet goes from 5 to 10: 13 pods, the most there may be.
5 new pods become ready: 13 available, 5 above the floor of 8, so the controller may remove old pods.
The old ReplicaSet scales from 3 to 0. It stays, empty, as revision 1: the template
kubectl rollout undogoes back to. Rollout complete, with 10 new pods available.
| maxSurge | maxUnavailable | Behaviour |
|---|---|---|
| 25% | 25% | The default: a balance of speed and spare capacity |
| 1 | 0 | Never below full capacity; needs room for one extra pod |
| 0 | 1 | No extra capacity needed; runs one pod short during the rollout |
| 100% | 0 | Starts a full new set before removing any old pod; doubles usage briefly |
Surge needs somewhere to land. With maxUnavailable: 0, a rollout cannot remove an old pod until a surge pod is available, so on a cluster with no room for one extra pod the surge pod stays Pending and the rollout makes no progress at all. Either leave room, or allow one pod of unavailability so the controller can free a slot first.
ExplainerA rolling update
25% of 4 is 1
25% of 4 is 1
Old pods leave only as new ones become available
The Deployment controller's arithmetic: one step per change, each new pod ready exactly the delay after it is created.
Old ReplicaSet nginx:1.27
spec.replicas 44 pods, all ready
New ReplicaSet nginx:1.28
spec.replicas 00 of 4 desired pods ready
kubectl get rs
Choose Cluster to read the ReplicaSets back from a simulated cluster at each second.
Step 0 of 8, 0 s in
The old ReplicaSet runs 4 of 4 pods ready. Step to start the rollout.
The Deployment controller's arithmetic: one step per change, each new pod ready exactly the delay after it is created.
The old ReplicaSet runs 4 of 4 pods ready. Step to start the rollout.
Two more behaviours follow from the same bookkeeping. If you change the template again in the middle of a rollout, the controller does not finish the first one: the ReplicaSet it was growing joins the old ones and is scaled down, which upstream calls a rollover. If you scale a Deployment mid-rollout, the extra replicas are split across the old and new ReplicaSets in proportion to their size.
Recreate
Some applications cannot run two versions side by side: a schema migration that the old version cannot read, a singleton that holds a lock, or a volume that only one pod may mount. strategy.type: Recreate handles them by terminating every old pod and waiting until they are gone before creating any new one. The price is a gap with no pods at all.
apiVersion: apps/v1
kind: Deployment
metadata:
name: ledger
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: ledger
template:
metadata:
labels:
app: ledger
spec:
containers:
- name: ledger
image: ghcr.io/kx-labs/web:1.0
The lesson’s working directory holds this manifest as ledger.yaml.
kubectl apply -f ledger.yaml
deployment.apps/ledger created
kubectl rollout status deployment/ledger
Waiting for deployment "ledger" rollout to finish: 0 of 3 updated replicas are available... deployment "ledger" successfully rolled out
kubectl set image deployment/ledger ledger=ghcr.io/kx-labs/web:1.1
deployment.apps/ledger image updated
kubectl get pods
NAME READY STATUS RESTARTS AGE ledger-847d66ff5-dq87d 1/1 Terminating 0 4s ledger-847d66ff5-nshlv 1/1 Terminating 0 4s ledger-847d66ff5-pggpj 1/1 Terminating 0 4s
kubectl rollout status deployment/ledger
Waiting for deployment "ledger" rollout to finish: 0 out of 3 new replicas have been updated... Waiting for deployment "ledger" rollout to finish: 0 of 3 updated replicas are available... deployment "ledger" successfully rolled out
kubectl get rs,pods
NAME DESIRED CURRENT READY AGE replicaset.apps/ledger-67d959d884 3 3 3 4s replicaset.apps/ledger-847d66ff5 0 0 0 10s NAME READY STATUS RESTARTS AGE pod/ledger-67d959d884-752xx 1/1 Running 0 3s pod/ledger-67d959d884-tv48k 1/1 Running 0 3s pod/ledger-67d959d884-xctrl 1/1 Running 0 3s
The guarantee covers upgrades only. If you delete one pod by hand, its ReplicaSet creates a replacement immediately, even while the old pod is still terminating.
Rollout control
kubectl rollout has six subcommands for a Deployment:
| Command | What it does |
|---|---|
status | Follows the rollout until it finishes, and exits non-zero if the progress deadline passes |
history | Lists revisions; --revision=N prints that revision’s template |
pause | Sets spec.paused, so template changes accumulate without starting a rollout |
resume | Clears spec.paused; the accumulated changes roll out as one revision |
undo | Restores the previous template, or --to-revision=N |
restart | Replaces every pod through a rolling update |
rollout status compares the Deployment’s status counters and prints whichever condition it is still waiting for: “N out of M new replicas have been updated” while the new ReplicaSet grows, “old replicas are pending termination” while old pods remain, and “N of M updated replicas are available” while new pods soak through minReadySeconds. It returns once all three are satisfied, or with an error when the progress deadline passes or its own --timeout runs out.
The CHANGE-CAUSE column comes from the kubernetes.io/change-cause annotation, which the controller copies from the Deployment to the revision’s ReplicaSet. Set it with kubectl annotate or in your manifests; the kubectl --record flag that also set it is deprecated.
kubectl apply -f web.yaml
deployment.apps/web created
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl rollout pause deployment/web
deployment.apps/web paused
kubectl set image deployment/web web=ghcr.io/kx-labs/web:1.1
deployment.apps/web image updated
kubectl set env deployment/web LOG_LEVEL=debug
deployment.apps/web env updated
kubectl get rs
NAME DESIRED CURRENT READY AGE web-78c7dc78cb 4 4 4 15s
kubectl annotate deployment/web kubernetes.io/change-cause='web 1.1 with debug logging'
deployment.apps/web annotated
kubectl rollout resume deployment/web
deployment.apps/web resumed
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "web" rollout to finish: 3 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl rollout history deployment/web
deployment.apps/web REVISION CHANGE-CAUSE 1 <none> 2 web 1.1 with debug logging
kubectl rollout undo deployment/web --to-revision=1
deployment.apps/web rolled back
kubectl rollout history deployment/web
deployment.apps/web REVISION CHANGE-CAUSE 2 web 1.1 with debug logging 3 <none>
Undo restored the revision 1 template, which became revision 3, and took the change cause of revision 1, which had none. Undo only touches the template: replicas and strategy stay as they are.
Undo works only while the old ReplicaSet still exists. revisionHistoryLimit (default 10) is how many old ReplicaSets the controller keeps, scaled to zero; setting it to 0 deletes them all and leaves nothing to roll back to. You cannot undo a paused Deployment until you resume it.
Progress and failure
The Deployment controller reports its state in conditions. Available says whether the minimum number of replicas is available. Progressing is True with reason NewReplicaSetCreated, FoundNewReplicaSet or ReplicaSetUpdated while a rollout moves, and NewReplicaSetAvailable once it has finished. A rollout makes progress whenever a ReplicaSet scales or a new pod becomes available.
progressDeadlineSeconds (default 600) is how long the controller waits without any progress. When it passes, Progressing turns False with reason ProgressDeadlineExceeded. It turns False early, with reason ReplicaSetCreateError, only when the controller cannot create the new ReplicaSet object itself. Pods that the new ReplicaSet fails to create, for example because a ResourceQuota refuses them, add a separate ReplicaFailure condition with reason FailedCreate, and Progressing still waits for the deadline. That is all Kubernetes does: it does not roll back, delete the new ReplicaSet or pause anything, and the old pods keep serving within the maxUnavailable budget. A paused Deployment is not checked against the deadline.
kubectl apply -f web.yaml
deployment.apps/web created
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... deployment "web" successfully rolled out
kubectl set image deployment/web web=ghcr.io/kx-labs/web:1.10
deployment.apps/web image updated
kubectl get rs,pods
NAME DESIRED CURRENT READY AGE replicaset.apps/web-5fc4d5465 2 2 0 0s replicaset.apps/web-78c7dc78cb 3 3 3 15s NAME READY STATUS RESTARTS AGE pod/web-5fc4d5465-4zlwf 0/1 Pending 0 0s pod/web-5fc4d5465-7lq9v 0/1 Pending 0 0s pod/web-78c7dc78cb-crwtn 1/1 Terminating 0 15s pod/web-78c7dc78cb-dq87d 1/1 Running 0 15s pod/web-78c7dc78cb-nshlv 1/1 Running 0 15s pod/web-78c7dc78cb-pggpj 1/1 Running 0 15s
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 2 out of 4 new replicas have been updated... error: deployment "web" exceeded its progress deadline
kubectl get deployment web -o jsonpath='{.status.conditions[?(@.type=="Progressing")].reason}'
ProgressDeadlineExceeded
kubectl rollout undo deployment/web
deployment.apps/web rolled back
kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 3 of 4 updated replicas are available... deployment "web" successfully rolled out
Read the stall from the top down. kubectl rollout status exits non-zero, which is what a CI job should gate on. The Deployment’s Progressing condition names the reason. kubectl get rs shows which ReplicaSet is stuck and how far the old one has shrunk, and kubectl describe on one of the new pods shows the kubelet’s own event, here a failed pull of a tag that does not exist. The bad-tag incident linked from this lesson replays the same failure with a pager attached.
Readiness sets the pace
Each step of a rolling update waits for new pods to become availableA pod that has been Ready for at least minReadySeconds without any container crashing. Deployments count available pods, not just running ones, against maxUnavailable.. Two settings decide when that happens.
The readiness probe decides when a pod is Ready. Without one, a pod is Ready as soon as its containers have started, so a release that starts and then fails to serve can replace every old pod before anyone notices. With a probe that checks the wrong thing, the opposite happens: the new pods run but never turn Ready, the rollout makes no progress, and a Service never sends them traffic. The readiness incident linked from this lesson is that failure.
minReadySeconds adds a soak time. A new pod must stay Ready for that many seconds before it counts as available, which catches a release that crashes a few seconds after it starts. progressDeadlineSeconds must be greater than minReadySeconds, and the API rejects a Deployment where it is not.
kubectl --context kind-kx create deployment demo --image=nginx:1.30 --replicas=4kubectl --context kind-kx get rs,pods -l app=demo --show-labelskubectl --context kind-kx set image deployment/demo nginx=nginx:1.31kubectl --context kind-kx rollout status deployment/demokubectl --context kind-kx events --for deployment/demokubectl --context kind-kx rollout undo deployment/demokubectl --context kind-kx delete deployment demo
For your own kind cluster kx: copy a command and paste it into your terminal. Run in the simulator runs the same command in this page's simulated cluster instead; nothing on this page touches your machine. With four replicas the defaults allow one pod over and one pod under, so the first events scale the new ReplicaSet to 1 and the old one to 3 together, before any new pod is ready.
Actual state
Check your understanding
Your first answer to each check is the one that counts. After a miss you can answer again; passing every check reconciles the lesson.
Lesson not yet reconciled
Desired: 4 objectives, 0 metReconciled: 4 of 4 objectives met
- Explain how a Deployment manages ReplicaSets and why the pod-template-hash label exists. (not yet met)
- Tune maxSurge and maxUnavailable for capacity and speed. (not yet met)
- Watch, pause, resume and undo a rollout. (not yet met)
- Diagnose a stuck rollout from progressDeadlineSeconds and ReplicaSet events. (not yet met)
Pass every check above to reconcile this lesson.
Every check has passed, so the desired state is met. First-try accuracy: 0 of 5. This lesson's review cards are in your queue.
Practice
Put this lesson to work on the simulated cluster. Everything runs in your browser, with nothing to install.
Changed since the notes
This lesson draws on an earlier set of study notes. Where the documentation contradicts a claim in them, qualifies it or has moved past it, the claim is listed here with the corrected statement and the source it was checked against.
Show 4 corrections
The notes saidWith 10 replicas, maxSurge 25% allows 12-13 pods; maxUnavailable 25% allows 7-8 pods
CorrectedmaxSurge rounds up (2.5 -> 3, so at most 13 Pods) and maxUnavailable rounds down (2.5 -> 2, so at least 8 available). There is no range.
Sourcekubernetes.io/docs/concepts/workloads/controllers/deployment
The notes saidDeployments handle selector changes when you change labels in the pod template
CorrectedIn apps/v1 a Deployment's selector is immutable after creation, and template labels must match it. Changing the selector means deleting and recreating the Deployment (use --cascade=orphan to keep its Pods).
Sourcekubernetes.io/docs/concepts/workloads/controllers/deployment
The notes saidPDBs protect against disruptions like drains or deployments
CorrectedPDBs apply only to the Eviction API. Rolling updates are not limited by PDBs; maxUnavailable governs them.
Sourcekubernetes.io/docs/concepts/workloads/pods/disruptions
The notes saidRollout is strictly add new, wait ready, then remove old
CorrectedWhen maxUnavailable > 0, the controller scales the old ReplicaSet down in the same step as it scales the new one up.
Sourcekubernetes.io/docs/concepts/workloads/controllers/deployment
Every correction across the guide, by note and topic.
Sources
Checked against kubernetes.io and AWS documentation for Kubernetes 1.37 on .
Deployments and rolling updates
↓ ↑ step, 1 to 7 scene, Esc leave