K8s部署健康检查机制踩坑记录
最近在K8s集群中部署应用时,遇到了一个令人头疼的健康检查问题。项目使用Spring Boot应用,通过Deployment控制器管理,配置了liveness和readiness探针。
环境信息
- Kubernetes版本: v1.24.6
- 应用框架: Spring Boot 2.7.0
- 镜像: openjdk:11-jre-slim
问题描述
应用启动后,Pod状态一直是CrashLoopBackOff,通过kubectl describe pod查看事件发现:
Liveness probe failed: HTTP probe failed with statuscode: 404
Readiness probe failed: HTTP probe failed with statuscode: 404
初始配置(失败)
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
readinessProbe:
httpGet:
path: /ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 3
排查过程
- 确认应用端口正确,8080端口正常监听
- 通过
kubectl exec -it pod-name -- curl http://localhost:8080/health测试,返回200 - 发现Spring Boot应用需要配置
management.endpoints.web.base-path=/actuator才能访问健康检查接口
正确配置方案
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 15
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/ready
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
监控指标
- 应用启动时间: ~30s
- Liveness探针成功率: 100%
- Readiness探针成功率: 100%
- Pod就绪时间: <60s
经验总结
- 健康检查路径必须与应用实际暴露的路径一致
initialDelaySeconds需要足够长,确保应用完全启动- 探针间隔不宜过短,避免频繁探测影响性能
- 建议使用
kubectl logs和describe组合排查问题
最终通过调整探针配置,Pod成功进入Running状态。

讨论