Kubernetes探针全解:你的Pod健康吗?
Kubernetes探针全解:你的Pod健康吗?
凌晨三点,监控大屏突然告警:核心服务流量暴跌50%!你发现新版本Pod全部卡在启动状态,旧Pod被误杀导致雪崩。这一切的根源竟是因为探针配置不当!本文将用真实生产案例,带你掌握Kubernetes探针的终极配置艺术。
一、探针三剑客:K8S的"健康管家"
| 探针类型 | 核心使命 | 失败后果 | 适用场景 |
|---|---|---|---|
| 存活探针 | 判断是否该"安乐死" | 杀死容器并重启 | 死锁检测、僵尸进程 |
| 就绪探针 | 决定是否接收流量 | 从Service摘除流量 | 慢启动服务、依赖预热 |
| 启动探针 | 保护启动期的"婴儿阶段" | 触发容器重启 | Java/Python等慢启动语言 |

二、生产级配置模板(含避坑注释)
电商大促场景配置示例:
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: order-service
livenessProbe:
httpGet:
path: /internal/health
port: 8080
initialDelaySeconds: 30 # 避免冷启动误杀
periodSeconds: 5
failureThreshold: 3 # 连续3次失败才判定死亡
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 2
successThreshold: 3 # 连续3次成功才标记就绪
startupProbe:
httpGet:
path: /startup
port: 8080
failureThreshold: 30 # 最长等待30*5=150秒
periodSeconds: 5
关键参数解析:
initialDelaySeconds:等待时间(冷启动保护)periodSeconds:检测间隔(频率控制)failureThreshold:失败容忍度(防抖动)successThreshold:成功确认次数(防误判)
三、三大死亡案例与重生方案
案例1:存活探针误杀门
现象:Spring Boot应用启动需60秒,存活探针默认10秒检测导致无限重启
根因分析:
livenessProbe:
initialDelaySeconds: 10 # 不足!
periodSeconds: 5
failureThreshold: 3 # 10+5*3=25秒即重启
修复方案:
startupProbe: # 增加启动保护
httpGet:
path: /actuator/health
port: 8080
failureThreshold: 30
periodSeconds: 5
livenessProbe:
initialDelaySeconds: 60 # 覆盖启动时间
案例2:就绪风暴
现象:服务过载时就绪探针失败,触发全节点摘流导致雪崩
优化方案:
readinessProbe:
httpGet:
path: /health?level=light # 轻量级检测
timeoutSeconds: 1 # 快速超时
successThreshold: 1
failureThreshold: 1
案例3:TCP检测陷阱
错误配置:
livenessProbe:
tcpSocket:
port: 8080
periodSeconds: 10
隐藏风险:端口监听≠服务就绪,导致流量打到未初始化完成的Pod
正确方案:
readinessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "curl -s http://localhost:8080/ready | grep OK"
四、探针选择三原则
-
HTTP探针(首选)
- 优点:精准检测业务状态
- 场景:Web服务、REST API
-
TCP探针
- 优点:快速简单
- 场景:数据库、缓存等端口服务
-
Exec探针
- 优点:高度定制
- 场景:复杂状态检测(如磁盘空间)
危险操作:
# 谨慎使用!可能引发副作用 exec: command: - "rm" - "-rf" - "/tmp/lock.file"
五、高级调试技巧
-
事件流监控
kubectl get events --sort-by=.metadata.creationTimestamp | grep -i probe -
临时关闭探针
# 紧急恢复手段(需权限控制) kubectl edit deploy order-service # 注释掉livenessProbe配置 -
分布式追踪集成
httpGet: path: /health port: 8080 httpHeaders: - name: X-Request-ID value: probe-check
六、监控大盘搭建指南
-
Prometheus指标
kubelet_prober_probe_total{probe_type="liveness"}kubelet_prober_probe_duration_seconds
-
Grafana面板
sum(rate(kubelet_prober_probe_total{namespace="$namespace", pod=~"$pod", probe_type=~"liveness|readiness"}[5m])) by (probe_type, result)![探针监控面板]()
-
告警规则
- alert: ProbeFailure expr: rate(kubelet_prober_probe_total{result="failed"}[5m]) > 0.5 for: 2m labels: severity: critical annotations: summary: "探针连续失败 ({{ $value }}%%)" description: "{{ $labels.pod }} 的{{ $labels.probe_type }}探针失败率过高"
七、未来演进:智能探针
-
自适应阈值调整
根据历史数据动态计算periodSeconds -
业务指标融合
httpGet: path: /health?metrics=queue_length,cpu_load -
eBPF深度检测
通过内核态探针判断真实服务状态
八、终极配置清单
✅ 存活/就绪探针必须同时配置
✅ 启动探针用于启动时间超过30秒的服务
✅ HTTP探针路径需轻量级(<100ms)
✅ 生产环境failureThreshold不低于3次
✅ 所有探针必须设置timeoutSeconds
记住:好的探针配置如同精准的脉搏监测仪,既要及时发现问题,又要避免误诊。掌握这三大探针的脾性,让你的K8S集群拥有钢铁般的免疫力!

浙公网安备 33010602011771号