k8s-ingress-502
Kubernetes Ingress 502 Bad Gateway 的定位
现象
通过 Ingress 访问返回 502。Ingress Controller 能收到请求,但无法从后端 Pod 拿到有效响应。
快速定位
kubectl get ingress -n <ns>
kubectl describe ingress <ingress> -n <ns>
kubectl get svc,endpoints <svc> -n <ns>
kubectl get pods -n <ns> -l <selector> -o wide
kubectl logs -n <ingress-ns> <controller-pod> --tail=100
常见根因
- 后端 Pod 未就绪或全部崩溃。
- Service
targetPort 与容器端口不一致。
- 后端协议错误(HTTP/HTTPS、gRPC)。
- readiness 探针失败,Endpoints 为空。
- Ingress annotation 配置错误(超时、body size、rewrite)。
- NetworkPolicy 阻断控制器到后端。
修复
- 先确认 Endpoints 是否为空,再查 selector 和端口。
- 修好 Pod readiness 和容器端口。
- 对齐 Ingress 的
service.port 与 Service 端口名。
- 检查 controller 日志里的 upstream 错误。
- 修正超时/body size/protocol annotations。
- 检查 NetworkPolicy 和 CNI 连通性。
相关命令
kubectl — Ingress/Service/Endpoints
k8s 分支的 ingress 页面 — 配置细节
curl — 直连后端验证
读完自测
合上页面,先回答,再展开对照。
- Ingress 返回 502 时,先确认后端的什么状态?
- 哪些端口或协议不一致会导致 502?
- 修复后如何验证?
答案
- 先查 Service 的 Endpoints 是否为空,再核对 selector、Pod 就绪状态和容器端口。
- Service
targetPort 与容器端口不一致、Ingress 的 service.port 与 Service 端口名不对齐,或后端 HTTP/HTTPS/gRPC 协议配置错误。
- 修好 Pod readiness、端口和 annotations,检查 controller 日志中的 upstream 错误,并用
curl 直连后端及经 Ingress 验证。