Sub topic 13
Graceful termination and preStop hooks
This chapter assumes you can read a Pod manifest, know the Pod lifecycle, and understand restartPolicy. It covers what happens between a delete request and the moment a Pod disappears.
What happens when a Pod is deleted
Deleting a Pod does not kill it right away. Kubernetes gives the containers time to finish their work. Without this, in-flight requests would be dropped on every deployment or scale-down.
The sequence is:
- The API server marks the Pod as
Terminatingand sets a deletion deadline. - The Pod is removed from Service endpoints, so new traffic stops arriving. This happens in parallel with the next steps, not before them.
- The kubelet runs each container's
preStophook, if one is defined. - The kubelet sends
SIGTERMto the main process of each container. - If a container is still running when the grace period ends, the kubelet sends
SIGKILL.
The grace period
terminationGracePeriodSeconds sets the total time allowed. The default is 30 seconds. The preStop hook and the SIGTERM handling both count against this one budget. The timer does not restart after the hook finishes.
apiVersion: v1
kind: Pod
metadata:
name: graceful
spec:
terminationGracePeriodSeconds: 30
containers:
- name: nginx
image: nginx:1.27
lifecycle:
preStop:
exec:
command:
- sh
- -c
- sleep 5Here the hook sleeps for 5 seconds. Then SIGTERM arrives, leaving about 25 seconds for nginx to drain connections and exit.
Why a preStop hook helps
Endpoint removal and shutdown run at the same time. For a short moment, a load balancer or proxy may still send requests to a Pod that has started shutting down. A short sleep in preStop keeps the container serving while that change spreads. It is a common workaround, and it is a delay, not a guarantee.
A hook can use exec to run a command inside the container, or httpGet to call an endpoint. If the hook fails or hangs, the kubelet still moves on to SIGTERM once the hook ends or the grace period runs out.
Handling SIGTERM correctly
The hook only buys time. Your application must still react to SIGTERM by stopping new work, finishing current work, and exiting.
- Shell wrappers swallow signals. If the container starts with
sh -c "myapp", the shell may be PID 1 and not forwardSIGTERM. Useexec myappin the script, or the exec form ofcommand. - Slow shutdowns get killed. If your app needs 60 seconds to drain, raise
terminationGracePeriodSecondsto more than that. - Fast shutdowns waste time. Setting it too high delays rollouts if the app ignores
SIGTERM, because Kubernetes waits for the full period beforeSIGKILL.
Exit code 143 (128 plus signal 15) means the process ended after SIGTERM. Exit code 137 (128 plus signal 9) means it was killed with SIGKILL, which usually points to a grace period that was too short or a process that ignored the signal.
Exercise
A Pod has terminationGracePeriodSeconds: 20 and a preStop hook that sleeps for 8 seconds. About how much time does the application have after it receives SIGTERM before SIGKILL, and why?
Show answer
About 12 seconds. The preStop hook and SIGTERM handling share the same 20-second budget, so the 8 seconds spent in the hook are not added back.