5

Kubernetes -- pod的关闭流程

 2 years ago
source link: https://segmentfault.com/a/1190000040817783
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

APIServer接收到删除Pod的请求后:

  • 首先,修改etcd中的状态;
  • 然后,把删除pod的事件,通知给kubelet和endpoint-controller;

    • kubelet: 负责pod资源的删除;
    • endpoint-controller: 负责endpoint资源的删除;
    • 两个组件并行执行;

image.png

Kubelet处理pod关闭

kubelet关闭pod的时候,会关闭pod中的每一个容器;kubelet给容器一定的时间(TerminationGracePeriod)优雅的停止。

kubelet终止容器的过程:

  • 执行preStop,等待它执行完毕;
  • 向容器的进程发送SIGTERM;
  • 等待容器优雅的关闭或超时(terminationGracePeriod);
  • 若关闭超时,则发送SIGKILL强制关闭;

image.png

endpoint-controller处理pod关闭

endpoint-controller接收到pod删除的通知时,向APIServer发送请求,修改svc的endpoints对象,从pod所在svc中删除该pod的endpoint。

然后,APIServer通知节点上的kube-proxy组件,kube-proxy会修改本机的iptables/ipvs规则,将该endpoint移除。

Kubelet和endpoint-controller并行执行的问题

kube-proxy可能由于过载处理请求变慢,会出现:

kubelet已经把容器删除,但kube-proxy还未更新iptables。

这种情况下,流量还会被分发到对应的endpoint,但是pod已删除,客户端返回"连接拒绝"之类的错误。

目前的解决方法,在pod的preStop中sleep一段时间,等待kube-proxy更新iptables完毕:

lifecycle:
  preStop:
    exec:
      command: [ "sh", "-c", "sleep 10" ]
  1. kubernetes in action

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK