[英]Automatic Pod Deletion Delay in Kubernetes
Is there is a way to automatically delay all Kubernetes pod deletion requests such that the endpoint deregistration is signaled, but the pod's SIGTERM is delayed by several seconds?是否有一种方法可以自动延迟所有Kubernetes pod 删除请求,以便通知端点注销,但 pod 的 SIGTERM 延迟了几秒钟?
It would be preferable, but not required, if the delay only affected pods with an Endpoint/Service.如果延迟仅影响具有端点/服务的 Pod,那将是可取的,但不是必需的。
Background:背景:
It is well established that some traffic can continue to a Pod after a pod has been sent the SIGTERM termination signal due to the asynchronous nature of endpoint deregistration and the deletion signal.这是很好建立,部分流量可以继续荚荚已发送SIGTERM终止信号由于后异步端点注销的性质和删除信号。 The recommended mitigation is to introduce a few seconds delay in the pod's preStop
lifecycle hook by invoking sleep
.推荐的缓解措施是通过调用sleep
在 pod 的preStop
生命周期挂钩中引入几秒钟的延迟。
The difficulty rapidly arises where the pod's deployment may be done via helm or other upstream source, or else there are large numbers of deployments and containers to be managed.如果 pod 的部署可能通过 helm 或其他上游源完成,或者有大量部署和容器需要管理,问题就会迅速出现。 Modifying many deployments in such a way may be difficult, or even impossible (eg the container may not have a sleep binary, shell, or anything but the application executable).以这种方式修改许多部署可能很困难,甚至不可能(例如,容器可能没有 sleep 二进制文件、shell 或除了应用程序可执行文件之外的任何东西)。
I briefly explored a mutating admission controller, but that seems unworkable to dynamically add a preStop
hook, as all images do not have a /bin/sleep
or already have a preStop
that could need image-specific knowledge to merge.我简要地探索了一个变异的准入控制器,但动态添加一个preStop
钩子似乎不可行,因为所有图像都没有/bin/sleep
或者已经有一个可能需要图像特定知识来合并的preStop
。
(Of course, all of this could be avoided if the K8S API made the endpoint deregistration synchronous with a timeout to avoid deadlock (hint, hint), but I haven't seen any discussions of such a change. Yes, there are tons of reasons why this isn't synchronous, but that doesn't mean something can't be done.) (当然,如果 K8S API 使端点注销与超时同步以避免死锁(提示,提示),所有这些都可以避免,但我还没有看到任何关于这种更改的讨论。是的,有很多这不是同步的原因,但这并不意味着某些事情无法完成。)
Kubernetes lifecycle has following steps. Kubernetes 生命周期有以下几个步骤。
Grace period is what you need.宽限期正是您所需要的。 It's important to node that this grace period is happening in parallel to the preStop hook and the SIGTERM signal.重要的是要注意这个宽限期与 preStop 钩子和 SIGTERM 信号并行发生。
A call to the preStop hook fails if the container is already in terminated or completed state.如果容器已处于终止或完成状态,则对 preStop 钩子的调用将失败。 It is blocking, meaning it is synchronous, so it must complete before the call to delete the container can be sent.它是阻塞的,意味着它是同步的,所以它必须在删除容器的调用被发送之前完成。
Here you can read more about Container Lifecycle Hooks .在这里你可以阅读更多关于Container Lifecycle Hooks 的信息。
So for example you could set the terminationGracePeriodSeconds: 90
and this might look like the following:因此,例如,您可以将terminationGracePeriodSeconds: 90
设置为如下所示:
spec:
terminationGracePeriodSeconds: 90
containers:
- name: myApplication
You can read the Kubernetes docs regarding Termination of Pods .您可以阅读关于Pods 终止的 Kubernetes 文档。 I also recommend great blog post Kubernetes best practices: terminating with grace .我还推荐了一篇很棒的博客文章Kubernetes 最佳实践:以优雅终止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.