In this lesson, you will learn how Kubernetes coordinates the orderly shutdown of a Pod to prevent data corruption and ensure pending tasks are completed.
The Termination Sequence
When Kubernetes decides to terminate a Pod (e.g., during a rollout, a node scale-down, or a manual kubectl delete), it doesn't just pull the plug. Instead, it follows a graceful process designed to give your application time to "clean up."
- SIGTERM Signal: The Kubelet sends a
SIGTERMsignal to the main process (PID 1) inside your container. This is your application’s "heads-up" that it is being asked to stop. - Grace Period: Kubernetes grants the Pod a
terminationGracePeriodSeconds(default is 30 seconds). During this time, your application should stop accepting new requests, finish processing existing ones, and close database connections. - SIGKILL Signal: If your application is still running after the grace period expires, the Kubelet sends a
SIGKILLsignal, which forces the container to exit immediately.
Why Handling SIGTERM Matters
If your application ignores the SIGTERM signal, it won't perform a clean shutdown. This might lead to incomplete database transactions or interrupted file writes. To handle this correctly, your code must listen for the termination signal.
For example, in a Python application, you would handle the signal like this:
import signal
import time
def handle_term(signum, frame):
print("Received SIGTERM! Saving state...")
# Add cleanup logic here
exit(0)
signal.signal(signal.SIGTERM, handle_term)
Coordination with Traffic
While your Pod is terminating, the Kubernetes API Server removes the Pod's endpoint from its Service. However, it takes a small amount of time for this change to propagate to all kube-proxy instances across the cluster. A well-behaved application will continue to "drain" its existing connections during this period before fully shutting down, ensuring that no active requests are dropped abruptly. By respecting the SIGTERM signal, you ensure your services remain reliable even during active cluster maintenance.
Key Takeaway: Graceful termination relies on your application listening for the SIGTERM signal and performing necessary cleanup tasks within the allotted grace period before the Kubelet forces a shutdown.