Note : It is one of the Kubernetes best practices to allow termination with grace.
Kubernetes waits for a specified time called the termination grace period. By default, this is 30 seconds. It’s important to note that this happens in parallel to the preStop hook and the SIGTERM signal.
NOTE : Kubernetes does not wait for the preStop hook to finish.If your app finishes shutting down and exits before the terminationGracePeriod is done, Kubernetes moves to the next step immediately.
If your pod usually takes longer than 30 seconds to shut down, make sure you increase the grace period. You can do that by setting the
terminationGracePeriodSeconds option in the Pod YAML.
It’s important that your application terminate gracefully so that there is minimal impact on the end user and the time-to-recovery is as fast as possible!
To change it to value lower or higher than default value of 30 seconds include the flag as below example.
- image: nginx
We can always make use of
kubectl explain command to find supported options under spec.
For example :
kubectl explain deployment.spec.template.spec
master $ kubectl explain deployment.spec.template.spec.terminationGracePeriodSeconds
FIELD: terminationGracePeriodSeconds <integer>
Optional duration in seconds the pod needs to terminate gracefully. May be
decreased in delete request. Value must be non-negative integer. The value
zero indicates delete immediately. If this value is nil, the default grace
period will be used instead. The grace period is the duration in seconds
after the processes running in the pod are sent a termination signal and
the time when the processes are forcibly halted with a kill signal. Set
this value longer than the expected cleanup time for your process. Defaults
to 30 seconds.