Skip to content

Scenario: Why is Ingress returning 502 Bad Gateway?

A 502 error is the LB saying: "I tried to talk to your Pod, but it hung up on me." It proves the Ingress Controller is alive, but the connection to the backend failed.

1. The Mismatch Theory

The most common cause is a port mismatch. The Service object points to targetPort: 8080, but your container is actually listening on 80.

Check Container Port:

bash
kubectl get pod <backend-pod> -o yaml | grep containerPort

Check Service Port:

bash
kubectl get svc <backend-service> -o yaml

2. The Header Problem

Some Ingress Controllers (like NGINX) expect the backend to return valid headers.

  • If your app crashes immediately upon receiving a request, NGINX returns 502.
  • If your app takes too long (timeout), NGINX returns 504.

Debugging Tip

Check the logs of the Ingress Controller itself, not just your app. It will tell you exactly why it disliked the upstream response.

bash
kubectl logs -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx