✦ For everyone, free.

Practical knowledge for real and everyday life

Home

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 unfolds from the moment a Pod is marked for deletion until its object is finally removed from etcd. This sequence is designed to give running workloads a bounded opportunity to shut down cleanly, drain in-flight work, and deregister from traffic, before the kubelet forcibly ends any processes that have not exited on their own.


Initiating Termination

The Deletion Request

Termination begins when a delete request reaches the API server, whether issued directly, through kubectl delete, or by a controller replacing the Pod during a rollout or scale-down. Rather than removing the object immediately, the API server sets metadata.deletionTimestamp and metadata.deletionGracePeriodSeconds, marking the Pod as terminating while it still exists.

kubectl delete pod termination-lifecycle-example --grace-period=30

Immediate Endpoint Removal

As soon as deletionTimestamp is set, the endpoint controller removes the Pod from any Service's endpoints, regardless of its current readiness state, ensuring no new connections are routed to a Pod that is already shutting down.


The preStop Hook

Running Before the Signal

If the container defines a preStop lifecycle hook, the kubelet executes it before sending any termination signal to the container's main process. This is the primary mechanism for coordinating graceful shutdown, commonly used to pause briefly so in-flight requests routed just before endpoint removal can still complete.

containers:
  - name: app
    lifecycle:
      preStop:
        exec:
          command: ["sh", "-c", "sleep 5"]

SIGTERM and the Grace Period

Signaling the Process

After preStop completes (or immediately, if none is defined), the kubelet sends SIGTERM to the container's main process, giving the application a chance to close connections, flush buffers, and exit voluntarily.

terminationGracePeriodSeconds

The Pod's terminationGracePeriodSeconds (default thirty seconds) bounds the total time allowed for preStop execution plus the application's own response to SIGTERM. If the process has not exited by the time this window elapses, the kubelet proceeds to forceful termination.

spec:
  terminationGracePeriodSeconds: 60

SIGKILL as the Final Step

Forceful Termination

Once the grace period expires without the process exiting, the kubelet sends SIGKILL, which cannot be caught or ignored by the application, ensuring termination completes even for a process that failed to honor SIGTERM.

lastState:
  terminated:
    reason: Error
    signal: 9

Object Removal

Final Deletion From etcd

Once all containers in the Pod have reached a terminated state, the kubelet reports this back to the API server, and the Pod object is finally removed from etcd, completing the termination lifecycle. Until this point, the Pod remains visible in kubectl get pods with a Terminating status derived from the presence of deletionTimestamp.

kubectl get pod termination-lifecycle-example
NAME                            READY   STATUS        RESTARTS   AGE
termination-lifecycle-example   1/1     Terminating   0          4m

Termination Lifecycle Diagram

Delete request, endpoints removed preStop SIGTERM grace period SIGKILL

Designing an application's shutdown behavior around this sequence, honoring SIGTERM promptly and sizing terminationGracePeriodSeconds to comfortably exceed real shutdown time, is what separates clean rolling updates from dropped connections and truncated work during routine Pod replacement.