Rolling updates and rollbacks
Ship a new version, and undo a bad one.
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: 8c52ddae0203cae6798053bdd1fd3bec79e1ebc1019853af5553709045306677Friday 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:
apiVersion: apps/v1kind: Deploymentmetadata:name: webannotations:kubernetes.io/change-cause: "first release, nginx 1.30.5"spec:replicas: 4progressDeadlineSeconds: 60 # give up waiting after 60 s without progress (default 600)strategy:type: RollingUpdaterollingUpdate:maxSurge: 1 # at most 1 Pod above the 4 asked formaxUnavailable: 0 # never fewer than 4 ready Podsselector:matchLabels:app: webtemplate:metadata:labels:app: webspec:containers:- name: nginximage: nginx:1.30.5-alpineports:- 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.
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:
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.
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:
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.
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.
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
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:
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.
maxSurge: 1 and maxUnavailable: 0 with 4 replicas. The new image cannot be pulled. What do users notice?kubectl rollout undo, the history went from 1, 2, 3 to 1, 3, 4. Why?kubectl rollout history on Kubernetes 1.37?kubectl annotate.--record is deprecated, and recent guides set the annotation instead.