tubaobaoya3 发表于 2018-1-5 14:51:39

[置顶]kubernetes--优雅删除资源对象

当用户请求删除含有pod的资源对象时(如RC、deployment等),K8S为了让应用程序优雅关闭(即让应用程序完成正在处理的请求后,再关闭软件),K8S提供两种信息通知:  1)、默认:K8S通知node执行docker stop命令,docker会先向容器中PID为1的进程发送系统信号SIGTERM,然后等待容器中的应用程序终止执行,如果等待时间达到设定的超时时间,或者默认超时时间(30s),会继续发送SIGKILL的系统信号强行kill掉进程。
  2)、使用pod生命周期(利用PreStop回调函数),它执行在发送终止信号之前。
  默认情况下,所有的删除操作的优雅退出时间都在30秒以内。kubectl delete命令支持--grace-period=的选项,以运行用户来修改默认值。0表示删除立即执行,并且立即从API中删除pod这样一个新的pod会在同时被创建。在节点上,被设置了立即结束的的pod,仍然会给一个很短的优雅退出时间段,才会开始被强制杀死。

pod生命周期例子
  

kind: Deployment  
metadata:
  name: nginx-demo
  labels:
  app:nginx-demo
  
spec:
  replicas: 1
  template:
  metadata:
  labels:
  app: nginx-demo
  spec:
  containers:
  - name: nginx-demo
  image: centos:nginx
  lifecycle:
  preStop:
  exec:
  # nginx -s quit gracefully terminate while SIGTERM triggers a quick exit
  command: ["/usr/local/nginx/sbin/nginx","-s","quit"]
  ports:
  - name: http
  containerPort: 80
  
页: [1]
查看完整版本: [置顶]kubernetes--优雅删除资源对象