简体   繁体   English

优美的吊舱终止

[英]Graceful pod termination

I need to let the container to run 5 minutes after the kubectl ' termination. 我需要让该容器在kubectl终止后5分钟运行。 It needs to do some work before it's destroyed. 它需要做一些工作才能销毁。 It seems that kubernetes contains exactly what I need: 看来kubernetes完全包含我需要的东西:

terminationGracePeriodSeconds: 300

so I defined it within my yaml. 所以我在yaml中定义了它。 I've updated running RCs , delete current pods so new ones were created and now I can see that a pod contains exactly this setting via get pod xyz -o=yaml . 我已经更新了正在运行的RCs ,删除了当前的Pod,因此创建了新的Pod,现在我可以通过get pod xyz -o=yaml看到一个Pod完全包含此设置。

Unfortunately, when I tried to do rolling-update , the original pod was killed after exactly 1 minute, not after 5 minutes. 不幸的是,当我尝试进行rolling-update ,原来的Pod在1分钟后(而不是5分钟后)就被杀死了。 I does ssh to the target machine and I could see that docker termineted the container after this time. 我确实对目标计算机执行了ssh操作,然后可以看到Docker在该时间之后终止了该容器。

I tried to do some investigation how the feature works. 我试图做一些调查功能如何工作。 I finally found the documentation to kubectl delete where there is a notion about graceful termination period: 我终于找到了kubectl delete的文档,其中有关于kubectl delete终止的概念:

http://kubernetes.io/docs/user-guide/pods/ http://kubernetes.io/docs/user-guide/pods/

By default, all deletes are graceful within 30 seconds. 默认情况下,所有删除都会在30秒内正常进行。 The kubectl delete command supports the --grace-period= option which allows a user to override the default and specify their own value. kubectl delete命令支持--grace-period =选项,该选项允许用户覆盖默认值并指定自己的值。 The value 0 indicates that delete should be immediate, and removes the pod in the API immediately so a new pod can be created with the same name. 值0表示删除应立即生效,并立即删除API中的容器,以便可以创建具有相同名称的新容器。 On the node pods that are set to terminate immediately will still be given a small grace period before being force killed 在节点上设置为立即终止的Pod仍将被给予短暂的宽限期,然后被强制杀死

So I took one pod, nginx, and try to delete it with grace-period=30 . 所以我拿了一个pod nginx,尝试用grace-period=30删除它。 It turned out, that original pod was immediately delete and get pods showed that new one was being started. 原来,原来的Pod已被立即删除,而get pods显示新的get pods正在启动。

So no 30 seconds. 所以没有30秒。 What am I doing wrong? 我究竟做错了什么? It seems that all pods kubernetes does not take these values into account. 似乎所有pod kubernetes都没有考虑这些值。 Note that I'm using kubernetes v1.2.2 请注意,我正在使用kubernetes v1.2.2

I also found this issue https://github.com/kubernetes/kubernetes/issues/24695 where the reporter had same problem and he solved it in the same fashion. 我还发现了这个问题https://github.com/kubernetes/kubernetes/issues/24695 ,那里的记者遇到了同样的问题,他以同样的方式解决了。 So eg 300 seconds is not too much for kubernetes. 因此,对于berbernetes而言,例如300秒并不是太多。

You may be able to set magic sleep in 'preStop' hook. 您可能可以在“ preStop”挂钩中设置魔术睡眠。 This hook will be extecuted prior to kubectl sending SIGTERM to your container. kubectlSIGTERM发送到您的容器之前,该钩子将被保护。

http://kubernetes.io/docs/user-guide/production-pods/#lifecycle-hooks-and-termination-notice http://kubernetes.io/docs/user-guide/production-pods/#lifecycle-hooks-and-termination-notice

something like: 就像是:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sleep","300"]

https://pracucci.com/graceful-shutdown-of-kubernetes-pods.html may this can help you. https://pracucci.com/graceful-shutdown-of-kubernetes-pods.html可能对您有帮助。

There're some circumstances where a SIGTERM violently kill the application, vanishing all your efforts to gracefully shutdown it. 在某些情况下,SIGTERM会猛烈杀死该应用程序,从而使您无法正常关闭该应用程序。 Nginx, for example, quickly exit on SIGTERM. 例如,Nginx在SIGTERM上快速退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM