Kubernetes Probe类型详解

Kubernetes Probe(探针)用于:

  • (startProbe)判断容器是否已经启动完成
  • (readlinessProbe)判断容器是否可对外提供服务加入/剔除service endpoint
  • (livenessProbe)判断容器是否健康

K8s 会根据 Probe 结果决定:

  • (startProbe)是否继续等待启动
  • (readlinessProbe)是否将 Pod 加入 Service Endpoint
  • (livenessProbe)是否重启容器

1.K8s Probe 三种类型

K8s 一共有三种 Probe:

Probe 作用 失败后行为 所属阶段
startupProbe 判断“是否启动完成” 超时后重启容器 启动阶段
readinessProbe 判断“是否可提供服务” 从 Service 剔除 运行阶段
livenessProbe 判断“是否还活着” 重启容器 运行阶段

2.三种Probe 生命周期关系

flowchart TD

A[容器启动]

--> B[startupProbe]

B -->|成功| C[readinessProbe开始]

C --> D[Pod加入Service]

D --> E[livenessProbe持续检测]

E -->|失败| F[重启容器]

B -->|失败次数超限| G[容器重启]

C -->|失败| H[从Service摘除]

H -->|恢复成功| D

3.startupProbe(启动探针)

3.1作用(冷启动)

用于解决:

“应用启动太慢导致 livenessProbe/readinessProbe 误杀”

这是:

  • Java
  • SpringBoot
  • Dubbo
  • Elasticsearch
  • AI模型
  • JVM冷启动

最重要的 Probe。


3.2工作阶段

仅在:

1
容器启动阶段

生效。

成功一次后:

  • startupProbe 永久停止
  • readiness/liveness 开始接管

3.3典型场景

你的日志就属于:

1
2
3
4
SpringBoot 启动慢
Dubbo初始化慢
Redis连接慢
RASP增强慢

没有 startupProbe:

1
2
3
4
5
6
7
容器还没启动完

liveness 先检查

失败

K8s kill pod

这就是你日志里的:

1
SIGTERM

根因之一。


3.4推荐配置(Java)

1
2
3
4
5
6
startupProbe:
httpGet:
path: /actuator/health
port: 8081
failureThreshold: 30 # 失败冷却30s
periodSeconds: 10

含义:

1
30 × 10 = 最长允许启动300秒

4.readinessProbe(就绪探针)

4.1作用

判断:

“当前 Pod 是否可以接流量”


4.2失败行为

不会重启容器。

而是:

1
从 Service Endpoint 删除

即:

1
不再接收请求

4.3工作阶段

应用启动成功后持续运行。


4.4典型用途

数据库未连接

1
2
3
4
5
DB断开

ready=false

从LB摘除

Dubbo 未注册完成

1
2
3
4
5
Nacos不可用

ready=false

避免流量进入

JVM FullGC

1
2
3
4
5
GC暂停严重

接口超时

ready=false

4.5推荐配置

1
2
3
4
5
6
7
8
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8081
initialDelaySeconds: 30
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3

5.livenessProbe(存活探针)

5.1作用

判断:

“进程是否已经假死”


5.2失败行为

直接:

1
2
kill container
重新拉起

5.3典型场景

死锁

1
2
线程死锁
接口永远不返回

JVM Hang

1
2
3
Safepoint卡死
GC卡死
CPU 100%

Netty事件循环阻塞

1
Dubbo/Redis线程池阻塞

5.4推荐配置

1
2
3
4
5
6
7
8
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8081
initialDelaySeconds: 120
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 5

6.为什么 startupProbe 非常关键

6.1没有 startupProbe 的问题

flowchart TD

A[SpringBoot启动80秒]
--> B[liveness 30秒开始检查]
--> C[health接口未ready]
--> D[K8s判定失败]
--> E[SIGTERM]
--> F[容器重启]
--> G[无限循环]

6.2有 startupProbe

flowchart TD

A[SpringBoot启动80秒]
--> B[startupProbe等待]
--> C[应用初始化完成]
--> D[startupProbe成功]
--> E[开始readiness/liveness]
--> F[正常提供服务]

7.Probe 支持方式

K8s 支持:

类型 说明
httpGet HTTP接口
tcpSocket TCP端口
exec 容器内命令
grpc gRPC健康检查

8.SpringBoot 最佳实践

推荐:

1
2
3
4
5
management:
endpoint:
health:
probes:
enabled: true

SpringBoot 2.3+:

自动提供:

接口 用途
/actuator/health/liveness 存活
/actuator/health/readiness 就绪

9.生产级推荐配置(Java 微服务)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
startupProbe:
httpGet:
path: /actuator/health
port: 8081
failureThreshold: 60
periodSeconds: 5

readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8081
initialDelaySeconds: 10
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3

livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8081
initialDelaySeconds: 120
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 5

10.生产经验(重要)

10.1 Java 服务必须有startupProbe

否则:

  • SpringBoot
  • Dubbo
  • ShardingSphere
  • Elasticsearch Client
  • AI模型

都容易被误杀。


10.2 liveness 不要过于激进

错误:

1
2
periodSeconds: 3
timeoutSeconds: 1

会导致:

1
GC抖动 -> 重启风暴

10.3 readiness 要比 liveness 更敏感

原因:

1
2
优先摘流量
而不是重启

这是高可用核心思想。


11.一句话理解三种 Probe

Probe 一句话
startupProbe “你启动完了吗?”
readinessProbe “你现在能接请求吗?”
livenessProbe “你是不是已经死了?”