I'm using preStop command to gracefully shutdown my server application when I delete a pod. What is the state of the pod/ container when it runs preStop command? For example, does it stop the network interfaces before running the preStop command?
当我删除一个pod时,我使用preStop命令来优雅地关闭我的服务器应用程序。在运行preStop命令时,pod/ container的状态是什么?例如,在运行preStop命令之前,它是否停止网络接口?
lifecycle:
preStop:
exec:
command: ["kill", "-SIGTERM", "`pidof java`"]
2 个解决方案
#1
2
The state of the pod doesn't change while preStop hooks are run -- the preStop hook is run in the container, and then the container is stopped.
在运行preStop钩子时,pod的状态不会改变——preStop钩子在容器中运行,然后容器被停止。
#2
1
When a pod should be terminated, Kubernetes does the following:
当一个豆荚被终止时,Kubernetes会做如下的事情:
- Switch the Pod to the
Terminating
state - 将Pod切换到终止状态。
- Invoke the
preStop
hook (if any) - 调用preStop钩子(如果有的话)
- Once the
preStop
hook ends, it sends aSIGTERM
to the main process in the container (PID 1) - 一旦preStop钩子结束,它就会向容器中的主进程发送一个SIGTERM (PID 1)
- If the container doesn't terminate with the termination grace period (30 seconds by default - starts counting at point #1), Kubernetes will send a
SIGKILL
to the container's main process to violently stop it - 如果容器没有终止使用终止grace周期(默认情况下是30秒),Kubernetes将发送一个SIGKILL到容器的主进程以阻止它。
More details here: Graceful shutdown of Kubernetes Pods
更多细节:Kubernetes pod的优雅关闭。
#1
2
The state of the pod doesn't change while preStop hooks are run -- the preStop hook is run in the container, and then the container is stopped.
在运行preStop钩子时,pod的状态不会改变——preStop钩子在容器中运行,然后容器被停止。
#2
1
When a pod should be terminated, Kubernetes does the following:
当一个豆荚被终止时,Kubernetes会做如下的事情:
- Switch the Pod to the
Terminating
state - 将Pod切换到终止状态。
- Invoke the
preStop
hook (if any) - 调用preStop钩子(如果有的话)
- Once the
preStop
hook ends, it sends aSIGTERM
to the main process in the container (PID 1) - 一旦preStop钩子结束,它就会向容器中的主进程发送一个SIGTERM (PID 1)
- If the container doesn't terminate with the termination grace period (30 seconds by default - starts counting at point #1), Kubernetes will send a
SIGKILL
to the container's main process to violently stop it - 如果容器没有终止使用终止grace周期(默认情况下是30秒),Kubernetes将发送一个SIGKILL到容器的主进程以阻止它。
More details here: Graceful shutdown of Kubernetes Pods
更多细节:Kubernetes pod的优雅关闭。