简体   繁体   English

即使 Kubernetes 就绪探测失败,Pod 也会收到流量

[英]Pod receives traffic even Kubernetes readiness probe fails

I have one application which servers for REST request and also is listening on a Kafka topic.我有一个应用程序,它为 REST 请求提供服务器,并且还在监听 Kafka 主题。 I deployed the application to Kubernetes and configure the readiness probe like this我将应用程序部署到 Kubernetes 并像这样配置就绪探针

readinessProbe:
  exec:
    command:
    - cat
    - /tmp/healthy
  initialDelaySeconds: 5
  periodSeconds: 5

basically following the instruction from [configure-liveness-readiness-startup-probes]基本上遵循[configure-liveness-readiness-startup-probes]的说明

After deployment is done, I can see the pod readiness probe fails部署完成后,我可以看到 Pod 就绪探测失败

Readiness probe failed: cat: can't open '/tmp/healthy': No such file or directory

That is expected.这是意料之中的。 Then I sent a kafka message to the topic.然后我向该主题发送了一条kafka消息。 I observed that我观察到

1) the kafka message has been consumed by my application and saved to database. 1)我的应用程序已使用 kafka 消息并保存到数据库。
2) the rest api can't be accessed. 2)无法访问rest api。

I assumed if the pod's readiness probe is failed, the application can neither receive kafka message nor the rest request.我假设如果 pod 的就绪探测失败,应用程序既不能接收 kafka 消息,也不能接收 rest 请求。 But why in my test, the REST request and Kafka message are handled differently.但是为什么在我的测试中,REST 请求和 Kafka 消息的处理方式不同。

According to the Kubernete documentation:根据 Kubernetes 文档:

The kubelet uses readiness probes to know when a Container is ready to start accepting traffic

But it doesn't say clearly what kind of traffic it really means.但它并没有明确说明它真正意味着什么样的流量。 Does kubernetes only restrict the http traffic to the pod if readiness probe failes but not restrict tcp traffic (as Kafka is working over tcp)?如果就绪探测失败,kubernetes 是否仅将 http 流量限制到 pod,但不限制 tcp 流量(因为 Kafka 在 tcp 上工作)?

My actual intention is to make my service application (kafka consumer) able to control when to receive kafka messages (and REST request as well).我的实际意图是让我的服务应用程序(kafka 消费者)能够控制何时接收 kafka 消息(以及 REST 请求)。 Eg if there is heavy opertion, my service will delete the /tmp/healthy file and thus make the pod not ready for recieving kafka message and Rest request.例如,如果操作繁重,我的服务将删除 /tmp/healthy 文件,从而使 pod 无法准备好接收 kafka 消息和 Rest 请求。 When the heavy operation is finished, the app write the healthy file to make the pod ready for receiving message.当繁重的操作完成后,应用程序会写入健康文件以使 pod 准备好接收消息。

Some more information, in my test, the kubernetes version is v1.14.3 and kafka broker is running in a separated vm outside of kubernetes.更多信息,在我的测试中,kubernetes 版本是 v1.14.3,并且 kafka 代理在 kubernetes 之外的单独虚拟机中运行。

This is two very different things:这是两个非常不同的事情:

  • Receiving requests : An external service is sending a request and expect a response.接收请求外部服务正在发送请求并期待响应。
  • Sending requests : Your service is sending a request and waiting for a response.发送请求:您的服务正在发送请求并等待响应。

ReadinessProbe就绪探针

When a ReadinessProbe fails, no new requests will be routed to the pod .当 ReadinessProbe 失败时,不会有新的请求被路由到 pod

Kafka consumer卡夫卡消费者

If your pod is a Kafka consumer , then your pod is initializing requests to Kafka, to retrieve messages from the topic .如果您的 pod 是Kafka 消费者,那么您的pod 正在初始化对 Kafka 的请求,以从主题中检索消息。

Check for required directory检查所需目录

can't open '/tmp/healthy': No such file or directory无法打开“/tmp/healthy”:没有这样的文件或目录

If the directory /tmp/healthy is needed for your service to work correctly, your service should check for it on startup, and exit(1) (crash with an error message) if the required directory isn't available.如果您的服务需要目录/tmp/healthy才能正常工作,您的服务应在启动时检查它,如果所需目录不可用,则exit(1) (崩溃并显示错误消息)。 This should be done before connecting to Kafka.这应该在连接到 Kafka 之前完成。 If your application uses the directory continually, eg writing to it, any operations error codes should be checked and handled properly - log and crash depending on your situation.如果您的应用程序持续使用该目录,例如写入该目录,则应检查并正确处理任何操作错误代码- 根据您的情况记录和崩溃。

Consuming Kafka messages消费 Kafka 消息

My actual intention is to make my service application (kafka consumer) able to control when to receive kafka messages ( and REST request as well).我的实际意图是让我的服务应用程序(kafka 消费者)能够控制何时接收 kafka 消息(以及 REST 请求)。 Eg if there is heavy opertion, my service will delete the /tmp/healthy file and thus make the pod not ready for recieving kafka message and Rest request.例如,如果操作繁重,我的服务将删除 /tmp/healthy 文件,从而使 pod 无法准备好接收 kafka 消息和 Rest 请求。

Kafka consumers poll Kafka for more data, whenever the consumer want.每当消费者需要时,Kafka 消费者都会轮询Kafka 以获取更多数据。 In other words, the Kafka consumer ask for more data whenever it is ready for more data.换句话说,当 Kafka 消费者准备好接收更多数据时,它就会请求更多数据。

Example consumer code:示例消费者代码:

 while (true) {
     ConsumerRecords<String, String> records = consumer.poll(100);
     for (ConsumerRecord<String, String> record : records) {
         // process your records
     }
 }

Remember to commit the records that you have processed so that the messages aren't processed multiple times eg after a crash.请记住commit您已处理的记录,以便消息不会被多次处理,例如在崩溃之后。

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

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