Rolling updates and rollbacks

Ship a new version, and undo a bad one.

Beginner25 min · lesson 10 of 21
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (2 files, 1 KB): rollouts.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/k8s-fund/rollouts.tar.gz && tar -xzf rollouts.tar.gz, which creates ~/lab/rollouts/. SHA-256: 8c52ddae0203cae6798053bdd1fd3bec79e1ebc1019853af5553709045306677

Friday afternoon, a release goes out with a typo in the image tag. With plain Docker you would already have stopped the old container, and now nothing starts in its place. Users see errors until someone finds the last good tag and starts it again by hand.

A Deployment updates differently. Give it a new image and it replaces Pods a few at a time, never taking down more old Pods than its maxUnavailable setting allows. If the new version never comes up, the update stops and the remaining old Pods keep serving. It also keeps earlier versions around, so going back is one command. This lesson ships a good version, then the broken one, then undoes it.

A Deployment with a careful update plan

This Deployment runs four copies of nginx 1.30.5. Two settings under strategy decide how an update moves, and progressDeadlineSeconds decides how long Kubernetes waits before it calls an update stuck:

web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
annotations:
kubernetes.io/change-cause: "first release, nginx 1.30.5"
spec:
replicas: 4
progressDeadlineSeconds: 60 # give up waiting after 60 s without progress (default 600)
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # at most 1 Pod above the 4 asked for
maxUnavailable: 0 # never fewer than 4 ready Pods
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.30.5-alpine
ports:
- containerPort: 80

maxSurge: 1 allows one extra Pod during an update, so five at most. maxUnavailable: 0 says the number of ready Pods may never drop below four. Together they mean: start one new Pod, wait until it is ready, remove one old Pod, repeat. The defaults are 25% for both, which allows a quicker update that may briefly run with fewer Pods. The annotation kubernetes.io/change-cause is a note for people; you will see it again in the history.

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl apply -f web.yaml && kubectl rollout status deployment/web
deployment.apps/web created Waiting for deployment "web" rollout to finish: 0 of 4 updated replicas are available... Waiting for deployment "web" rollout to finish: 1 of 4 updated replicas are available... Waiting for deployment "web" rollout to finish: 3 of 4 updated replicas are available... deployment "web" successfully rolled out
$ kubectl expose deployment web --port 80
service/web exposed
$ kubectl run client --image=busybox:1.38.0 -- sleep 1d && kubectl wait --for=condition=Ready pod/client --timeout=60s
pod/client created pod/client condition met
$ kubectl get deployment web -o wide && kubectl get rs -l app=web
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR web 4/4 4 4 1s nginx nginx:1.30.5-alpine app=web NAME DESIRED CURRENT READY AGE web-788d9b45d8 4 4 4 1s

Four Pods are ready, and one ReplicaSet owns them. kubectl expose gives them a Service called web, like the ones in lesson 8, Services. The client Pod is a small busybox Pod that stays running, so you can send requests to that Service from inside the cluster with kubectl exec.

Roll out a new version

kubectl set image changes the image in the Pod template. Any change to the template starts a rollout. The second command updates the change-cause note to match:

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl set image deployment/web nginx=nginx:1.30.6-alpine kubectl annotate deployment/web kubernetes.io/change-cause="update to nginx 1.30.6" kubectl rollout status deployment/web
deployment.apps/web image updated deployment.apps/web annotated Waiting for deployment "web" rollout to finish: 1 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 1 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 1 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 2 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 3 out of 4 new replicas have been updated... Waiting for deployment "web" rollout to finish: 3 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: 1 old replicas are pending termination... Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination... deployment "web" successfully rolled out

Read the rollout status lines from top to bottom: 1, then 2, then 3 out of 4 new replicas updated, and finally one old replica pending termination. That is the one-at-a-time plan from the strategy. Each line repeats while Kubernetes waits for the new Pod to become ready before it takes away an old one.

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl get rs -l app=web -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR web-768f9fc75f 4 4 4 3s nginx nginx:1.30.6-alpine app=web,pod-template-hash=768f9fc75f web-788d9b45d8 0 0 0 4s nginx nginx:1.30.5-alpine app=web,pod-template-hash=788d9b45d8
$ kubectl exec client -- wget -qS -O /dev/null http://web 2>&1 | grep -i server
Server: nginx/1.30.6

There are now two ReplicaSets. The new one runs nginx 1.30.6 with four Pods. The old one is still there with DESIRED 0. Kubernetes keeps it on purpose: it is the record of the old version, and the way back. The Server header from inside the cluster confirms the new version answers.

A broken version

Now ship a tag that does not exist. This is what a typo in a release pipeline looks like:

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl set image deployment/web nginx=nginx:1.30.99-alpine kubectl annotate deployment/web kubernetes.io/change-cause="update to nginx 1.30.99"
deployment.apps/web image updated deployment.apps/web annotated
$ kubectl rollout status deployment/web
Waiting for deployment "web" rollout to finish: 1 out of 4 new replicas have been updated... error: deployment "web" exceeded its progress deadline

rollout status waited at "1 out of 4 new replicas have been updated" and then gave up with exceeded its progress deadline and exit code 1. That took 60 seconds because of progressDeadlineSeconds: 60; the default is 600. A release pipeline that runs kubectl rollout status after a deploy fails at this point and can alert someone.

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl get deployment web && kubectl get pods -l app=web
NAME READY UP-TO-DATE AVAILABLE AGE web 4/4 1 4 65s NAME READY STATUS RESTARTS AGE web-5bff485d6c-8qjd4 0/1 ImagePullBackOff 0 61s web-768f9fc75f-2sn8n 1/1 Running 0 63s web-768f9fc75f-5h2fr 1/1 Running 0 62s web-768f9fc75f-j4zsg 1/1 Running 0 63s web-768f9fc75f-mb4rc 1/1 Running 0 64s
$ kubectl get pods -l app=web -o custom-columns=NAME:.metadata.name,IMAGE:.spec.containers[0].image,WAITING:.status.containerStatuses[0].state.waiting.reason
NAME IMAGE WAITING web-5bff485d6c-8qjd4 nginx:1.30.99-alpine ImagePullBackOff web-768f9fc75f-2sn8n nginx:1.30.6-alpine <none> web-768f9fc75f-5h2fr nginx:1.30.6-alpine <none> web-768f9fc75f-j4zsg nginx:1.30.6-alpine <none> web-768f9fc75f-mb4rc nginx:1.30.6-alpine <none>

Here is why nothing broke for users. The Deployment still shows READY 4/4 and AVAILABLE 4, with only one Pod UP-TO-DATE. Four old Pods are Running. The one new Pod is stuck in ImagePullBackOff, because there is no such image to pull. With maxUnavailable: 0 Kubernetes may not remove an old Pod until a new one is ready, and the new one never will be, so the rollout stops right there.

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl get deployment web -o jsonpath='{range .status.conditions[*]}{.type}{" "}{.status}{" "}{.reason}{"\n"}{end}'
Available True MinimumReplicasAvailable Progressing False ProgressDeadlineExceeded
$ kubectl exec client -- sh -c 'for i in 1 2 3 4 5; do wget -qS -O /dev/null http://web 2>&1 | grep Server; done'
Server: nginx/1.30.6 Server: nginx/1.30.6 Server: nginx/1.30.6 Server: nginx/1.30.6 Server: nginx/1.30.6

The Deployment's conditions tell the same story in a form tools can read: Available True, but Progressing False with reason ProgressDeadlineExceeded. Kubernetes does not undo anything by itself when this happens. It only marks the rollout as stuck. Meanwhile every request was answered by nginx 1.30.6. The broken Pod is not ready, so the Service sends it no traffic.

History and undo

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl rollout history deployment/web
deployment.apps/web REVISION CHANGE-CAUSE 1 first release, nginx 1.30.5 2 update to nginx 1.30.6 3 update to nginx 1.30.99

Each revision is one ReplicaSet, and the CHANGE-CAUSE column comes from the annotation. Older guides use kubectl ... --record to fill this column. That flag is deprecated; set the annotation yourself, as above. rollout undo goes back to the previous revision:

ubuntu@secopslog-k8s:~/lab/rollouts · Kubernetes 1.37
$ kubectl rollout undo deployment/web && kubectl rollout status deployment/web
Warning: resource deployments/web was previously managed with 'kubectl apply'. Rolling back will not update the kubectl.kubernetes.io/last-applied-configuration annotation, which may cause unexpected behavior on future 'kubectl apply' operations. Consider using 'kubectl apply' with your previous configuration file instead. deployment.apps/web rolled back deployment "web" successfully rolled out
$ kubectl get pods -l app=web && kubectl rollout history deployment/web
NAME READY STATUS RESTARTS AGE web-5bff485d6c-8qjd4 0/1 Terminating 0 62s web-768f9fc75f-2sn8n 1/1 Running 0 64s web-768f9fc75f-5h2fr 1/1 Running 0 63s web-768f9fc75f-j4zsg 1/1 Running 0 64s web-768f9fc75f-mb4rc 1/1 Running 0 65s deployment.apps/web REVISION CHANGE-CAUSE 1 first release, nginx 1.30.5 3 update to nginx 1.30.99 4 update to nginx 1.30.6
$ kubectl get deployment web -o jsonpath='{range .status.conditions[*]}{.type}{" "}{.status}{" "}{.reason}{"\n"}{end}'
Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable

The warning matters. The Deployment was created with kubectl apply, and undo changes it behind the back of your YAML file, so the next apply of an old file would undo the undo. In a team, the lasting fix is to change the file and apply it again; rollout undo is the quick way out during an incident.

The undo itself was fast: the 1.30.6 Pods were still running, so Kubernetes only had to remove the broken Pod, still shown as Terminating. Look at the history again. Revision 2 is gone and revision 4 has appeared with the 1.30.6 note. An undo does not jump back in the list; it copies the old template forward as a new revision. The conditions are back to Progressing True with NewReplicaSetAvailable.

Hands-on

1. Go back to the very first version: kubectl rollout undo deployment/web --to-revision=1.

2. Follow the rollout: kubectl rollout status deployment/web.

3. Print the image the Deployment now runs: kubectl get deployment web -o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'.

4. List the revisions: kubectl rollout history deployment/web.

5. Clean up with kubectl delete deployment web, kubectl delete service web and kubectl delete pod client --now (--now skips the usual 30 second grace period). Wait for the web Pods to go with kubectl wait --for=delete pod -l app=web --timeout=90s, which returns once they no longer exist, then run kubectl get all.

What you should see: step 1 prints the same last-applied-configuration warning as before and rolled back. Step 2 counts through all four Pods again, because the 1.30.5 ReplicaSet had zero Pods and had to start them one by one. Step 3 prints nginx:1.30.5-alpine. Step 4 lists revisions 3, 4 and 5, and 5 carries the note "first release, nginx 1.30.5". After step 5, kubectl get all lists only service/kubernetes, the API server's own Service.

Remember

With maxUnavailable: 0, a broken version stalls the rollout with ProgressDeadlineExceeded while the old Pods keep serving. kubectl rollout undo brings an earlier version back as a new revision.

Quick check
01A rollout uses maxSurge: 1 and maxUnavailable: 0 with 4 replicas. The new image cannot be pulled. What do users notice?
Incorrect — The broken Pod was never ready, so the Service sent it nothing.
Correct — With maxUnavailable 0 no old Pod is removed before a new one is ready. Every request got nginx 1.30.6.
Incorrect — No old Pod was taken away, because no new Pod ever became ready.
Incorrect — Kubernetes only marks the rollout ProgressDeadlineExceeded. Undoing it is up to you.
02After kubectl rollout undo, the history went from 1, 2, 3 to 1, 3, 4. Why?
Incorrect — Revision 2 was the good 1.30.6 version; 3 was the broken one.
Incorrect — Revision 3, the newest, is still in the list after the undo.
Incorrect — Nothing was renumbered: 1 and 3 kept their numbers.
Correct — An undo reuses the old template as a new revision, with its change-cause note.
03How do you fill the CHANGE-CAUSE column of kubectl rollout history on Kubernetes 1.37?
Correct — The column shows this annotation. The lab set it in the YAML and with kubectl annotate.
Incorrect — --record is deprecated, and recent guides set the annotation instead.
Incorrect — There is no such flag. The column only reads the annotation.
Incorrect — There is no such field in the Deployment API.

Related