App
Graceful shutdown
SIGTERM and the endpoint race
preStop, SIGTERM, the 30-second grace period, and why requests can still arrive after shutdown starts. Deleting a Pod starts two clocks at once - the control plane removing it from the Service, and the kubelet stopping the container - and they are not synchronized. The whole craft of graceful shutdown is buying overlap between them. The times here belong to one example run; Kubernetes guarantees none of the propagation times.
- Pod
- web-…-p8m4z · serving 12 rpm when deleted
- Grace
- terminationGracePeriodSeconds 30
- PreStop
- sleep 5 · this example's choice
- The race
- endpoint removal vs iptables · no fixed bound
- Timings
- one example run, not limits
Read the whole lesson as text
1. Two clocks start
The delete lands and two sequences begin in parallel. Track one, control plane: the endpointslice controller drops 10.244.2.18 from the web Service. Track two, node: the kubelet begins the 30-second countdown to kill the container. Between them sits the actual race - kube-proxy on every node has to notice track one and rewrite its iptables rules before new connections stop landing here. How long that takes depends on three things: the watch delivering the change, kube-proxy's wait between syncs, and the time to program the rules. minSyncPeriod sets only the middle one.
- The Pod is marked not ready in the Service's endpoint list, so it should get no new traffic.
- The kubelet starts the 30-second clock - but the network doesn't know yet.
- In this run node-1's kube-proxy hears within 25 ms, but its rules are not rewritten yet. Other nodes may hear later.
2. The race, lost
First, the version without a preStop hook - the default. The kubelet would send SIGTERM at once and the app would close its listener, while the rules on node-1 still point at it. Here's a request that finds that gap: DNAT'd to 10.244.2.18 by rules that were still live, it arrives at a listener that is already closing - connection refused, a reset mid-request at worst. The fix isn't to prevent this; it's to make it survivable: the client's library retries, this time draws a healthy endpoint, and the user sees a 40-ms wobble, not an error page. Until node-1's rules are rewritten, a retry could draw the dying Pod again.
- Without a hook: one request, routed by rules that are out of date, reaches a Pod that already got SIGTERM.
- The Pod resets the connection - its listener is on the way out.
- This retry draws a healthy endpoint, so in this run the request costs 38 extra milliseconds.
3. PreStop
Now the version this Pod actually has. Back on node-2, the kubelet runs the preStop hook before anything terminal happens: sleep 5. Five seconds of deliberate nothing, while the container keeps serving normally. That gives the change from step 1 time to spread, but it confirms nothing: the kubelet never asks any kube-proxy whether its rules changed. When the timer ends, SIGTERM follows, even if a node is still behind. The sleep also spends the grace period: 30 total = 5 preStop + 25 for SIGTERM and drain.
- Before any signal, the kubelet runs the preStop hook: five seconds for the change to spread.
- The sleep is over. The kubelet moves on without checking whether every node's rules have changed.
4. SIGTERM
The kubelet asks containerd to stop the container, and the runtime sends SIGTERM to PID 1 - the app itself, if you used exec. A well-behaved server does three things: close the listener (no new requests), finish the ones in flight, flush and exit 0. The traps are everywhere: a shell wrapper eats the signal; a naive app exits mid-request; an eager one exits before the rules propagate and re-creates step 2's race single-handedly.
- The kubelet asks containerd to stop the app, politely, with a timeout.
- The app closes its listener, finishes two in-flight requests, and exits cleanly.
5. The deadline
Had the app hung, the story ends differently: the grace period expires and the runtime sends SIGKILL. Uncatchable - open requests die on the wire, half-written files stay half-written, and the exit code is 137. The grace period is a contract: the platform will wait exactly this long, and no longer. Workloads that genuinely need more - big flushes, long uploads - must raise terminationGracePeriodSeconds rather than hope.
- Our app exited 0 at seven seconds; the hammer never came.
- In the timeline where the app hung, this is the 30-second mark: SIGKILL.
Summary
Shutdown is a distributed dance with no conductor: the control plane retires the endpoint, every node's rules converge at their own pace, and the process drains at its own. preStop buys time for those rules to change without confirming that they did, SIGTERM asks the process politely, and the 30-second grace is the exact width of the platform's patience. Together they make step 2's reset unlikely, not impossible, so clients still need to retry.
Next lesson: Autoscaling