简体   繁体   English

Kubernetes 优雅关闭:在终止期间继续服务流量

[英]Kubernetes Graceful Shutdown: Continue to serve traffic during termination

This question is about graceful shutdowns in Kubernetes.这个问题是关于 Kubernetes 中的正常关闭。

I have a set of servers that are eventually consistent and requires each of them to communicate with each other constantly, even if some servers are terminating.我有一组服务器,它们最终是一致的,并且要求它们中的每一个都不断地相互通信,即使某些服务器正在终止。

Since the servers communicate via HTTP requests, I need to ensure that the terminating server needs to be able to continue to receive HTTP requests in order to pass on relevant information to its peers.由于服务器通过 HTTP 请求进行通信,因此我需要确保终止服务器需要能够继续接收 HTTP 请求,以便将相关信息传递给其对等方。

Based on my understanding, there will be a few things that will happen in parallel when a server is shutting down: it will be removed from endpoints list for service and that if there is a preStop hook it will be run.根据我的理解,当服务器关闭时会同时发生一些事情:它将从服务的端点列表中删除,如果有 preStop 钩子,它将运行。 Originally I thought if I use a preStop handler and perform a sleep command then I can let the server continue to server traffic.最初我想如果我使用 preStop 处理程序并执行 sleep 命令,那么我可以让服务器继续服务器流量。 But after some experimentation I realized that the server will stop serving traffic even before the sleep ends.但经过一些实验后,我意识到即使在睡眠结束之前,服务器也会停止提供流量。

I was wondering if there are any ways I can configure the servers to keep receiving traffic while it's shutting down.我想知道是否有任何方法可以配置服务器以在关闭时继续接收流量。

Is there a way to still be able to reach the server while it's gracefully shutting down?有没有办法在服务器正常关闭时仍然能够访问服务器?

Yes, configure your Service with:是的,使用以下配置您的服务:

spec.publishNotReadyAddresses: true

Then your pods will receive traffic even though they are in a Terminating state, as the Kubernetes docs explain:然后,您的 pod 将接收流量,即使它们位于Terminating state 中,正如Kubernetes 文档解释的那样:

publishNotReadyAddresses, when set to true, indicates that DNS implementations must publish the notReadyAddresses of subsets for the Endpoints associated with the Service.当设置为 true 时,publishNotReadyAddresses 表示 DNS 实现必须为与服务关联的端点发布子集的 notReadyAddresses。 The default value is false.默认值为假。 The primary use case for setting this field is to use a StatefulSet's Headless Service to propagate SRV records for its Pods without respect to their readiness for purpose of peer discovery.设置此字段的主要用例是使用 StatefulSet 的 Headless Service 为其 Pod 传播 SRV 记录,而不考虑它们是否已准备好用于对等点发现。

Note that spec.publishNotReadyAddresses only works in Kubernetes v1.11+ .请注意, spec.publishNotReadyAddresses仅适用于 Kubernetes v1.11+ In older clusters, use the service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" annotation to get a similar behaviour .在较旧的集群中,使用service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"注释来获得类似的行为

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

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