探针决定 Pod 会不会被重启、会不会接流量;requests / limits 决定 Pod 能被调度到哪台节点、被压到极限时是限流还是被杀。两者配合,工作负载既不被误杀,也不拖垮节点。

三种探针的分工

  • livenessProbe:连续失败达到 failureThreshold,kubelet 杀掉容器并按 restartPolicy 重启。处理死锁、假死这类进程自己不会退出的情况。
  • readinessProbe:失败只把 Pod 的 IP 从匹配 Service 的 EndpointSlice 摘除,停止接收新流量,容器不重启;恢复后自动回到端点列表。
  • startupProbe:只在启动阶段执行。配置后,startup 成功前 kubelet 不执行 liveness / readiness;startup 一直失败则杀掉容器。
  • 启动耗时不可预测时,用 startupProbe 的 failureThreshold × periodSeconds 覆盖最坏启动时间,而不是调大 liveness 的等待窗口(initialDelaySeconds + failureThreshold × periodSeconds)。

四种探测方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# exec:容器内执行命令,退出码 0 成功;每次探测 fork 进程,Pod 密度高时慎用
livenessProbe:
exec: {command: ["cat", "/tmp/healthy"]}

# httpGet:200–399 成功,其余失败;scheme 默认 HTTP,path 默认 "/"
readinessProbe:
httpGet: {path: /healthz, port: 8080, httpHeaders: [{name: X-Probe, value: "on"}]}

# tcpSocket:端口能建立连接即成功;探测由 kubelet 从节点侧发起,host 写 Service 名解析不了
livenessProbe:
tcpSocket: {port: 6379}

# grpc:要求应用实现 gRPC Health Checking Protocol,返回 SERVING 才算成功
# port 必填且只能写端口号,不支持命名端口与自定义 hostname
livenessProbe:
grpc: {port: 50051, service: liveness}
  • HTTP 探测默认带 User-Agent: kube-probe/<kubelet 版本> 和 Accept: */*,可用 httpHeaders 覆盖或置空。
  • kubelet 只读响应体前 10KiB,健康检查端点应返回小体积响应,否则应用日志会出现连接重置的噪声。
  • grpc 探针在 v1.27 转为 stable;默认明文,mode: TLS 依赖较新的版本。

探针参数与默认值

1
2
3
4
5
6
7
livenessProbe:
httpGet: {path: /healthz, port: 8080}
initialDelaySeconds: 5 # 启动后延迟多久开始探测,默认 0,最小 0
periodSeconds: 10 # 探测周期,默认 10,最小 1
timeoutSeconds: 3 # 单次超时,默认 1,最小 1
successThreshold: 1 # 失败后连续成功几次才算恢复,默认 1
failureThreshold: 3 # 连续失败几次判定失败,默认 3,最小 1
  • successThreshold 在 livenessProbe 与 startupProbe 上只能是 1,只有 readiness 允许调大。
  • 定义了 startupProbe 时,liveness / readiness 的 initialDelaySeconds 从 startup 成功之后才开始计时;readiness 在初始延迟结束前按 Failure 处理。
  • 探针级 terminationGracePeriodSeconds(v1.28 GA)可覆盖 Pod 级默认 30 秒,写到 readinessProbe 上会被 API 拒绝。

requests 与 limits

1
2
3
4
5
6
7
resources:
requests: # 调度依据:scheduler 只把 Pod 放到 requests 之和装得下的节点
cpu: "250m" # 1000m = 1 核
memory: "256Mi" # Mi/Gi 是 1024 进制,M/G 是 1000 进制,别写小写 m
limits: # 下发给运行时,最终由内核 cgroup 强制执行
cpu: "500m"
memory: "512Mi"
  • 只写 limit 不写 request 时,limit 的值会被复制为 request;request 大于 limit 会被 API 拒绝(Requests cannot exceed Limits)。
  • 节点剩余可分配量装不下 requests,Pod 保持 Pending,事件为 FailedScheduling。
  • CPU 可压缩:超过 limit 只是被 throttle,容器不会被杀;CPU 打满且延迟升高先查 CPU limit。
  • 内存不可压缩:超过 limit 由内核 OOM killer 终止,原因是 OOMKilled、exit code 137;只超 request 不超 limit 通常无事,但节点内存紧张时这类 Pod 先被驱逐。

QoS 等级

判定看 Pod 内所有容器,顺序 Guaranteed → Burstable → BestEffort:

  • Guaranteed:每个容器都同时设置了 cpu、memory 的 request 与 limit,各自相等且都大于 0。
  • Burstable:不满足 Guaranteed,但至少一个容器设置了 request 或 limit。
  • BestEffort:所有容器都没设置任何 CPU / 内存的 request 或 limit。
  • 节点资源紧张时驱逐顺序为 BestEffort → Burstable → Guaranteed;Guaranteed Pod 还能配合 static CPU 管理策略使用独占核。

完整 Deployment 示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
apiVersion: apps/v1
kind: Deployment
metadata: {name: api}
spec:
replicas: 3
selector: {matchLabels: {app: api}}
template:
metadata: {labels: {app: api}}
spec:
containers:
- name: api
image: registry.example.com/api:1.4.2 # 固定 tag
ports: [{name: http, containerPort: 8080}]
startupProbe: # 最多等 30 × 5 = 150 秒,给慢启动兜底
httpGet: {path: /healthz, port: http}
failureThreshold: 30
periodSeconds: 5
readinessProbe: {httpGet: {path: /ready, port: http}, periodSeconds: 5} # 不通过就摘流量,不重启
livenessProbe:
httpGet: {path: /healthz, port: http}
periodSeconds: 10
timeoutSeconds: 3
failureThreshold: 6 # 阈值放宽,避免高负载下被误杀
resources:
requests: {cpu: "250m", memory: "256Mi"}
limits: {cpu: "500m", memory: "512Mi"}

常见报错

  • Liveness probe failed:liveness 指向了依赖下游的端点,应拆成 readiness,或调大 failureThreshold / timeoutSeconds。
  • OOMKilled + exit code 137:先看 kubectl top pod 实际用量再调 limit;limits 与 requests 差距过大,节点紧张时先被驱逐。
  • Pod 长期 Pending、事件为 FailedScheduling:requests 之和超过节点可分配量,调小 requests 或扩容节点。
  • readiness 一直失败但容器活着:检查端口与路径,HTTP 探测只有 200–399 成功,404 与 5xx 都算失败。

参考