Delete a Pod in Kubernetes

Harish SharmaHarish Sharma
2 min read

When you delete a pod in Kubernetes, several things happen as part of Kubernetes’ pod lifecycle and orchestration to maintain the desired state of the cluster:

1. Graceful Termination Process Initiated

  • When you delete a pod, Kubernetes triggers a graceful termination process. This allows the pod to shut down cleanly.

  • By default, Kubernetes waits for 30 seconds (terminationGracePeriodSeconds) before forcibly terminating the pod, allowing it to complete ongoing requests or jobs.

2. PreStop Hook Execution

  • If a preStop hook is defined, it executes before the container stops. This is useful for running custom commands (e.g., saving data or closing connections) before shutting down the pod.

3. Pod Enters the "Terminating" State

  • Kubernetes changes the pod’s status to Terminating, which notifies users and other parts of the system that the pod is in the process of shutting down.

4. Container Stop Signal Sent

  • The kubelet sends a termination signal (SIGTERM) to each container within the pod. The application inside the container should handle this signal to cleanly exit, freeing up resources.

5. Pod Removed from Service Endpoint

  • During this period, the pod is removed from any Service endpoints so that traffic is no longer routed to it. This ensures that the pod does not receive new requests during shutdown.

6. Final Pod Termination (SIGKILL)

  • After the terminationGracePeriodSeconds (default: 30 seconds), any remaining containers are forcibly terminated with a SIGKILL signal if they haven’t already shut down.

7. Pod Deletion from etcd

  • Kubernetes removes the pod’s information from etcd, the cluster’s central data store, which stores the desired and current state of resources in the cluster. Once deleted, the pod no longer appears in the cluster.

8. Recreation of the Pod (if part of a Controller)

  • If the pod is managed by a controller (such as a Deployment, ReplicaSet, StatefulSet, or DaemonSet), the controller detects the missing pod and creates a new pod to maintain the desired replica count.

  • The recreated pod will have a new IP address and identifier, as Kubernetes treats each pod as an ephemeral instance.

Summary

In short, deleting a pod starts a controlled shutdown sequence, removing it from service, handling pre-stop tasks, and ensuring graceful termination where possible. If the pod is part of a managed set, the controller automatically replaces it to maintain application availability.

0
Subscribe to my newsletter

Read articles from Harish Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harish Sharma
Harish Sharma

Devops Engineer.