It is possible pod in kubernetes terminate before service process success?

huangapple go评论101阅读模式
英文:

It is possible pod in kubernetes terminate before service process success?

问题

嗨,我有关于使用Kubernetes容器的Golang微服务的问题。例如,我有两个名为A和B的代码部署。

  1. A有一个Pod发送请求,在B服务中创建文档。
  2. B有一个自动缩放配置,可以在1-5个Pod之间等待来自A的请求,并处理在多个集合中创建文档的过程。

有时,服务A会从服务B收到"EOF"错误,我去检查服务B的日志,但找不到导致Pod终止或出现crashloopbackoff状态的错误。看起来服务B只是停止运行这个进程。

我想知道"EOF"错误的原因是否与B服务中的自动缩放配置有关。在我理解中,对B服务的高流量应该将Pod扩展到5个Pod,但当流量减少时,Pod也必须缩减。

如果在Pod中有任何正在运行的进程,在成功处理之前就会被终止,这种情况是否可能发生?

英文:

Hi I have question about golang micro service via kubernetes container. Example I have 2 deploy of code name A and B.

  1. A have 1 pod do a sent request to create document in B service.
  2. B have autoscaling config between 1 - 5 pod waiting for request from A and process to create a document in many collection.

Sometime service A have error "EOF" from service B and I go to check log in service B couldn't find error that makes pod terminate or crashloopbackoff status from kubectl terminal it like service B just stop running this process.

I wonder cause of error "EOF" is about autoscaling config in B service in my understanding high traffic to B service have to scale up pod to 5 pod but when traffic go down pod must scale down too.

It is possible if any process working in pod that would to scale down it terminated before process success?

答案1

得分: 1

当Pod被终止(由于缩容),它们会收到一个终止信号并从负载均衡器中注销。在你的情况下,问题可能是正在运行的进程立即被终止并关闭连接。你可以尝试通过为容器添加一个preStop生命周期钩子来规避这个问题,即在Pod被终止之前给正在运行的进程一些时间来完成。

  lifecycle:
    preStop:
      exec:
        command:
          - "sleep"
          - "10"

通过这个钩子,在缩容时,Pod将立即从负载均衡器中注销,防止它接收到进一步的请求,并执行preStop生命周期钩子。在这种情况下,它只会等待10秒钟。只有在钩子执行完成后,容器才会收到终止信号。

https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/

英文:

When pods are terminated (due to scale down), they receive a term signal and are deregistered from the load balancer. The problem in your case might be, that running processes are terminated imediatelly and connections get closed. You can try to circumvent the problem with a preStop lifecycle hook for your container, i.e. give your running processes time to finish before the pod gets terminated.

  lifecycle:
    preStop:
      exec:
        command:
          - "sleep"
          - "10"

With this hook, on scale down the pod will be deregistered from the load balancer imediately, preventing it from receiving further requests, and the preStop lifecycle hook will be executed. In this case it just waits 10 seconds. Only after the hook is finished, the container will receive the term signal.

https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/

答案2

得分: 1

确保你的服务B能够进行优雅关闭。

在缩容或删除时,Pod首先会收到一个TERM信号。然后,在一个优雅期限之后,如果需要,它会发送一个KILL信号。

你的服务应该响应TERM信号,并停止所有内部进程(在你的情况下是goroutines),然后终止。如果你能够做到这一点,就不需要发送KILL信号。

这在Pod生命周期文档中有描述。

英文:

Make sure your service B can do a graceful shutdown.

On scale down, or delete, the pod will first receive a TERM signal. Then, after a grace period, it will send a KILL if needed.

Your service should respect TERM and stop all internal processes (goroutines in your case) and then terminate. If you manage there will be no need for a KILL signal.

It's described in the Pod Lifecycle documentation

huangapple
  • 本文由 发表于 2022年11月28日 15:02:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/74597046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定