Kubernetes 0-1 Pod中的livenessProbe和readinessProbe解读

本文详细解读了Kubernetes (K8S) 中的livenessProbe和readinessProbe探针,包括它们的作用、定义参数、示例及在服务负载均衡中的应用。通过实例演示,展示了如何配置和理解这两种健康检查机制。

写在前面

K8S对Pod的健康状态可以通过两类探针来检查:livenessProbe和readinessProbe,kubelet通过定期执行这两类探针来诊断容器的健康状况。

livenessProbe简介

存活指针,判断Pod(中的应用容器)是否健康,可以理解为健康检查。我们使用livenessProbe来定期的去探测,如果探测成功,则Pod状态可以判定为Running;如果探测失败,可kubectl会根据Pod的重启策略来重启容器。

如果未给Pod设置livenessProbe,则默认探针永远返回Success。

当我们执行kubectl get pods命令,输出信息中STATUS一列我们可以看到Pod是否处于Running状态。

readinessProbe简介

就绪指针,就绪的意思是已经准备好了,Pod的就绪我们可以理解为这个Pod可以接受请求和访问。我们使用readinessProbe来定期的去探测,如果探测成功,则Pod 的Ready状态判定为True;如果探测失败,Pod的Ready状态判定为False。

与livenessProbe不同的是,kubelet不会对readinessProbe的探测情况有重启操作。

当我们执行kubectl get pods命令,输出信息中READY一列我们可以看到Pod的READY状态是否为True。

定义参数

livenessProbe和readinessProbe的定义参数是一致的,可以通过kubectl explain pods.spec.containers.readinessProbekubectl explain pods.spec.containers.livenessProbe命令了解:

就绪探针的几种类型:

httpGet

向容器发送Http Get请求,调用成功(通过Http状态码判断)则确定Pod就绪;

使用方式:

livenessProbe:
  httpGet:
    path: /app/healthz
    port: 80

exec

在容器内执行某命令,命令执行成功(通过命令退出状态码为0判断)则确定Pod就绪;

使用方式:

livenessProbe:
  exec:
    command:
    - cat 
    - /app/healthz

tcpSocket

打开一个TCP连接到容器的指定端口,连接成功建立则确定Pod就绪。

使用方式:

livenessProbe:
  tcpSocket:
    port: 80

一般就绪探针会在启动容器一段时间后才开始第一次的就绪探测,之后做周期性探测。所以在定义就绪指针时,会给以下几个参数:

  • initialDelaySeconds:在初始化容器多少秒后开始第一次就绪探测;
  • timeoutSeconds:如果该次就绪探测超过多少秒后还未成功,判定为超时,该次探测失败,Pod不就绪。默认值1,最小值1;
  • periodSeconds:如果Pod未就绪,则每隔多少秒周期性的做就绪探测。默认值10,最小值1;
  • failureThreshold:如果容器之前探测成功,后续连续几次探测失败,则确定容器未就绪。默认值3,最小值1;
  • successThreshold:如果容器之前探测失败,后续连续几次探测成功,则确定容器就绪。默认值1,最小值1。

使用示例

目前我在docker hub有一个测试镜像:med1tator/helloweb:v1,容器启动后,有一个健康检查路由/healthz/return200,访问该路由状态码返回200;有一个检查路由/health/return404,访问该路由状态码返回404。

readinessProbe示例

在实验之前先了解一下Pod和Service的负载均衡关系:在K8S中,Service作为Pod的负载均衡器,是通过Label Selector匹配Pod的。但是这句话没有说完整,因为还有一个必要条件:Pod当前已经就绪。也就是说,Service通过Label Selector匹配当前就绪的Pod,还未就绪的Pod就算labelSelector匹配上了,也不会出现在Service的endpoints中,请求是不会被转发过去的,如下图示例。

示例说明:我们使用med1tator/helloweb:v1镜像启动三个Pod,三个Pod的Label都设置成一样,为了使Service匹配到;三个Pod其中两个readinessProbe使用httpGet探测/health/return200,模拟探测成功,一个readinessProbe使用httpGet探测/health/return404,模拟探测失败。

编写我们的helloweb-readinessProbe.yaml文件如下:

apiVersion: v1
kind: Pod
metadata:
  name: helloweb1
  labels:
    app: helloweb
spec:
  containers:
    - name: helloweb
      image: med1tator/helloweb:v1
      readinessProbe:
          httpGet:
            path: /healthz/return200
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 10     
      ports:
        - containerPort: 80

---
apiVersion: v1
kind: Pod
metadata:
  name: helloweb2
  labels:
    app: helloweb
spec:
  containers:
    - name: helloweb
      image: med1tator/helloweb:v1
      readinessProbe:
          httpGet:
            path: /healthz/return200
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 10     
      ports:
        - containerPort: 80

---
apiVersion: v1
kind: Pod
metadata:
  name: helloweb3
  labels:
    app: helloweb
spec:
  containers:
    - name: helloweb
      image: med1tator/helloweb:v1
      readinessProbe:
          httpGet:
            path: /healthz/return404
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 10     
      ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name:  helloweb
spec:
  selector:
    app:  helloweb
  type:  ClusterIP
  ports:
  - name:  http
    port:  80
    targetPort:  80

运行命令部署Pod和Service:

kubectl apply -f helloweb-readinessProbe.yaml

之后,我们查看Pod的就绪情况:

可以看到Pod只有helloweb1和helloweb2当前使处于READY(就绪)的状态,helloweb3尚未Ready,我们接着查看helloweb service的endpoints:

可以看到Service的EndPoints只将helloweb1、helloweb2 pod的IP负载上了。

查看日志访问情况:

可以看到每隔10秒(readniessProbe.periodSeconds默认10s)就会做一次就绪探测。

livenessProbe示例

编写我们的helloweb-livenessProbe.yaml文件如下:

apiVersion: v1
kind: Pod
metadata:
  name: helloweb4
  labels:
    app: helloweb
spec:
  containers:
    - name: helloweb
      image: med1tator/helloweb:v1
      livenessProbe:
          httpGet:
            path: /healthz/return200
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 10     
      ports:
        - containerPort: 80

---
apiVersion: v1
kind: Pod
metadata:
  name: helloweb5
  labels:
    app: helloweb
spec:
  containers:
    - name: helloweb
      image: med1tator/helloweb:v1
      livenessProbe:
          httpGet:
            path: /healthz/return404
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 10     
      ports:
        - containerPort: 80

运行命令部署Pod和Service:

kubectl apply -f helloweb-livenessProbe.yaml

之后,我们查看Pod的就绪情况:

可以看到helloweb4的STATUS状态为Running,而helloweb5的STATUS状态最终变为CrashLoopBackOff,并且一直在重启。

相信到这里,你已经对readniessProbelivenessProbe有一个清晰的了解了。

### readinessProbe 在优雅关闭过程中的作用 在 Kubernetes 中,`readinessProbe` 的主要职责是判断容器是否已经准备好接收流量。当应用开始执行优雅关闭流程时,Kubernetes 会将该 Pod 从服务的端点列表中移除,以防止新请求被路由到正在关闭的实例上[^1]。这一行为确保了系统不会继续向即将终止的 Pod 发送新的请求,从而避免服务中断或请求失败。 在 Spring Boot 应用中,若启用了内置的优雅关闭机制(通过 `server.shutdown=graceful` 配置),则在接收到 `SIGTERM` 信号后,应用服务器会停止接受新的 HTTP 请求,同时等待正在进行的请求处理完成。在此期间,`readinessProbe` 通常会失败,因为健康检查接口可能返回非就绪状态,这触发了 Kubernetes 将该 Pod 标记为不就绪,并从负载均衡池中剔除[^1]。 ### livenessProbe 对优雅关闭的影响 与 `readinessProbe` 不同,`livenessProbe` 用于检测容器是否仍在运行。如果探针失败,Kubernetes 会重启容器。然而,在优雅关闭过程中,Spring Boot 应用可能会进入一个中间状态,其中虽然不再接受新请求,但仍在处理旧请求。此时,如果 `livenessProbe` 超时时间设置得太短,可能导致 Kubernetes 过早地认为容器已经“死亡”,并强制终止进程,从而打断优雅关闭流程。 为了避免这种情况,应合理配置 `livenessProbe` 的超时时间探测频率。例如,可以适当增加 `initialDelaySeconds` `failureThreshold`,确保在优雅关闭期间探针不会频繁失败。此外,还应确保 `livenessProbe` 探测路径不会依赖正在关闭的服务组件,以免误判容器状态。 ### 示例:优化的探针配置 以下是一个适用于 Spring Boot 应用的探针配置示例: ```yaml readinessProbe: httpGet: path: /actuator/health port: 8080 initialDelaySeconds: 10 periodSeconds: 5 timeoutSeconds: 2 successThreshold: 1 failureThreshold: 3 livenessProbe: httpGet: path: /actuator/info port: 8080 initialDelaySeconds: 30 periodSeconds: 10 timeoutSeconds: 5 successThreshold: 1 failureThreshold: 5 ``` 上述配置中,`readinessProbe` 使用 `/actuator/health` 端点来判断应用是否就绪,而 `livenessProbe` 则使用 `/actuator/info` 来减少对主业务逻辑的依赖。这样即使在关闭阶段,只要应用仍在运行,探针仍能成功响应。 ### 总结 结合 `readinessProbe` `livenessProbe` 的合理配置,可以在 Spring Boot 应用优雅关闭过程中实现更稳定的行为。`readinessProbe` 确保新请求不会被发送到正在退出的 Pod,而 `livenessProbe` 的设计应避免干扰正常的关闭流程。通过这种方式,可以提升系统的健壮性用户体验。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值