Graceful termination isn't a new idea; it is a common practice in programming. Killing a pod forcibly while it's still working is like suddenly unplugging the power cord of a running computer, which could harm the data.
The implementation principally includes three steps:
- Register a handler to capture termination signals.
- Do everything required in the handler, such as freeing resources, writing data to external persistent layers, releasing distribution locks, or closing connections.
- Perform a program shutdown. Our previous example demonstrates the idea: closing the controller thread on SIGTERM in the graceful_exit_handler handler. The code can be found here: https://github.com/DevOps-with-Kubernetes/okeydokey/blob/master/app.py.
Due to the fact that Kubernetes can only send signals to the PID 1 process in a container, there are some common pitfalls that could fail the graceful handler in our program.