1. 概述

Kubernetes 提供了三种常见的容器健康检查机制:

  • startupProbe:检查容器是否成功启动
  • livenessProbe:判断容器是否存活
  • readinessProbe:确认容器是否准备好接收流量

这些探针(Probe)共同作用,确保 Pod 内容器的健康状态和可用性。

在本教程中,我们将通过实际配置不同场景,深入理解这三种探针的工作原理和使用方式。

2. 基本概念

当 Pod 启动时,Kubernetes 会先使用 startupProbe 判断容器是否已经启动成功。一旦启动完成,livenessProbe 会持续检查容器是否处于运行状态;而 readinessProbe 则决定容器是否可以接收外部请求。

简要说明每种探针的作用:

探针类型 作用说明
startupProbe 确保容器在启动阶段能够成功运行
livenessProbe 检查容器是否仍在运行,若失败则触发重启
readinessProbe 检查容器是否准备好接收流量,若失败则从服务中移除

行为差异

  • 如果 startupProbe 失败,Kubernetes 会认为容器根本无法启动,直接终止流程。
  • livenessProbe 失败,Kubernetes 会尝试重启容器。
  • readinessProbe 失败,Kubernetes 会将该 Pod 从服务中摘除,但不会重启容器。当探针恢复成功后,Pod 会重新加入服务。

3. HTTP Get 探针

通过 HTTP 请求的方式,Kubernetes 可以定期访问容器中的某个路径来判断其健康状态。

配置示例如下:

readinessProbe:
  httpGet:
    path: /
    port: 80
livenessProbe:
  httpGet:
    path: /
    port: 80
startupProbe:
  httpGet:
    path: /
    port: 80

常用参数说明:

参数名 默认值 说明
initialDelaySeconds 0 容器启动后等待多少秒开始第一次探测
periodSeconds 10 探测周期(秒)
timeoutSeconds 1 探测超时时间
successThreshold 1 成功多少次才认为成功
failureThreshold 3 连续失败多少次认为失败
terminationGracePeriodSeconds 30 容器终止前等待时间

示例:Nginx Pod 配置

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx-container
      image: nginx
      ports:
        - containerPort: 80
      readinessProbe:
        httpGet:
          path: /
          port: 80
        initialDelaySeconds: 5
        periodSeconds: 10
      livenessProbe:
        httpGet:
          path: /
          port: 80
        initialDelaySeconds: 10
        periodSeconds: 15
      startupProbe:
        httpGet:
          path: /
          port: 80
        failureThreshold: 30
        periodSeconds: 10

创建 Pod 并验证状态:

kubectl apply -f nginx-pod.yaml
kubectl get pods

输出示例:

NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          3m43s

Tips:HTTP 探针返回 2xx 状态码表示健康,否则视为失败。

4. 命令探针(Command Probe)

除了 HTTP 请求,Kubernetes 还支持通过执行容器内的 shell 命令进行健康检查。

配置结构如下:

readinessProbe:
  exec:
    command:
      - "ls"
      - "/tmp"

工作原理:

  • 命令执行成功(退出码为 0):视为健康
  • 命令执行失败(非 0 退出码):视为不健康

示例:Ubuntu 容器配置

apiVersion: v1
kind: Pod
metadata:
  name: test-ubuntu
spec:
  containers:
    - name: test-ubuntu
      image: ubuntu
      command:
        - "sleep"
        - "604800"
      livenessProbe:
        exec:
          command:
            - "ps"
            - "-C"
            - "sleep"
        initialDelaySeconds: 5
        periodSeconds: 10
        successThreshold: 1
        failureThreshold: 2
      readinessProbe:
        exec:
          command:
            - "ls"
            - "/bin"
            - "/boot"
            - "/usr"
        initialDelaySeconds: 10
        periodSeconds: 15
        successThreshold: 1
        failureThreshold: 2
      startupProbe:
        exec:
          command:
            - "ls"
            - "/boot"
        initialDelaySeconds: 30
        periodSeconds: 20
        successThreshold: 1
        failureThreshold: 100

创建 Pod 并查看状态:

kubectl apply -f ubuntu-pod.yaml
kubectl get pod test-ubuntu

输出示例:

NAME          READY   STATUS    RESTARTS   AGE
test-ubuntu   1/1     Running   0          24s

⚠️ 踩坑提醒:命令探针要轻量,避免对容器性能造成影响。

5. TCP Socket 探针

对于使用 TCP 协议的服务,可以使用 TCP Socket 探针进行健康检查。

配置示例如下:

readinessProbe:
  tcpSocket:
    port: 6379
livenessProbe:
  tcpSocket:
    port: 6379
startupProbe:
  tcpSocket:
    port: 6379

示例:Redis Pod 配置

apiVersion: v1
kind: Pod
metadata:
  name: redis-server
spec:
  containers:
    - name: redis
      image: redis:latest
      ports:
        - containerPort: 6379
      readinessProbe:
        exec:
          command:
            - "redis-cli"
            - "ping"
        initialDelaySeconds: 15
        periodSeconds: 20
        timeoutSeconds: 5
      livenessProbe:
        tcpSocket:
          port: 6379
        initialDelaySeconds: 15
        periodSeconds: 20
        timeoutSeconds: 5
      startupProbe:
        tcpSocket:
          port: 6379
        initialDelaySeconds: 30
        periodSeconds: 20
        timeoutSeconds: 10

创建并验证 Pod:

kubectl apply -f redis-server-pod.yaml
kubectl get pods

输出示例:

NAME           READY   STATUS    RESTARTS   AGE
redis-server   1/1     Running   0          2m56s

Tips:TCP 探针适用于非 HTTP 服务,如数据库、消息队列等。

6. 总结

本文介绍了 Kubernetes 中三种常用的探针机制:

  • startupProbe:确保容器能顺利启动
  • livenessProbe:检测容器是否存活,失败则重启
  • readinessProbe:判断容器是否就绪,失败则从服务中移除

并通过 HTTP Get、Command、TCP Socket 三种方式配置了不同场景下的探针,帮助我们更灵活地控制 Pod 的健康状态。

推荐实践:

  • 探针应尽量轻量,避免影响容器性能
  • startupProbe 可以设置更宽松的阈值
  • livenessProbe 失败会导致容器重启,需谨慎配置
  • readinessProbe 控制服务流量,适用于灰度发布等场景

合理使用这三种探针,可以显著提升 Kubernetes 集群的稳定性和容错能力。


原始标题:Understanding livenessProbe and readinessProbe in Kubernetes