Kubernetes pod graceful shutdown

Because Pods represent processes running on nodes in the cluster, it is important to allow those processes to gracefully terminate when they are no longer needed (rather than being abruptly stopped with a KILL signal and having no chance to clean up).
Typically, the container runtime sends a TERM signal to the main process in each container. Many container runtimes respect the STOPSIGNAL value defined in the container image and send this instead of TERM. Once the grace period has expired, the KILL signal is sent to any remaining processes, and the Pod is then deleted from the API Server. If the kubelet or the container runtime's management service is restarted while waiting for processes to terminate, the cluster retries from the start including the full original grace period.

example flow

Kubectl delete pod ftp-rest-service-84599d54fd-m56bj

This command is trying to delete a specific pod, with the default grace period (30 seconds).

  1. If you use kubectl describe to check on the Pod you're deleting, that Pod shows up as "Terminating". On the node where the Pod is running: as soon as the kubelet sees that a Pod has been marked as terminating (a graceful shutdown duration has been set), the kubelet begins the local Pod shutdown process.
  • If one of the Pod's containers has defined a preStop hook, the kubelet runs that hook inside of the container. If the preStop hook is still running after the grace period expires, the kubelet requests a small, one-off grace period extension of 2 seconds. If the preStop hook needs longer to complete than the default grace period allows, you must modify terminationGracePeriodSeconds to suit this.
  • The kubelet triggers the container runtime to send a TERM signal to process 1 inside each container (current example we run one container in the pod).
  1. At the same time as the kubelet is starting graceful shutdown
  • the control plane removes that shutting-down Pod from Endpoints (and, if enabled, EndpointSlice) objects where these represent a Service with a configured selector.
  • ReplicaSets and other workload resources no longer treat the shutting-down Pod as a valid, in-service replica.
  • Pods that shut down slowly cannot continue to serve traffic as load balancers (like the service proxy) remove the Pod from the list of endpoints as soon as the termination grace period begins.
  1. When the grace period expires, the kubelet triggers forcible shutdown. The container runtime sends SIGKILL to any processes still running in any container in the Pod. The kubelet also cleans up a hidden pause container if that container runtime uses one.

  2. The kubelet triggers forcible removal of Pod object from the API server, by setting grace period to 0 (immediate deletion).

  3. The API server deletes the Pod's API object, which is then no longer visible from any client.

Summary

image.png

The following 5 steps occur when Kubernetes kills a pod:

  • The pod switches to Terminating state and stops receiving any new traffic. Container is still running inside the pod.
  • preStop hook that is a special command or HTTP request is executed, and is sent to the container inside the pod.
  • SIGTERM signal is sent to pod and the container realizes that it will close soon.
  • Kubernetes waits for a grace period (terminationGracePeriodSeconds). This waiting is parallel to preStop hook and SIGTERM signal executions (default 30 sec). So, Kubernetes doesn’t wait for these to finish. If this period is finished, it goes directly to the next step. It is very important to correctly set the value of the grace period.
  • SIGKILL signal is sent to the pod, and the pod is removed. If the container is still running after the grace period, the pod is forcibly removed by SIGKILL, and the termination is finished.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Pod是Kubernetes集群中的最小單元,而 Pod 是由容器組成的,所以在討論 Pod 的生命周期的時(shí)候我們...
    祁恩達(dá)閱讀 2,879評論 0 0
  • 初始化 kubeadm安裝 root用戶執(zhí)行以下命令 執(zhí)行完后,可以看到已經(jīng)安裝成功 切換到非root用戶 此時(shí)k...
    SparkOnly閱讀 550評論 0 0
  • 1 Pod - 實(shí)例 Pod是一組緊密關(guān)聯(lián)的容器集合,支持多個(gè)容器在一個(gè)Pod中共享網(wǎng)絡(luò)和文件系統(tǒng),可以通過進(jìn)程間...
    HankerCloud2019閱讀 375評論 0 0
  • Introduction Modern stateless applications are built and ...
    ???木?燚??閱讀 550評論 0 0
  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,866評論 0 4

友情鏈接更多精彩內(nèi)容