即使 Kubernetes 就绪探测失败,Pod 也会接收流量
Pod receives traffic even Kubernetes readiness probe fails
我有一个应用程序,它为 REST 请求提供服务器并且还在监听 Kafka 主题。
我将应用程序部署到 Kubernetes 并像这样配置就绪探测器
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
基本上按照[configure-liveness-readiness-startup-probes]
的说明
部署完成后,我可以看到 pod 就绪探测失败
Readiness probe failed: cat: can't open '/tmp/healthy': No such file or directory
这是意料之中的。然后给主题发了一条kafka消息。我观察到
1) kafka 消息已被我的应用程序消费并保存到数据库中。
2) 其余api无法访问
我假设如果 pod 的就绪探测失败,应用程序既不能接收 kafka 消息也不能接收 rest 请求。但是为什么在我的测试中,REST请求和Kafka消息的处理方式不同。
根据 Kubernete 文档:
The kubelet uses readiness probes to know when a Container is ready to start accepting traffic
但是并没有说清楚它到底是指什么样的流量。
如果就绪探测失败,kubernetes 是否仅限制到 pod 的 http 流量但不限制 tcp 流量(因为 Kafka 在 tcp 上工作)?
我的实际意图是让我的服务应用程序(kafka 消费者)能够控制何时接收 kafka 消息(以及 REST 请求)。例如。如果有大量操作,我的服务将删除 /tmp/healthy 文件,从而使 pod 无法接收 kafka 消息和 Rest 请求。当繁重的操作完成后,应用程序写入健康文件,使 pod 准备好接收消息。
更多信息,在我的测试中,kubernetes 版本是 v1.14.3,kafka broker 运行 在 kubernetes 之外的单独虚拟机中。
这是两个截然不同的事情:
- 接收请求:外部服务正在发送请求并期待响应。
- 正在发送请求: 您的服务正在发送请求并等待响应。
ReadinessProbe
当 ReadinessProbe 失败时,不会将新请求路由到 pod。
卡夫卡消费者
如果您的 pod 是 Kafka 消费者,那么您的 pod 正在向 Kafka 初始化请求,以从 话题.
检查所需目录
can't open '/tmp/healthy': No such file or directory
如果您的服务需要目录 /tmp/healthy
才能正常工作,您的服务应该在启动时检查它,如果所需的目录不存在,则 exit(1)
(崩溃并显示错误消息)可用。这应该在连接到 Kafka 之前完成。如果您的应用程序不断使用该目录,例如写入它,任何操作错误代码都应该被检查并正确处理 - 根据您的情况记录和崩溃。
消费 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). E.g. 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.
Kafka 消费者 poll Kafka 获取更多数据,只要消费者需要。换句话说,Kafka 消费者 会在准备好获取更多数据时请求 获取更多数据。
消费者代码示例:
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
// process your records
}
}
记住 commit
您已经 处理过 的记录,这样消息就不会被处理多次,例如撞车后。
我有一个应用程序,它为 REST 请求提供服务器并且还在监听 Kafka 主题。 我将应用程序部署到 Kubernetes 并像这样配置就绪探测器
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5
基本上按照[configure-liveness-readiness-startup-probes]
的说明部署完成后,我可以看到 pod 就绪探测失败
Readiness probe failed: cat: can't open '/tmp/healthy': No such file or directory
这是意料之中的。然后给主题发了一条kafka消息。我观察到
1) kafka 消息已被我的应用程序消费并保存到数据库中。
2) 其余api无法访问
我假设如果 pod 的就绪探测失败,应用程序既不能接收 kafka 消息也不能接收 rest 请求。但是为什么在我的测试中,REST请求和Kafka消息的处理方式不同。
根据 Kubernete 文档:
The kubelet uses readiness probes to know when a Container is ready to start accepting traffic
但是并没有说清楚它到底是指什么样的流量。 如果就绪探测失败,kubernetes 是否仅限制到 pod 的 http 流量但不限制 tcp 流量(因为 Kafka 在 tcp 上工作)?
我的实际意图是让我的服务应用程序(kafka 消费者)能够控制何时接收 kafka 消息(以及 REST 请求)。例如。如果有大量操作,我的服务将删除 /tmp/healthy 文件,从而使 pod 无法接收 kafka 消息和 Rest 请求。当繁重的操作完成后,应用程序写入健康文件,使 pod 准备好接收消息。
更多信息,在我的测试中,kubernetes 版本是 v1.14.3,kafka broker 运行 在 kubernetes 之外的单独虚拟机中。
这是两个截然不同的事情:
- 接收请求:外部服务正在发送请求并期待响应。
- 正在发送请求: 您的服务正在发送请求并等待响应。
ReadinessProbe
当 ReadinessProbe 失败时,不会将新请求路由到 pod。
卡夫卡消费者
如果您的 pod 是 Kafka 消费者,那么您的 pod 正在向 Kafka 初始化请求,以从 话题.
检查所需目录
can't open '/tmp/healthy': No such file or directory
如果您的服务需要目录 /tmp/healthy
才能正常工作,您的服务应该在启动时检查它,如果所需的目录不存在,则 exit(1)
(崩溃并显示错误消息)可用。这应该在连接到 Kafka 之前完成。如果您的应用程序不断使用该目录,例如写入它,任何操作错误代码都应该被检查并正确处理 - 根据您的情况记录和崩溃。
消费 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). E.g. 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.
Kafka 消费者 poll Kafka 获取更多数据,只要消费者需要。换句话说,Kafka 消费者 会在准备好获取更多数据时请求 获取更多数据。
消费者代码示例:
while (true) {
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
// process your records
}
}
记住 commit
您已经 处理过 的记录,这样消息就不会被处理多次,例如撞车后。