在 Kubernetes 中,Readiness Probe(就绪探针) 用于确定 Pod 是否准备好接收流量。如果探针失败,Pod 不会被加入 Service 的负载均衡池,导致请求无法到达该 Pod。常见的错误之一是:
Readiness probe failed: HTTP probe failed with statuscode: 500本文将从 问题现象、可能原因、排查方法、解决方案 等多个角度,深入分析如何解决此类问题,并提供代码示例和最佳实践。
Kubernetes 使用 Readiness Probe 检测 Pod 是否已经启动并可以处理请求。如果探针失败,Pod 会被标记为 NotReady,并从 Service 的 Endpoints 中移除,直到探针再次成功。
探针支持三种检测方式:
2xx 或 3xx。0 表示成功。示例配置:
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5 # 容器启动后等待 5 秒开始探测
periodSeconds: 5 # 每 5 秒探测一次
failureThreshold: 3 # 连续失败 3 次后标记为未就绪当探针返回 HTTP 500 时,意味着:
使用 kubectl describe pod 查看 Pod 的详细状态:
kubectl describe pod <pod-name>重点关注:
Readiness probe failed 或其他错误(如 OOMKilled)。示例输出:
Events:
Warning Unhealthy 3s (x3 over 13s) kubelet Readiness probe failed: HTTP probe failed with statuscode: 500使用 kubectl logs 检查应用日志:
kubectl logs <pod-name> --tail=100如果应用依赖数据库或外部服务,检查是否有连接错误:
ERROR: Failed to connect to MySQL: dial tcp 10.0.0.1:3306: connect: connection refused进入 Pod 并手动访问探针端点:
kubectl exec -it <pod-name> -- sh
curl http://localhost:8080/health如果返回 500,说明应用内部有问题。
如果应用启动较慢,可以增加 initialDelaySeconds:
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30 # 等待 30 秒再开始探测
periodSeconds: 5
failureThreshold: 3如果应用依赖数据库、Redis 或其他微服务,确保它们正常运行:
kubectl get pods -n <namespace>并测试网络连通性:
kubectl exec -it <pod-name> -- curl http://<service-name>:<port>如果 Pod 因 OOM 被杀死:
kubectl describe pod <pod-name> | grep -i "OOMKilled"调整 resources 配置:
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1000m"
memory: "1Gi"如果 Pod 无法访问依赖服务,可能是 NetworkPolicy 限制:
kubectl get networkpolicy -n <namespace>确保允许流量通过:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-db-access
spec:
podSelector:
matchLabels:
app: my-app
ingress:
- from:
- podSelector:
matchLabels:
app: my-app
ports:
- protocol: TCP
port: 3306 # MySQL 端口/health 应该只检查关键依赖(如数据库、缓存)。initialDelaySeconds 应大于应用启动时间。failureThreshold 和 periodSeconds 应根据业务需求调整。问题 | 排查方法 | 解决方案 |
|---|---|---|
应用返回 500 | kubectl logs | 修复代码或依赖 |
探针配置错误 | kubectl describe pod | 修正 path 或 port |
应用启动慢 | 观察日志 | 增加 initialDelaySeconds |
依赖服务不可用 | kubectl get svc | 检查数据库/缓存 |
资源不足 | kubectl top pod | 调整 resources |
网络策略限制 | kubectl get networkpolicy | 调整 NetworkPolicy |
通过以上方法,可以系统性地解决 Readiness probe failed: HTTP probe failed with statuscode: 500 问题,确保 Kubernetes 应用稳定运行。
希望这篇指南能帮助你快速定位和解决问题! 🚀