ReadinessProbe 和 livenessProbe 可以使用类似探测方式,只是对 Pod 的处置方式不同:
readinessProbe 当检测失败后,将 Pod 的 IPort 从对应的 EndPoint 列表中删除。
livenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启计谋来决定作出对应的步伐。
Pod 探针使用示例
livenessProbe:用于探测容器是否运行。如果存活探测失败,则 kubelet 会杀死容器,并且容器将受到其重启计谋的影响决定是否重启。如果容器不提供存活探针,则默认状态为 Success。
readinessProbe:一般用于探测容器内的程序是否健康,容器是否准备好服务请求。如果停当探测失败,endpoint 将从与 Pod 匹配的所有 Service 的端点中删除该 Pod 的 IP 地址。初始延迟之前的停当 状态默认为 Failure。如果容器不提供停当探针,则默认状态为 Success。
startupProbe: 探测容器中的应用是否已经启动。如果提供了启动探测(startup probe),则禁用所有其他探测,直到它成功为止。如果启动探测失败,kubelet 将杀死容器,容器服从其重启计谋进行重启。 如果容器没有提供启动探测,则默认状态为成功 Success。
可以自界说在 pod 启动时是否执行这些检测,如果不设置,则检测效果均默认为通过,如果设置, 则序次为 startupProbe>readinessProbe>livenessProbe。
为什么要用 startupProbe?
在 k8s 中,通过控制器管理 pod,如果更新 pod 的时候,会创建新的 pod,删除老的 pod,但是如果新的 pod 创建了,pod 里的容器还没完成初始化,老的 pod 就被删除了,会导致访问 service 大概 ingress 时候,访问到的 pod 是有题目标,所以 k8s 就加入了一些存活性探针:livenessProbe、停当性探针 readinessProbe 以及启动探针 startupProbe。
startupProbe 是在 k8s v1.16 加入了 alpha 版,官方对其作用的解释是:
Indicates whether the application within the Container is started. All other probes are disabled if a startup probe is provided, until it succeeds. If the startup probe fails, the kubelet kills the Container, and the Container is subjected to its restart policy. If a Container does not provide a startup probe, the default state is Success
LivenessProbe 会导致 pod 重启,ReadinessProbe 只是不提供服务。我们最初的理解是 LivenessProbe 会在 ReadinessProbe 成功后开始查抄,但究竟并非云云。
kubelet 使用存活探测器来知道什么时候要重启容器。例如,存活探测器可以捕捉到死锁(应用程序在运行,但是无法继承执行背面的步骤)。这样的环境下重启容器有助于让应用程序在有题目标环境下可用。
kubelet 使用停当探测器可以知道容器什么时候准备好了并可以开始担当请求流量, 当一个 Pod 内的所有容器都准备好了,才气把这个 Pod 看作停当了,这种信号的一个用途就是控制哪个 Pod 作为 Service 的后端,在 Pod 还没有准备好的时候,会从 Service 的负载均衡器中被剔除的。
kubelet 使用启动探测器(startupProbe)可以知道应用程序容器什么时候启动了。如果配置了这类探测器,就可以控制容器在启动成功后再进行存活性和停当查抄,确保这些存活,停当探测器不会影响应用程序的启动。这可以用于对慢启动容器进行存活性检测,制止它们在启动运行之前就被杀掉。
真正的启动序次
官方文档:Caution: Liveness probes do not wait for readiness probes to succeed. If you want to wait before executing a liveness probe you should use initialDelaySeconds or a startupProbe.
也就是 Liveness probes 并不会比及 Readiness probes 成功之后才运行,根据上面的官方文档,Liveness 和 readiness 应该是某种并发的关系。
写在最后