简体   繁体   English

Kubernetes 中的自动 Pod 删除延迟

[英]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 生命周期有以下几个步骤。

  • Pod is set to the “Terminating” State and removed from the endpoints list of all Services Pod 设置为“终止”状态并从所有服务的端点列表中删除
  • preStop hook is executed执行 preStop 钩子
  • SIGTERM signal is sent to the pod SIGTERM 信号发送到 pod
  • Kubernetes waits for a grace period, default is 30 seconds Kubernetes 等待一个宽限期,默认为 30 秒
  • SIGKILL signal is sent to pod, and the pod is removed向pod发送SIGKILL信号,移除pod

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.

相关问题 自动重启Kubernetes Pod - Automatic restart of a Kubernetes pod Kubernetes-在污点/公差上启用自动Pod重新计划 - Kubernetes - Enable automatic pod rescheduling on taint/toleration 延迟kubernetes pod的创建以实现零停机时间 - Delay kubernetes pod creation for zero downtime 如果另一个 pod 重启,如何在 Kubernetes 中配置自动 pod 重启 - How to configure automatic pod reboot in Kubernetes if another pod reboots Redis 在 kube.netes HELM 图表中删除 pod 后哨兵不通信 - Redis sentinel is not communicating after pod deletion in kubernetes HELM Chart Kubernetes Pod 从删除重新启动时接收 curl 请求,复制的 pod 处于活动状态 - Kubernetes Pod receiving curl requests when restarting from deletion, with replicated pod alive 如何保持 kubernetes pod 的状态不变或自动恢复 - how to keep kubernetes pod's status the same or recover automatic 在使用pod之前是否有kubernetes config parm(service或rc或其他)延迟 - Is there a kubernetes config parm (service or rc or other) to delay before using a pod 为什么GCE卷安装在kubernetes容器中会导致延迟? - Why does a GCE volume mount in a kubernetes pod cause a delay? 当 Kubernetes 活跃度探测失败时,是否可以指定 Pod 重启的延迟? - Is it possible to specify a delay for pod restart when Kubernetes liveness probe fails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM