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.
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.01sWithout 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:
- The kubelet runs the
preStophook, then sends SIGTERM to the container. - 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.
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.
The app does its part on SIGTERM: it fails readiness, stops accepting new connections, finishes in-flight requests, then exits.
NGINX Gateway Fabric applies endpoint changes by reloading NGINX: 61
reloads during these rollouts, according to the gateway pod's log. That's
the same idle-keepalive race measured in the Nginx module's
process model lesson, and the
source of the single RemoteProtocolError and ReadError in two of the
runs. Clients must retry idempotent requests on a closed connection.
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 = TrueAn undo is just another rollout, to an older template, with the same surge, readiness and preStop behaviour.
Revision numbers aren't stable: an undo moves the old revision to a new,
higher number. The first version of the lab hardcoded --to-revision=2
and rolled back to the wrong template on its second run. It now picks
the revision by inspecting the ReplicaSets' templates.
Lab
- Run
rollouts.pytwice. Then setmaxSurge: 0, maxUnavailable: 1and run it again. What changes in capacity during the rollout? - Deploy an image tag that doesn't exist. What happens to the rollout and to traffic? Undo it.
- Make the app ignore SIGTERM (comment out the handler) and roll. How long does each old pod take to go away, and why?