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

常见根因

  1. 后端 Pod 未就绪或全部崩溃。
  2. Service targetPort 与容器端口不一致。
  3. 后端协议错误(HTTP/HTTPS、gRPC)。
  4. readiness 探针失败,Endpoints 为空。
  5. Ingress annotation 配置错误(超时、body size、rewrite)。
  6. NetworkPolicy 阻断控制器到后端。

修复

相关命令

读完自测

合上页面,先回答,再展开对照。

  1. Ingress 返回 502 时,先确认后端的什么状态?
  2. 哪些端口或协议不一致会导致 502?
  3. 修复后如何验证?
答案
  1. 先查 Service 的 Endpoints 是否为空,再核对 selector、Pod 就绪状态和容器端口。
  2. Service targetPort 与容器端口不一致、Ingress 的 service.port 与 Service 端口名不对齐,或后端 HTTP/HTTPS/gRPC 协议配置错误。
  3. 修好 Pod readiness、端口和 annotations,检查 controller 日志中的 upstream 错误,并用 curl 直连后端及经 Ingress 验证。