探针决定 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
| livenessProbe: exec: {command: ["cat", "/tmp/healthy"]}
readinessProbe: httpGet: {path: /healthz, port: 8080, httpHeaders: [{name: X-Probe, value: "on"}]}
livenessProbe: tcpSocket: {port: 6379}
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 periodSeconds: 10 timeoutSeconds: 3 successThreshold: 1 failureThreshold: 3
|
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: cpu: "250m" memory: "256Mi" limits: 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 管理策略使用独占核。
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 ports: [{name: http, containerPort: 8080}] startupProbe: 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 都算失败。
参考