How Kubelet Deletes Pods Gracefully (Step-by-step)

When a Pod is deleted, Kubelet (the agent running on each AKS node) handles its graceful termination using a process called graceful shutdown. Here’s how it works:

1. User runs kubectl delete pod

You (or a controller like Deployment) run:

kubectl delete pod my-app

This sends a delete request to the Kubernetes API server.

2. API server sets a “deletion timestamp”

The pod is not immediately removed. Instead, Kubernetes marks it for deletion with a terminationGracePeriodSeconds (default: 30 seconds).

This is a signal to Kubelet to begin shutting it down gracefully.

3. Kubelet receives the deletion event

Kubelet notices the pod has a deletion timestamp and begins termination.


💬 4. Kubelet sends SIGTERM to containers

Kubelet sends a SIGTERM (terminate signal) to each container in the pod.

  • Containers should catch this signal and begin shutting down.
  • For example, a web server may finish processing current requests.

5. Wait for terminationGracePeriodSeconds

Kubelet gives the container time to exit gracefully (default 30s, can be customized in Pod spec):

spec:
terminationGracePeriodSeconds: 10

6. If container doesn’t stop → send SIGKILL

If the container doesn’t exit in time, Kubelet forcefully stops it using SIGKILL.

This is like a hard shutdown (kill -9).


✅ 7. Kubelet deletes the Pod from the node

Once containers are stopped (gracefully or forcefully), Kubelet cleans up:

  • Removes the pod’s containers and volumes
  • Frees node resources
  • Updates the status back to the API server

👀 Optional: preStop Hook

You can run a custom script before shutdown using preStop:

lifecycle:
preStop:
exec:
command: [“/bin/sh”, “-c”, “echo Shutting down… && sleep 5”]

This runs before SIGTERM is sent.

Leave a Comment

Your email address will not be published. Required fields are marked *