Learn Labs
Running Applications

Rolling Updates

Surge and unavailability, graceful termination, preStop, and rollback

A rolling update replaces pods a few at a time: a new ReplicaSet scales up while the old one scales down. Kubernetes promises the pod counts. Whether clients notice depends on details this lesson measures.

strategy:type: RollingUpdaterollingUpdate:maxSurge: 1at most 1 pod above replicas during the rolloutmaxUnavailable: 0never fewer than replicas READY pods

With 3 replicas: start a 4th (new) pod, wait until it's ready, remove an old one, repeat. Readiness gates every step, so a new version that never becomes ready stops the rollout with the old pods still serving, instead of replacing everything with broken pods.

Measured: with and without a preStop sleep

rollouts.py sends 20 requests per second through the Gateway while it restarts the Deployment, with the configured preStop sleep and then without it. Four runs of each:

./scripts/lab.sh rollouts.py
2. rollout WITH preStop sleep 5s: rollout took 18s; 415 requests, 415 ok, errors: none
 versions served: {'v1': 257, 'v2': 158}
3. rollout WITHOUT preStop: rollout took 19s; 202 requests, 199 ok, errors: {'ReadTimeout': 3}, slowest 5.01s
runwith preStoperrors: 0, 1, 0, 1without preStoperrors: 2, 3, 3, 2run 1✓✗run 2!✗run 3✓✗run 4!✗

Without the sleep, 2 to 3 requests per rollout hung until the client's 5-second timeout. With it, 0 or 1 requests failed, and never by hanging (the occasional one was a reset connection, explained below).

Why: termination is a race

When a pod is deleted, two things start at the same time:

  1. The kubelet runs the preStop hook, then sends SIGTERM to the container.
  2. The endpoints controller removes the pod from the Service's EndpointSlices, and every consumer of those (kube-proxy on every node, the Gateway's NGINX) updates on its own schedule.
proxies updated5 sendpointspod listedremovedgateway NGINXold upstreamsreloadedno preStopTERMgone: requests hangpreStop 5 sstill servingTERMseconds after the pod is marked for deletion →

Without a delay, the app can exit before the proxies stop sending it requests. Those requests go to a pod that's gone. A preStop sleep keeps the old pod serving for a few seconds, long enough for every proxy to drop it, and only then does SIGTERM arrive.

terminationGracePeriodSeconds: 30preStop + shutdown must fit in this, then SIGKILLlifecycle:preStop:sleep: { seconds: 5 }built-in sleep action: no shell needed in the image

The app does its part on SIGTERM: it fails readiness, stops accepting new connections, finishes in-flight requests, then exits.

Config changes don't roll anything

1. ConfigMap VERSION=v2, 5 s later, pods still serve: {'v1': 88}

Only changes to the pod template start a rollout. See Configuration.

Rollback

Every template version is a ReplicaSet the Deployment keeps (revisionHistoryLimit: 5):

4. revisions (ReplicaSets): [(3, 'no preStop'), (4, 'no preStop'), (5, 'preStop'), (6, 'preStop'), (7, 'no preStop'), (8, 'no preStop')]
 rollout undo --to-revision=6: preStop back = True
kubectl -n shop rollout history deploy/webkubectl -n shop rollout undo deploy/webto the previous revisionkubectl -n shop rollout undo deploy/web --to-revision=6to a specific onekubectl -n shop rollout pause deploy/webstop mid-rollout

An undo is just another rollout, to an older template, with the same surge, readiness and preStop behaviour.

Lab

  1. Run rollouts.py twice. Then set maxSurge: 0, maxUnavailable: 1 and run it again. What changes in capacity during the rollout?
  2. Deploy an image tag that doesn't exist. What happens to the rollout and to traffic? Undo it.
  3. Make the app ignore SIGTERM (comment out the handler) and roll. How long does each old pod take to go away, and why?

On this page