k8s---pod的优雅退出流程(prestop和terminationGracePeriodSeconds)

在k8s中我们在部署更新服务的时候,旧的pod会被杀掉,新的pod会生成并接替工作。但是在这个交接工作过程中旧的pod有一个很长的操作,我们想在这个操作成功后再杀掉这个pod。不然的话可能会丢失一定的流量,或者外界无法感知到该pod被杀死

Pod终止过程

终止过程主要分为如下几个步骤:

  1. 用户发出删除 pod 命令
  2. K8S 会给旧POD发送SIGTERM信号;将 pod 标记为“Terminating”状态;pod 被视为“dead”状态,此时将不会有新的请求到达旧的pod;
  3. 并且等待宽限期(terminationGracePeriodSeconds 参数定义,默认情况下30秒)这么长的时间
  4. 第三步同时运行,监控到 pod 对象为“Terminating”状态的同时启动 pod 关闭过程
  5. 第三步同时进行,endpoints 控制器监控到 pod 对象关闭,将pod与service匹配的 endpoints 列表中删除
  6. 如果 pod 中定义了 preStop 处理程序,则 pod 被标记为“Terminating”状态时以同步的方式启动执行;若宽限期结束后,preStop 仍未执行结束,第二步会重新执行并额外获得一个2秒的小宽限期(最后的宽限期,所以定义prestop 注意时间,和terminationGracePeriodSeconds 参数配合使用),
  7. Pod 内对象的容器收到 TERM 信号
  8. 宽限期结束之后,若存在任何一个运行的进程,pod 会收到 SIGKILL 信号
  9. Kubelet 请求 API Server 将此 Pod 资源宽限期设置为0从而完成删除操作

pod.spec.terminationGracePeriodSeconds

[root@k8s-master ~]# kubectl explain pod.spec.terminationGracePeriodSeconds
KIND:     Pod
VERSION:  v1

FIELD:    terminationGracePeriodSeconds <integer>

DESCRIPTION:
     Optional duration in seconds the pod needs to terminate gracefully. May be
     decreased in delete request. Value must be non-negative integer. The value
     zero indicates stop immediately via the kill signal (no opportunity to shut
     down). If this value is nil, the default grace period will be used instead.
     The grace period is the duration in seconds after the processes running in
     the pod are sent a termination signal and the time when the processes are
     forcibly halted with a kill signal. Set this value longer than the expected
     cleanup time for your process. Defaults to 30 seconds.

pod.spec.containers.lifecycle.preStop

[root@k8s-master ~]# kubectl explain pod.spec.containers.lifecycle.preStop
KIND:     Pod
VERSION:  v1

RESOURCE: preStop <Object>

DESCRIPTION:
     PreStop is called immediately before a container is terminated due to an
     API request or management event such as liveness/startup probe failure,
     preemption, resource contention, etc. The handler is not called if the
     container crashes or exits. The reason for termination is passed to the
     handler. The Pod's termination grace period countdown begins before the
     PreStop hooked is executed. Regardless of the outcome of the handler, the
     container will eventually terminate within the Pod's termination grace
     period. Other management of the container blocks until the hook completes
     or until the termination grace period is reached. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

     Handler defines a specific action that should be taken

FIELDS:
   exec <Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.

   httpGet      <Object>
     HTTPGet specifies the http request to perform.

   tcpSocket    <Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

posted @ 2022-04-27 11:26  du-z  阅读(5809)  评论(0编辑  收藏  举报