9.18 Kubernetes Pod Termination Lifecycle
Kubernetes Pod Termination Lifecycle outlines how pods gracefully shut down, ensuring data consistency and resource cleanup before termination.
Kubernetes Pod Termination Lifecycle is the ordered sequence of events that occurs from the moment a Pod is marked for deletion through to its final removal from the cluster, encompassing the invocation of shutdown hooks, the delivery of termination signals to running processes, the enforcement of a bounded grace period, and the eventual cleanup of the Pod object itself, coordinated jointly by the API server and the kubelet.
Initiating Termination
Marking the Pod for Deletion
Termination begins when a delete request is issued against the Pod object, whether by direct user action, a controller replacing the Pod, or the node-level eviction process. Rather than being removed from etcd instantly, the API server sets a deletionTimestamp on the Pod, signaling that termination has started while the object still temporarily exists.
deletionGracePeriodSeconds
Alongside the deletion timestamp, the API server records a deletionGracePeriodSeconds value, derived from the Pod's configured terminationGracePeriodSeconds unless overridden by the deletion request itself, establishing the maximum duration the Pod is allowed before being forcibly removed.
Pre-Stop Signal Phase
preStop Hook Execution
If a container defines a preStop lifecycle hook, the kubelet invokes it before sending any termination signal to the container's main process. This hook runs synchronously, meaning the subsequent termination signal is withheld until the hook completes or the grace period is exhausted, whichever comes first.
Simultaneous Endpoint Removal
Concurrently with the start of termination, the Pod is typically removed from the endpoint lists of any Services that select it, since its deletionTimestamp being set is treated as a signal that it should no longer receive new traffic, even before its containers have actually begun shutting down.
Termination Signal Delivery
SIGTERM to the Main Process
Once any preStop hook has finished, or immediately if none is defined, the container runtime sends a termination signal, conventionally SIGTERM on Linux systems, to the container's main process, giving the application an opportunity to shut down gracefully, close open connections, and flush any pending state.
Application Responsibility During This Window
The application itself is responsible for handling the termination signal appropriately; if it ignores the signal entirely, it will continue running until the grace period expires and it is forcibly killed, meaning graceful shutdown fundamentally depends on cooperative signal handling within the container's own process.
Grace Period Enforcement
Countdown Toward Forced Termination
From the moment the deletion is initiated, the kubelet tracks elapsed time against the configured grace period. If the container's process has not exited on its own once this period elapses, the kubelet escalates by sending a forceful kill signal, conventionally SIGKILL, which cannot be intercepted or ignored by the process.
Balancing Cleanliness Against Timeliness
The grace period represents a deliberate tradeoff between allowing enough time for genuinely graceful shutdown and ensuring that a hung or unresponsive process does not indefinitely delay Pod removal, with the default value of thirty seconds serving as a reasonable baseline that can be tuned per workload.
Object Cleanup
Removal From etcd
Once all containers in the Pod have terminated, the kubelet signals this back to the API server, which then proceeds to remove the Pod object from etcd entirely, completing the termination lifecycle and freeing the Pod's name for potential reuse.
Interaction With Owner References and Garbage Collection
If the Pod carries owner references marking it as a dependent of a controller, its removal may also be coordinated with the broader garbage collection process, particularly under foreground deletion semantics, where the owning object may itself remain in a terminating state until this dependent Pod has been fully cleaned up.
Special Case: Forceful Deletion
Bypassing the Grace Period
A deletion request can specify a gracePeriodSeconds of zero, requesting immediate forceful removal, which skips the ordinary cooperative shutdown window entirely. This is generally reserved for situations where a Pod is already known to be unresponsive or where immediate removal is explicitly required, since it bypasses the opportunity for clean application shutdown.