Pod探针

针对运行中的容器,kubelet 可以选择是否执行以下三种探针,以及如何针对探测结果作出反应:

  • livenessProbe:指示容器是否正在运行。如果存活态探测失败,则 kubelet 会杀死容器, 并且容器将根据其重启策略决定未来。如果容器不提供存活探针, 则默认状态为 Success。如果容器中的进程能够在遇到问题或不健康的情况下自行崩溃,则不一定需要存活态探针; kubelet 将根据 Pod 的restartPolicy 自动执行修复操作。如果你希望容器在探测失败时被杀死并重新启动,那么请指定一个存活态探针, 并指定restartPolicy 为 “Always“ 或 “OnFailure“。

  • readinessProbe:指示容器是否准备好为请求提供服务。如果就绪态探测失败, 端点控制器将从与 Pod 匹配的所有服务的端点列表中删除该 Pod 的 IP 地址。 初始延迟之前的就绪态的状态值默认为 Failure。 如果容器不提供就绪态探针,则默认状态为 Success。如果要仅在探测成功时才开始向 Pod 发送请求流量,请指定就绪态探针。 在这种情况下,就绪态探针可能与存活态探针相同,但是规约中的就绪态探针的存在意味着 Pod 将在启动阶段不接收任何数据,并且只有在探针探测成功后才开始接收数据。


  • startupProbe: 指示容器中的应用是否已经启动。如果提供了启动探针,则所有其他探针都会被 禁用,直到此探针成功为止。如果启动探测失败,kubelet 将杀死容器,而容器依其 重启策略进行重启。 如果容器没有提供启动探测,则默认状态为 Success。对于所包含的容器需要较长时间才能启动就绪的 Pod 而言,启动探针是有用的。 你不再需要配置一个较长的存活态探测时间间隔,只需要设置另一个独立的配置选定, 对启动期间的容器执行探测,从而允许使用远远超出存活态时间间隔所允许的时长。

存活探针

  1. [root@clientvm ~]# cat liveness-pod.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. labels:
  6. test: liveness
  7. name: liveness-exec
  8. spec:
  9. containers:
  10. - name: liveness
  11. image: busybox
  12. imagePullPolicy: IfNotPresent
  13. args:
  14. - /bin/sh
  15. - -c
  16. - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
  17. livenessProbe:
  18. exec:
  19. command:
  20. - cat
  21. - /tmp/healthy
  22. initialDelaySeconds: 5
  23. periodSeconds: 5

periodSeconds 字段指定了 kubelet 应该每 5 秒执行一次存活探测。
nitialDelaySeconds 字段告诉 kubelet 在执行第一次探测前应该等待 5 秒。
kubelet 在容器内执行命令 cat /tmp/healthy 来进行探测。 如果命令执行成功并且返回值为 0,kubelet 就会认为这个容器是健康存活的。
这个容器生命的前 30 秒, /tmp/healthy 文件是存在的。 所以在这最开始的 30 秒内,执行命令 cat /tmp/healthy 会返回成功代码。 30 秒之后,执行命令 cat /tmp/healthy 就会返回失败代码。

创建Pod,观察状态

  1. [root@clientvm ~]# kubectl apply -f liveness-pod.yaml -n mytest
  2. [root@clientvm ~]# kubectl get pod -n mytest --watch
  3. NAME READY STATUS RESTARTS AGE
  4. busybox 1/1 Running 43 4d23h
  5. dns 1/1 Running 42 4d21h
  6. liveness-exec 1/1 Running 0 19s
  7. liveness-exec 1/1 Running 1 75s
  8. [root@clientvm ~]# kubectl describe pod -n mytest liveness-exec
  9. ......
  10. Events:
  11. Type Reason Age From Message
  12. ---- ------ ---- ---- -------
  13. Normal Scheduled 2m11s default-scheduler Successfully assigned mytest/liveness-exec to worker2.example.com
  14. Normal Pulled 57s (x2 over 2m11s) kubelet Container image "busybox" already present on machine
  15. Normal Created 56s (x2 over 2m11s) kubelet Created container liveness
  16. Normal Started 56s (x2 over 2m10s) kubelet Started container liveness
  17. Warning Unhealthy 12s (x6 over 97s) kubelet Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
  18. Normal Killing 12s (x2 over 87s) kubelet Container liveness failed liveness probe, will be restarted

存活探针支持exec,httpGet,tcpSocket等多种不同的探测方式及其他相关参数配置:

  1. [root@clientvm ~]# kubectl explain Pod.spec.containers.livenessProbe
  2. KIND: Pod
  3. VERSION: v1
  4. RESOURCE: livenessProbe <Object>
  5. DESCRIPTION:
  6. Periodic probe of container liveness. Container will be restarted if the
  7. probe fails. Cannot be updated. More info:
  8. https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
  9. Probe describes a health check to be performed against a container to
  10. determine whether it is alive or ready to receive traffic.
  11. FIELDS:
  12. exec <Object>
  13. One and only one of the following should be specified. Exec specifies the
  14. action to take.
  15. failureThreshold <integer>
  16. Minimum consecutive failures for the probe to be considered failed after
  17. having succeeded. Defaults to 3. Minimum value is 1.
  18. httpGet <Object>
  19. HTTPGet specifies the http request to perform.
  20. initialDelaySeconds <integer>
  21. Number of seconds after the container has started before liveness probes
  22. are initiated. More info:
  23. https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
  24. periodSeconds <integer>
  25. How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
  26. value is 1.
  27. successThreshold <integer>
  28. Minimum consecutive successes for the probe to be considered successful
  29. after having failed. Defaults to 1. Must be 1 for liveness and startup.
  30. Minimum value is 1.
  31. tcpSocket <Object>
  32. TCPSocket specifies an action involving a TCP port. TCP hooks not yet
  33. supported
  34. timeoutSeconds <integer>
  35. Number of seconds after which the probe times out. Defaults to 1 second.
  36. Minimum value is 1. More info:
  37. https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

就绪探针

  1. [root@clientvm ~]# cat readiness-pod.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. labels:
  6. test: liveness
  7. name: readiness-exec
  8. spec:
  9. containers:
  10. - name: liveness
  11. image: busybox
  12. imagePullPolicy: IfNotPresent
  13. args:
  14. - /bin/sh
  15. - -c
  16. - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
  17. readinessProbe:
  18. exec:
  19. command:
  20. - cat
  21. - /tmp/healthy
  22. initialDelaySeconds: 5
  23. periodSeconds: 5
  1. [root@clientvm ~]# kubectl get pod -w
  2. NAME READY STATUS RESTARTS AGE
  3. readiness-exec 1/1 Running 0 25s
  4. readiness-exec 0/1 Running 0 43s
  1. [root@clientvm ~]# kubectl describe pod readiness-exec
  2. ......
  3. Events:
  4. Type Reason Age From Message
  5. ---- ------ ---- ---- -------
  6. Normal Scheduled 68s default-scheduler Successfully assigned default/readiness-exec to worker2.example.com
  7. Normal Pulled 68s kubelet Container image "busybox" already present on machine
  8. Normal Created 68s kubelet Created container liveness
  9. Normal Started 67s kubelet Started container liveness
  10. Warning Unhealthy 1s (x8 over 36s) kubelet Readiness probe failed: cat: can't open '/tmp/healthy': No such file or directory