Sub topic 12
Probes and graceful shutdown
This chapter assumes you know Deployments, Services, and EndpointSlices, and that you have read the earlier chapters on readiness and liveness probes. It covers what happens to a Pod's traffic when the Pod is deleted, and how to stop requests from failing during that window.
What happens when a Pod is deleted
Deleting a Pod, for example during a rolling update or a scale-down, starts two actions at about the same time:
- The control plane marks the Pod as terminating and removes it from Service endpoints.
- The kubelet runs the container's
preStophook, then sendsSIGTERMto the main process.
These two actions are not coordinated. Endpoint changes must reach every kube-proxy, ingress controller, and load balancer before they stop sending traffic. If your process exits on SIGTERM right away, some requests reach a Pod that is already gone, and clients see connection resets.
Where probes fit
Termination does not wait for a readiness probe to fail. The Pod leaves the endpoints because it is terminating. Probes still matter in two ways:
- A probe that keeps failing in the final seconds does not change the outcome, but a liveness failure during shutdown can trigger a restart attempt. Keep liveness endpoints independent of shutdown state, so a draining process is not reported as dead.
- If the application receives
SIGTERM, it should keep serving in-flight and late-arriving requests. It can make its readiness endpoint return an error so that load balancers that run their own health checks, outside Kubernetes, also stop sending traffic.
Configure the shutdown window
Two settings control the window. terminationGracePeriodSeconds defaults to 30 and is the total time before the kubelet sends SIGKILL. The preStop hook counts against that total.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
terminationGracePeriodSeconds: 45
containers:
- name: web
image: registry.example.com/web:1.0.0
ports:
- containerPort: 8080
lifecycle:
preStop:
exec:
command: ["sleep", "10"]
readinessProbe:
httpGet:
path: /ready
port: 8080
periodSeconds: 5
failureThreshold: 3The 10-second preStop sleep gives endpoint removal time to propagate while the app still serves traffic. SIGTERM then arrives, and the app has about 35 seconds to finish requests and exit.
Application responsibilities
On SIGTERM, a well-behaved server does this:
- Optionally flips its readiness endpoint to failing.
- Stops accepting new connections.
- Finishes in-flight requests.
- Closes database connections and exits with status 0.
Set the grace period longer than your slowest normal request plus the preStop delay. If shutdown regularly hits the limit, SIGKILL cuts requests mid-flight.
Exercise
A Deployment has terminationGracePeriodSeconds: 30 and a preStop hook that sleeps 25 seconds. The app needs 10 seconds to drain after SIGTERM. What goes wrong, and how do you fix it?
Show answer
The preStop hook counts against the 30-second grace period, so only about 5 seconds remain after SIGTERM. The kubelet sends SIGKILL before the app finishes draining. Fix it by shortening the preStop sleep (for example to 10 seconds) or raising terminationGracePeriodSeconds to at least 35 seconds, preferably with some margin.