Skip to main content
Running workloadsLesson 6 of 14

Graceful shutdown — going down without losing requests

Rollouts, scale-downs and node drains all end with "delete one pod". Here we follow the order in which that pod goes down without losing the requests it was handling. The full rollout flow is in Zero-Downtime Deploys (Rolling Update), and node maintenance is in Draining Nodes and Upgrading the Cluster.

Pods taking requests2
web-1 in-flight requests3
Grace time left—
Failed requests0
User requestsmust not breakDelete requestrollout·scale·drainAPI servermarks deletionServicespreads requestskubeletnode managerweb-1stoppingweb-2keeps running
RequestWatchAppears in a later step

What you will learn

  • Removal from the request targets and the stop signal happen at the same time
  • Why preStop waits a few seconds
  • What an app must do on SIGTERM
  • After the grace period, SIGKILL forces a stop

Shutdown order at a glance​

StepWhat happensWhat the app or config must do
1The pod becomes Terminating and the grace period startsRaise terminationGracePeriodSeconds for long tasks (default 30s)
2Removal from the request targets and stopping start at the same timeDesign assuming the order is not guaranteed
3The preStop hook runsWait a few seconds so the target removal can spread
4SIGTERM arrivesStop accepting new requests, finish in-flight ones, exit on its own
5After the grace period, SIGKILL forces a stopAt this point, in-flight requests fail

Set terminationGracePeriodSeconds and preStop in the pod template spec. Check the applied values with kubectl get pod <name> -o yaml.

Try it yourself​

kubectl get pod <pod> -o jsonpath='{.spec.terminationGracePeriodSeconds}' # Grace period (default 30)
kubectl get pod <pod> -o jsonpath='{.spec.containers[*].lifecycle.preStop}' # preStop hook
kubectl get pods -w # Watch a deleted pod go Terminating
kubectl get endpointslices -l kubernetes.io/service-name=<service> -w # Watch it leave the targets
kubectl get events --field-selector reason=Killing # When the kubelet stopped the container

Check yourself

  1. Without a preStop hook, users see intermittent errors during every rollout. Why?
  2. What should an app do when it receives SIGTERM?
  3. A deleted pod stays Terminating for about 30 seconds and then vanishes, and its in-flight requests fail. What is most likely?

Further reading: Pod lifecycle — termination of Pods · Container lifecycle hooks