Kubernetes 健康探针详解
在 Kubernetes 中,健康探针是确保应用高可用性和可靠性的关键组件。通过三种不同类型的探针,Kubernetes 能够智能地管理容器的生命周期,确保流量只被路由到准备就绪的容器,并在应用出现故障时自动恢复。
探针工作机制概述
健康探针通过定期执行诊断检查来监控容器的状态,每种探针都有特定的作用和执行时机:
apiVersion: v1
kind: Pod
metadata:name: health-check-demo
spec:containers:- name: appimage: my-app:latestports:- containerPort: 8080startupProbe:httpGet:path: /health-startupport: 8080failureThreshold: 30periodSeconds: 10livenessProbe:httpGet:path: /health-liveport: 8080initialDelaySeconds: 5periodSeconds: 10readinessProbe:httpGet:path: /health-readyport: 8080initialDelaySeconds: 5periodSeconds: 5
三种探针的详细解析
🚀 Startup Probe(启动探针)
启动探针用于检测应用程序何时完成启动过程,特别适用于启动时间较长的应用。
startupProbe:httpGet:path: /health-startupport: 8080initialDelaySeconds: 10periodSeconds: 5failureThreshold: 30successThreshold: 1timeoutSeconds: 1
关键特性
- 目的:确定容器应用是否已成功启动
- 适用场景:启动缓慢的应用程序(如Java应用、大型单体应用)
- 工作方式:在启动期间禁用其他探针,直到启动成功
- 失败后果:容器被杀掉并重启
- 建议配置:设置较长的 failureThreshold 和适当的 periodSeconds
❤️ Liveness Probe(存活探针)
存活探针用于检测容器是否仍在正常运行,如果检查失败,容器将被重启。
livenessProbe:httpGet:path: /health-liveport: 8080httpHeaders:- name: Custom-Headervalue: AwesomeinitialDelaySeconds: 30periodSeconds: 10timeoutSeconds: 5failureThreshold: 3successThreshold: 1
检查方式
- HTTP GET:对指定的路径和端口执行HTTP请求
- TCP Socket:尝试建立TCP连接
- Exec:在容器内执行指定命令
✅ Readiness Probe(就绪探针)
就绪探针确定容器是否已准备好接收流量,只有当所有容器都就绪时,Pod才会被添加到服务端点。
readinessProbe:exec:command:- cat- /tmp/healthyinitialDelaySeconds: 5periodSeconds: 5failureThreshold: 1successThreshold: 1
关键特性
- 目的:检测容器是否准备好服务请求
- 失败后果:从服务的端点列表中移除,停止接收流量
- 适用场景:应用需要加载大量数据、依赖外部服务初始化等
探针配置参数详解
每种探针都支持相同的配置参数,用于精确控制检查行为:
参数 | 说明 | 默认值 |
---|---|---|
initialDelaySeconds | 容器启动后到开始第一次检查的等待时间 | 0 |
periodSeconds | 执行检查的频率(秒) | 10 |
timeoutSeconds | 检查超时时间 | 1 |
failureThreshold | 连续失败多少次后认为检查失败 | 3 |
successThreshold | 连续成功多少次后认为检查成功 | 1 |
实际应用场景
场景1:Web 应用健康检查
apiVersion: v1
kind: Pod
metadata:name: web-application
spec:containers:- name: webimage: nginx:latestports:- containerPort: 80livenessProbe:httpGet:path: /healthzport: 80initialDelaySeconds: 10periodSeconds: 5readinessProbe:httpGet:path: /readyzport: 80initialDelaySeconds: 5periodSeconds: 5
场景2:慢启动应用
apiVersion: v1
kind: Pod
metadata:name: slow-start-app
spec:containers:- name: java-appimage: my-java-app:lateststartupProbe:httpGet:path: /healthport: 8080failureThreshold: 30periodSeconds: 10livenessProbe:httpGet:path: /healthport: 8080periodSeconds: 5readinessProbe:httpGet:path: /readyport: 8080periodSeconds: 5
最佳实践
1. 合理的配置策略
livenessProbe:httpGet:path: /healthport: 8080initialDelaySeconds: 15 # 给应用足够的启动时间periodSeconds: 10 # 合理的检查频率timeoutSeconds: 3 # 适当的超时时间failureThreshold: 3 # 避免因瞬时故障重启
2. 探针端点设计
- 使用轻量级的健康检查端点
- 避免在探针检查中执行复杂操作
- 确保健康检查端点不依赖外部服务
- 为不同的探针使用不同的端点
故障排查技巧
常见问题及解决方案
1. 容器频繁重启
# 查看容器重启历史
kubectl describe pod <pod-name># 检查存活探针配置
kubectl get pod <pod-name> -o yaml | grep -A 10 livenessProbe
2. Pod 未就绪
# 检查就绪探针状态
kubectl describe pod <pod-name># 手动测试就绪端点
kubectl exec <pod-name> -- curl http://localhost:8080/health
总结
Kubernetes 的健康探针机制为容器化应用提供了强大的自我修复和流量管理能力:
- Startup Probe:保护慢启动应用,避免在启动期间被误杀
- Liveness Probe:确保故障应用能够自动恢复
- Readiness Probe:保证流量只被路由到准备就绪的应用
合理配置这三种探针,可以显著提高应用的可用性和可靠性,是生产环境部署的必备实践。