Pod termination and graceful shutdown
What happens between kubectl delete and a Pod disappearing: the grace period, preStop, SIGTERM, the endpoint race, and how to lose no requests on a rollout.
Liveness, readiness and startup probesEndpointSlices and kube-proxy
Pod termination and graceful shutdown
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Deleting a Pod only marks it: the API server sets deletionTimestamp and deletionGracePeriodSeconds, and the Pod stays Terminating until the kubelet confirms its containers have stopped.
- The kubelet runs the preStop hook, then sends the stop signal (SIGTERM unless the image says otherwise), then SIGKILLs whatever is left when terminationGracePeriodSeconds (default 30) runs out. preStop time counts against that budget.
- Endpoint removal happens in parallel, not first. The EndpointSlice marks the Pod terminating at once, but kube-proxy, ingress controllers and cloud load balancers catch up over seconds, so traffic can still arrive after SIGTERM.
- A short preStop sleep (the native sleep action needs no shell in the image) keeps the app serving while the rest of the cluster stops sending it work.
- The app must handle SIGTERM as PID 1: stop accepting new work, finish in-flight requests, close connections and exit before the grace period ends.
- Force deletion with --grace-period=0 --force removes the API object without waiting for the node. The process may still be running, which for a StatefulSet can mean two Pods with the same identity.
The requests that die on every deploy
A rolling update replaces Pods one at a time. Each old Pod has to stop, and if it stops carelessly the requests it was handling die with it. Teams usually discover this as a small, regular spike of 502s on every deploy, which nobody can reproduce by hand.
Stopping a Pod well is a coordination problem across several components that don’t talk to each other directly: the API server, the kubelet on the Pod’s node, the EndpointSlice controller, kube-proxy on every node, and whatever load balancer sits in front. Each one learns about the deletion on its own schedule. Graceful shutdown is about bridging those timing gaps.
Deletion is a mark, not a removal
When you run kubectl delete pod, the API server doesn’t remove the object. It sets two fields and leaves the Pod in place. On a local API server whose fake node has no kubelet:
$ kubectl delete pod web --wait=false
pod "web" deleted from default namespace
$ kubectl get pod web -o jsonpath='{.metadata.deletionTimestamp} {.metadata.deletionGracePeriodSeconds}'
2026-11-28T17:49:11Z 45
$ kubectl get pod web
NAME READY STATUS RESTARTS AGE
web 1/1 Terminating 0 13s
deletionGracePeriodSeconds came from the Pod’s terminationGracePeriodSeconds: 45. The Pod was still Terminating 75 seconds later, long after its grace period, because the only thing that finishes a deletion is the node’s kubelet reporting the containers stopped and then deleting the object with a zero grace period. With no kubelet, nothing ever does. A real cluster shows the same thing when a node dies: Pods on it hang in Terminating.
What the kubelet does with the grace period
When the kubelet sees the deletion timestamp, it stops each container in a fixed order (from killContainer in pkg/kubelet/kuberuntime):
- Run the container’s preStop hook, if any, and wait for it.
- Send the stop signal,
SIGTERMunless the image sets a differentSTOPSIGNAL. - Wait for the process to exit, up to whatever remains of the grace period.
- Send
SIGKILL.
The preStop time comes out of the same budget: the code subtracts the hook’s duration from the grace period, then raises the remainder to a minimum of 2 seconds. With the default 30 seconds and a 25-second preStop, your app gets about 5 seconds after SIGTERM. Native sidecar containers are stopped after the main containers, so a logging or proxy sidecar outlives the app it serves.
The signal goes to PID 1 in the container. If PID 1 is a shell that doesn’t forward signals, the app never sees SIGTERM and simply gets killed at the end of the grace period. The docker lesson on CMD, ENTRYPOINT and PID 1 covers why exec form matters.
The endpoint race
At the same moment the kubelet starts stopping containers, the EndpointSlice controller sees the deletion too. Here is the same web Pod’s endpoint just before and just after the delete, with a Service selecting it:
before: ["10.244.1.7"] {"ready":true,"serving":true,"terminating":false}
after: ["10.244.1.7"] {"ready":false,"serving":true,"terminating":true}
ready drops to false immediately, and terminating says why. serving still follows the readiness probe, which the kubelet keeps running during termination, as the probes lesson showed.
The catch is what happens next. kube-proxy on every node has to watch that change and rewrite its rules. An ingress controller has to reload its upstreams. A cloud load balancer has to deregister the target. Each takes from milliseconds to tens of seconds, and none of it is coordinated with the kubelet. If the app exits the instant SIGTERM arrives, requests still routed to it during those seconds fail.
Bridging the gap with preStop
The standard fix is to keep serving for a few seconds before shutting down. That used to need sleep in the image. Kubernetes now has a native sleep action, run by the kubelet itself, so it works in distroless images:
spec:
terminationGracePeriodSeconds: 45
containers:
- name: web
image: nginx:1.29
lifecycle:
preStop:
sleep: {seconds: 10}
The timeline becomes: deletion → endpoints marked terminating and the app keeps serving for 10 seconds while proxies catch up → SIGTERM → the app drains in-flight work → exit, with 35 seconds of headroom before SIGKILL. Pick the sleep from how slow your slowest traffic path is to notice. Behind a cloud load balancer with long deregistration delays it may need to be longer, and the grace period with it.
An httpGet or exec preStop can also tell the app to start draining, for servers like nginx that want a specific command for a graceful stop.
What the app must do on SIGTERM
The platform can only buy time. The app has to use it:
- Stop accepting new connections, or start failing readiness, but keep serving the ones it has.
- Finish in-flight requests and background jobs, or hand them back to a queue.
- Close keep-alive connections cleanly so clients reconnect elsewhere instead of hitting a dead socket.
- Exit with code 0 before the grace period ends.
Most web frameworks have a graceful-shutdown setting that does the first three. It is often off by default.
Force deletion and its cost
kubectl delete pod web --force --grace-period=0 removes the object at once and prints a warning that the resource “may continue to run on the cluster indefinitely”. That is literal. For a Deployment it only means an extra copy for a while. For a StatefulSet, whose Pods have stable identities and often exclusive volumes, it can mean two processes believing they are db-0. Force-delete only when you know the node is gone.
About the video
The embed is TheCodeAlchemist’s “Kubernetes Pod Lifecycle: How To Actually Enable Graceful Shutdown”, July 2026, 16 minutes. Its chapters cover the problem, the Pod termination lifecycle step by step, and a live demo sending requests during a rolling update. The examples use Spring Boot, so the app-side settings are Spring’s, but the Kubernetes sequence is the same for any app. I judged it from chapters and description only, since transcripts couldn’t be fetched. English was judged from its “English (US) original” audio track. Alex Pliutau’s ContainerDays talk in deeper covers the same ground from a Go service’s point of view.
What was run: the deletion fields, the Pod stuck in Terminating, the force-deletion warning, and the EndpointSlice conditions before and after deletion are real output from a local Kubernetes v1.37.3 API server whose nodes run no containers. The Pod was marked Running and Ready by patching its status by hand, standing in for a kubelet. The kubelet’s stop order, the preStop subtraction and the 2-second minimum were read from kuberuntime_container.go and kuberuntime_manager.go in the v1.37.3 source. Signal delivery, sidecar ordering and propagation delays are from the Pod lifecycle docs, checked 2026-11-28.
Where this fits
Pods now start and stop cleanly, so it is safe to add and remove them automatically. Next: the Horizontal Pod Autoscaler.
Check yourself
A Deployment's Pods return a burst of 502s on every rollout, though the app handles SIGTERM correctly. What is the likely cause, and the usual fix?
The app shuts down as soon as SIGTERM arrives, while kube-proxy rules and the load balancer still point at it for a few seconds. Add a preStop sleep of 5 to 15 seconds so it keeps serving until endpoint removal has propagated, and keep terminationGracePeriodSeconds above sleep plus drain time.
terminationGracePeriodSeconds is 30 and the preStop hook runs for 25 seconds. How long does the app get after SIGTERM?
About 5 seconds. The preStop time is taken out of the same budget, and SIGKILL comes when the remainder runs out (the kubelet guarantees at least 2 seconds).
You kubectl delete a Pod whose node has lost power. Why does it sit in Terminating, and why is force deletion risky?
Only that node's kubelet can confirm the containers stopped, and it is unreachable, so the object waits. Force deletion removes the object without that confirmation. If the node was only partitioned, the process may still be running and writing while a replacement starts.
Why does a Pod being deleted show ready: false but serving: true in its EndpointSlice?
ready is false for any terminating endpoint, so normal routing stops picking it. serving still reflects the readiness probe, which the kubelet keeps running during termination, so proxies can fall back to serving-but-terminating endpoints when nothing else is left.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.