在Pod处于终止宽限期内时如何将流量从Service发送到Pod [英] How to send traffic from Service to Pod while Pod is in termination grace period

查看:155
本文介绍了在Pod处于终止宽限期内时如何将流量从Service发送到Pod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个附加了Service和Horizo​​ntalPodAutoscaler的部署(A吊舱).我希望能够控制按比例缩小的过程,并在关闭Pod之前进行一些清理.问题是,清理可能要花费大量时间,并且清理完成一些其他服务(B容器)应该能够访问试图关闭的容器.

I have a deployment (A pods) with a Service and HorizontalPodAutoscaler attached. I want to be able to control the scale down process and do some cleanup before the pod shutdown. Problem is, the cleanup can take a lot of time and for it to complete some other service (B pods) should be able to access the pod trying to shut down.

为此,我将部署A设置为具有较长的spec.terminationGracePeriodSeconds值.当Pod获得SIGTERM时,它将开始完成并结束该过程.

To accomplish this I set the deployment A to have a long spec.terminationGracePeriodSeconds value. When A pod gets the SIGTERM it starts finishing up and closing the process when it's done.

从点Pod A获得SIGTERM以来,它不再接收来自Pod B的连接,因为该服务从端点删除了它的IP,这使Pod A无法完成其清理.

From the point pod A get the SIGTERM it is no longer receives connections from pod B because the service removes it's IP from the endpoint - making it impossible for pod A to finish it's cleanup.

尝试使用ClusterIP和Headless服务,两者的行为相同.

Tried using ClusterIP and Headless services, both acts the same.

即使获得了SIGTERM,如何使服务继续向Pod A发送流量呢?我不介意来自B吊舱的请求在尝试到达A吊舱时会出错.

How can I make the service continue sending traffic to pod A even after it got the SIGTERM? I don't mind requests from B pods getting errors when trying to get to A pods.

推荐答案

通过以下方式配置服务:

Configure your Service with:

spec.publishNotReadyAddresses: true

然后,即使您的Pod处于Terminating状态,它们也会收到流量,因为

Then your pods will receive traffic even though they are in a Terminating state, as the Kubernetes docs explain:

publishNotReadyAddresses设置为true时,指示DNS实现必须发布与服务关联的端点的子集的notReadyAddresses.默认值为false.设置此字段的主要用例是使用StatefulSet的Headless Service为其Pod传播SRV记录,而无需考虑它们为对等点发现而准备就绪的情况.

publishNotReadyAddresses, when set to true, indicates that DNS implementations must publish the notReadyAddresses of subsets for the Endpoints associated with the Service. 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.

请注意,spec.publishNotReadyAddresses 仅适用于Kubernetes v1.11 + . 在较旧的群集中,使用service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"批注获取类似行为.

Note that spec.publishNotReadyAddresses only works in Kubernetes v1.11+. In older clusters, use the service.alpha.kubernetes.io/tolerate-unready-endpoints: "true" annotation to get a similar behaviour.

这篇关于在Pod处于终止宽限期内时如何将流量从Service发送到Pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆