Course contents · Lesson 19 of 22

Kubernetes Rolling Updates and Rollbacks, Hands-On

Hands-On with kind · Lesson 19 of 22 · 12 min Hands-on

Your team has a fix ready while users are on the site. Restart all three pods at once and everyone gets errors while the new version loads; ship a broken image and the errors never stop. Kubernetes rolling updates avoid both. This lesson shows how, on your kind cluster from lesson 17.

How a rolling update swaps pods

As you saw in lesson 10, changing a Deployment's pod template creates a new ReplicaSet that scales up while the old one scales down. Each template change is a new revision, a numbered version of the Deployment; scaling is not. A pod is available once it is ready (and has stayed ready for minReadySeconds, 0 by default). The default strategy, RollingUpdate, has two limits:

  • maxSurge: how many pods may exist above replicas during the update. Default 25 percent, rounded up.
  • maxUnavailable: how many of the replicas may be unavailable. Default 25 percent, rounded down.

Each takes a number or a percentage. For 3 replicas, 25 percent is 0.75 pods: surge rounds up to 1 and unavailable rounds down to 0. So Kubernetes may run 4 pods but must keep 3 available, which means it adds a new pod before it removes an old one. The other strategy, Recreate, stops all old pods first, leaving a gap.

In the snapshots, follow the green lines. A Service's endpoints, the pod addresses it sends traffic to (lesson 11), include only ready pods, so a starting pod gets nothing.

Four snapshots of a Deployment with replicas: 3, maxSurge 1 and maxUnavailable 0, updating from nginx:1.26 to nginx:1.27. 1, start: 3 old pods, all ready and connected to the Service web. 2, surge: the same 3 old pods plus 1 new pod that is starting and has no connection to the Service. 3, halfway: 2 old ready pods, 1 new ready pod and 1 new pod starting; the 3 ready pods are connected. 4, done: 3 new ready pods. Every snapshot says 3 pods get traffic; moments in between are skipped
However far the update has got, three ready pods are behind the Service.

Trigger an update and watch it

The lab folder course-labs/containers-and-kubernetes-fundamentals/03-rolling-updates holds lesson 18's Service and a Deployment that starts at nginx:1.26, with a readiness probe and the defaults written out so you can see where they go. Clean up lab 02 first if it is still running. Here is deployment.yaml, trimmed:

spec:
  replicas: 3
  revisionHistoryLimit: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  ...
      containers:
        - name: web
          image: nginx:1.26
          readinessProbe:
            httpGet:
              path: /
              port: 80
            periodSeconds: 5

The selector, labels and container port, left out here, are as in lesson 18. From the lab folder, with kubectl pointed at kind-lab, apply both files and list the pods:

kubectl apply -f deployment.yaml -f service.yaml
kubectl get pods

You should see deployment.apps/web created and service/web created, then, within a minute, three web- pods at 1/1 and Running. That is revision 1. ImagePullBackOff here means the node cannot download the image, so check your connection. Now give the container named web a new image and follow the rollout:

kubectl set image deployment/web web=nginx:1.27
kubectl rollout status deployment/web

Before the equals sign comes the container's name from the template, and after it the new image. rollout status waits and prints progress until the rollout ends. You should see something like this; which waiting lines appear depends on timing:

deployment.apps/web image updated
Waiting for deployment "web" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
deployment "web" successfully rolled out

The last line means all three pods run the new template and are available: revision 2. kubectl get rs now lists two ReplicaSets, the new one with 3 pods and the old one with 0. A misspelt container name gives unable to find container named and changes nothing. In real projects you change the image in the file and apply it, so file and cluster agree.

Readiness probes decide when a new pod counts

Every step of a rollout trusts "ready". As lesson 15 showed, a container without a readiness probe is ready as soon as it runs, even if the app inside only returns errors. A rollout would then swap working pods for broken ones.

The lab's probe asks nginx for / on port 80 every 5 seconds. Until it succeeds, the new pod gets no traffic from the Service and does not count as available, so no old pod goes. A version that starts but fails its probe stalls the rollout instead of taking all the traffic.

In the flowchart, notice the "no" branch: Kubernetes never removes old pods to make room. After progressDeadlineSeconds, 600 seconds by default, it only reports that the rollout is stuck. Undoing it is your decision.

Flowchart. The pod template changes, making a new revision and a new ReplicaSet. Start one new pod, as maxSurge allows 1 extra. Is the new pod ready, meaning its readiness probe passes? Yes: remove one old pod, so 3 pods stay available, then ask whether all 3 pods are new; no loops back to start the next new pod, yes ends at successfully rolled out. No: the rollout stalls and old pods keep serving; after 600 seconds the stall is reported, never undone. Then your call: undo? Yes: kubectl rollout undo, and the old ReplicaSet scales up. No: fix the image instead, which starts a new rollout
A pod that never becomes ready stops the rollout, not the app.

Break it on purpose: a tag that does not exist

Now roll out a typo. No tag nginx:1.277 exists, so the node cannot pull the image, the container never runs, and the new pod can never become ready. This is revision 3:

kubectl set image deployment/web web=nginx:1.277
kubectl rollout status deployment/web

You should see Waiting for deployment "web" rollout to finish: 1 out of 3 new replicas have been updated... and then nothing more. Press Ctrl+C, which stops the watching but not the rollout; left alone, it would end after 600 seconds with error: deployment "web" exceeded its progress deadline. Now kubectl get pods shows this, with your own names:

NAME                   READY   STATUS             RESTARTS   AGE
web-5d8c6b7f4b-4xq9z   1/1     Running            0          4m
web-5d8c6b7f4b-h7m2k   1/1     Running            0          4m
web-5d8c6b7f4b-tw8pd   1/1     Running            0          4m
web-6c7d9f8b5x-zx2lm   0/1     ImagePullBackOff   0          50s

The three nginx:1.27 pods are still ready, next to the one extra pod that maxSurge allows. That pod switches between ErrImagePull and ImagePullBackOff, and kubectl describe pod shows a Failed to pull image event for it. With maxUnavailable at 0, no old pod may go. Check that users still get answers by calling the Service from a throwaway pod, as in lesson 11:

kubectl run client --rm -i --restart=Never --image=busybox:1.36 -- wget -qO- http://web

You should see the HTML of the nginx welcome page, served by an old pod, then a line saying the pod client was deleted; bad address 'web' means the Service is missing. kubectl get deploy web agrees: READY 3/3, UP-TO-DATE 1, AVAILABLE 3.

Undo: rollout history and rollout undo

Each revision lives on as a ReplicaSet that keeps its pod template, even at 0 pods. kubectl rollout history deployment/web lists them, oldest first. You should see:

deployment.apps/web
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>

CHANGE-CAUSE is copied from the annotation kubernetes.io/change-cause, which nobody set here. Adding --revision=3 prints that revision's pod template, image included. Now undo to the previous revision and check the history again:

kubectl rollout undo deployment/web
kubectl rollout history deployment/web

The undo prints deployment.apps/web rolled back and copies revision 2's template back. The nginx:1.27 ReplicaSet, still running its three pods, becomes current again, and the broken pod disappears. The history now lists 1, 3 and 4: an undo is a new rollout, so revision 2 returns as revision 4. To pick a particular one, add --to-revision=1.

How far back you can go depends on revisionHistoryLimit, the number of old ReplicaSets kept: 10 by default, while 0 leaves nothing to undo to. In the timeline, available pods never drop below 3 in either story.

Timeline in two panels over seven illustrative steps, t0 to t6. Good update, nginx:1.26 to nginx:1.27: the nginx:1.26 ReplicaSet goes 3, 3, 2, 2, 1, 1, 0 and the nginx:1.27 ReplicaSet 0, 1, 1, 2, 2, 3, 3, each new pod starting before it is ready; available stays 3, and the nginx:1.26 ReplicaSet is kept at 0 pods for a rollback. Failed update, then undo, nginx:1.27 to nginx:1.277: the nginx:1.27 ReplicaSet stays at 3 while the nginx:1.277 ReplicaSet holds 1 pod that is never ready from t1 to t4, so the rollout stalls; after kubectl rollout undo it drops to 0, and the nginx:1.27 ReplicaSet, still at 3 pods, is revision 4. Available stays 3 throughout
A failed update never touches the serving ReplicaSet, so the undo has nothing to rebuild.

Check yourself

A Deployment has 10 replicas and the default strategy. During a rollout, how many pods can exist at most, and how many must stay available?

13 and 8. A quarter of 10 is 2.5, which the two limits always round in opposite directions: maxSurge up to 3 extra pods, maxUnavailable down to 2 missing ones.

A new version starts but answers every request with an HTTP 500 error, and there is no readiness probe. What happens, and what would a probe on / change?

With no probe, each new pod is ready once its container runs, so the rollout completes and all traffic reaches the broken version. A probe on / would fail, so the rollout would stall with the old pods serving.

Revisions 1 (nginx:1.26), 2 (nginx:1.27) and 3 (nginx:1.277) exist, and you run kubectl rollout undo twice. Which image runs afterwards?

nginx:1.277, the broken one. Undo goes to the previous revision: the first brings revision 2 back as revision 4, so the previous one is then revision 3. Name the version you want with --to-revision.

Go deeper