liveBook · Manning

Termination grace period

As explained in Kubernetes in Action, Second Edition

The terminationGracePeriodSeconds field specifies the default number of seconds Kubernetes allows a pod's containers to terminate gracefully.

The termination grace period is the time Kubernetes gives a container to shut down cleanly. The pod’s spec.terminationGracePeriodSeconds field sets the default, which is 30 seconds. section 6.4.2

Kubernetes follows this shutdown sequence:

  • Runs the container’s pre-stop hook, if one exists. section 6.4.2
  • Sends SIGTERM to the main container process, asking the application to exit. section 6.4.2
  • Sends KILL if the process remains after the grace period expires. section 6.4.2
  • Restarts the container afterward when the pod’s restart policy allows it. section 6.4.2

Deleting a Pod object uses a deletion grace period instead. It defaults to terminationGracePeriodSeconds but can be overridden with kubectl delete --grace-period. Containers in the pod shut down in parallel, and the Pod object is deleted after they stop. section 6.4.3

Shortening a pod’s shutdown window

Set spec.terminationGracePeriodSeconds to 5 for a pod that should finish shutdown sooner. A better fix for slow shutdown is usually handling SIGTERM in the application, allowing the server to close cleanly before Kubernetes forces termination. section 6.4.3 section 6.5

Figure 6.15 A container’s termination sequence
figure

Follow the termination sequence from the pre-stop hook through SIGTERM and forced KILL. section 6.4.2

How Kubernetes in Action, Second Edition covers it

How deep it goes
Explains hooks, SIGTERM, KILL, defaults, restart behavior, and deletion-period overrides.
Examples
Uses the kiada-ssl pod, a five-second manifest, kubectl deletion, and a SIGTERM handler.
What it assumes
Assumes familiarity with pods and containers while explaining lifecycle hooks, signals, and shutdown behavior.
What you can do after
Operate Kubernetes shutdowns by configuring grace periods and diagnosing applications that ignore SIGTERM.
How it connects
Connects pre-stop hooks, SIGTERM, KILL, restartPolicy, and deletionGracePeriodSeconds.

Also explained in

Compare how 2 books explain termination grace period →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kubernetes in Action, Second Edition. The section links point to the parts of the book it draws on.