解决 K8s 报错 readiness probe failed connection refused
分析 Kubernetes Pod 就绪探针报 Connection refused 导致流量无法切入的根因,提供 initialDelaySeconds 优化配置。
问题现象
Kubernetes Pod 处于 Running 状态,但 READY 列显示为 0/1,无法对外承接 Service 流量,事件日志显示:
Warning Unhealthy 10s (x10 over 90s) kubelet Readiness probe failed: Get "http://10.244.1.20:8080/health": dial tcp 10.244.1.20:8080: connect: connection refused
触发根因
- 应用程序冷启动耗时较长(如 Spring Boot 初始化耗时 40 秒),而探针在容器一启动就立即发起了探测。
- 就绪探针指定的检测端口与应用实际监听的端口不一致。
- 应用程序仅监听了
127.0.0.1本地回环接口,而 kubelet 探针是从宿主机网络向 Pod IP 发起请求,被拒绝连接。
解决步骤与操作命令
步骤一:确保应用监听 0.0.0.0 所有网卡
检查应用配置,确保其绑定在通配地址 0.0.0.0,而非仅 localhost。
步骤二:合理配置 initialDelaySeconds 缓冲启动时间
编辑 Deployment 清单,为复杂的应用预留初始化启动窗口:
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30 # 等待应用初始化 30 秒后再开始探测
periodSeconds: 10 # 探测间隔
timeoutSeconds: 3 # 探测超时
failureThreshold: 3 # 连续 3 次失败判定不就绪
步骤三:结合 StartupProbe(启动探针)使用
对于启动特别慢的应用(如老旧单体系统),推荐配置独立的 startupProbe:
startupProbe:
httpGet:
path: /health
port: 8080
failureThreshold: 30
periodSeconds: 5 # 最长允许 150 秒完成冷启动
验证确认
更新 Deployment 后观察新 Pod 启动状态:
kubectl rollout status deployment/my-app
Pod 在健康检查通过后状态迅速变为 1/1 READY,Service 端点自动关联 Endpoint。


