All guides

Graceful shutdown in Kubernetes: SIGTERM, preStop and grace periods

When Kubernetes stops a pod, the kubelet runs the preStop hook, sends TERM to process 1 of each container and sends KILL when the grace period ends. The pod's endpoints are updated at the same time.

Over the shoulder of someone working in a code editor on a laptop

The termination sequence

The Kubernetes documentation describes the flow for a pod that is deleted with the default grace period:

  1. The API server records on the pod the time after which it is considered dead, along with the grace period. kubectl describe shows the pod as Terminating.
  2. The kubelet sees the mark and begins the local shutdown. If a container defines a preStop hook, the kubelet runs it inside the container.
  3. The kubelet has the container runtime send a TERM signal to process 1 in each container. Many runtimes respect a STOPSIGNAL set in the image and send that signal in place of TERM.
  4. When the grace period expires, the runtime sends SIGKILL to any process still running.

Endpoint removal runs in parallel

At the same time as the kubelet starts the shutdown, the control plane evaluates whether to remove the pod from the EndpointSlices of its Services. A terminating endpoint always has its ready status set to false, so load balancers do not use it for regular traffic.

AWS's guidance for Amazon EKS spells out the consequence. The two actions occur at the same time and there are no dependencies or sequences between them. If the application drops in-flight requests and connections on receipt of SIGTERM, clients see 500 errors.

A preStop sleep

A preStop hook must complete before the TERM signal is sent. The sleep action uses that to delay the signal. The two durations below are yours to choose:

YAML

spec:
  terminationGracePeriodSeconds: 45
  containers:
    - name: my-app
      image: registry.example.com/my-app:1.4.2
      lifecycle:
        preStop:
          sleep:
            seconds: 10

The enhancement proposal for the action gives its purpose: clients that have not finished endpoint termination still open new connections, and the action handles those. In its example, the hook delays the shutdown while the Endpoint Controller removes the terminating pod, and traffic goes to the other running pods.

sleep is stable since Kubernetes v1.34, and the kubelet executes it. Before it existed, the same delay took an exec hook that ran a sleep command, which requires a sleep binary in the image. For the duration, the AWS guidance says to test the value so that it accounts for the latency of Kubernetes and of the application's dependencies.

The grace period

terminationGracePeriodSeconds defaults to 30 seconds. The countdown begins before the preStop hook is executed, so the period covers the hook and the container's own stop together. The documentation's example: with a grace period of 60 seconds, a hook that takes 55 seconds and a container that takes 10 seconds to stop, the container is killed before it has stopped.

Set the value longer than the expected cleanup time of your process, and raise it when the hook needs longer than the period allows. If the hook is still running when the period expires, the kubelet requests a one-off extension of 2 seconds.

kubectl delete supports --grace-period=<seconds> to override the value. A grace period of 0 together with --force is a forced deletion: the API server removes the pod with no confirmation from the kubelet that it has terminated. The documentation warns that this can be disruptive for some workloads.

SIGTERM in the application

The Kubernetes documentation says that a pod that shuts down should start terminating and finish processing open connections. AWS's guidance lists the cleanup an application should run when it receives SIGTERM:

  • saving data
  • closing file descriptors
  • closing database connections
  • completing in-flight requests gracefully
  • exiting in a timely manner

Whatever the application or the hook does, the containers are terminated with SIGKILL at the end of the grace period.

The PID 1 problem

The TERM signal goes to process 1 in the container. Docker's Dockerfile reference explains what the shell form of ENTRYPOINT does to it: /bin/sh -c starts the command as its subcommand and passes no signals on. The executable is not PID 1 and does not receive the signal. These are the reference's own three variants, to compare:

Dockerfile

# Shell form: top is a child of /bin/sh -c and gets no SIGTERM
ENTRYPOINT top -b

# Shell form with exec: top is PID 1
ENTRYPOINT exec top -b

# Exec form, which the reference calls the preferred form
ENTRYPOINT ["top", "-b"]

In the reference's test, docker stop on the first variant does not exit cleanly, and the command is forced to send SIGKILL after the timeout. The same reference says that a starter script for a single executable should launch it with exec, so that the executable receives the signals.

Four checks to run

  1. Read the ENTRYPOINT and CMD of each image. Change a shell form to the exec form, or start the executable with exec.
  2. Add the duration of the preStop hook to the time the container needs to stop, and compare the total with terminationGracePeriodSeconds.
  3. Restart the workload with kubectl rollout restart deployment/<name> while you send it requests, and count the failures.
  4. Run kubectl describe pod on a pod with a hook. A hook handler that fails broadcasts a FailedPreStopHook event, and its logs are not exposed in pod events.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides