gpt4 book ai didi

kubernetes - 优雅的 Pod 终止

转载 作者:行者123 更新时间:2023-12-01 11:23:43 25 4
gpt4 key购买 nike

我需要让容器在 kubectl 之后运行 5 分钟'终止。它需要在销毁之前做一些工作。似乎 kubernetes 包含我需要的内容:

terminationGracePeriodSeconds: 300

所以我在我的 yaml 中定义了它。我更新了运行 RCs , 删除当前的 Pod 以便创建新的 Pod,现在我可以通过 get pod xyz -o=yaml 看到一个 Pod 正好包含此设置.

不幸的是,当我试图做 rolling-update ,原来的 pod 在 1 分钟后被杀死,而不是在 5 分钟后被杀死。我对目标机器进行了 ssh,我可以看到 docker 在这段时间之后终止了容器。

我试图做一些调查该功能是如何工作的。我终于找到了 kubectl delete 的文档其中有关于优雅终止期的概念:

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

By default, all deletes are graceful within 30 seconds. The kubectl delete command supports the --grace-period= option which allows a user to override the default and specify their own value. 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. On the node pods that are set to terminate immediately will still be given a small grace period before being force killed



所以我拿了一个 pod,nginx,并尝试用 grace-period=30 删除它.原来,原来的 pod 立即被删除, get pods表明新的正在启动。

所以没有30秒。我究竟做错了什么?似乎所有 pod Kubernetes 都没有考虑这些值。
请注意,我使用的是 kubernetes v1.2.2

我也发现了这个问题 https://github.com/kubernetes/kubernetes/issues/24695记者遇到了同样的问题,他以同样的方式解决了它。所以例如300 秒对于 Kubernetes 来说并不算多。

最佳答案

您可以在 'preStop' Hook 中设置魔法 sleep 。此 Hook 将在 kubectl 之前执行发送 SIGTERM到你的容器。

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

就像是:

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"]

关于kubernetes - 优雅的 Pod 终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39493043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com