Kubernetes 502 Bad Gateway and 503 from your ingress: how to fix them
Reproduced on k3s v1.31.5 (k3d) with kubectl v1.36.1, ingress-nginx controller v1.15.1 and the Traefik v2.11.18 that ships with k3s.
What you see
$ curl -i -H "Host: typo.lab" http://localhost:18081/
HTTP/1.1 503 Service Temporarily Unavailable
Content-Type: text/html
Content-Length: 190
Connection: keep-alive
<head><title>503 Service Temporarily Unavailable</title></head>
$ kubectl get endpoints -n lab
NAME ENDPOINTS AGE
closer 10.42.0.12:8080 34s
notready 34s
web 10.42.0.10:80 34s
web-badport 10.42.0.10:8080 34s
web-typo <none> 34s
$ kubectl get endpointslices -n lab -l kubernetes.io/service-name=notready -o yaml
endpoints:
- addresses:
- 10.42.0.11
conditions:
ready: false
serving: false
terminating: false
$ kubectl describe ingress typo-nginx -n lab
Rules:
Host Path Backends
---- ---- --------
typo.lab
/ web-typo:80 ()
$ kubectl logs -n ingress-nginx deploy/ingress-nginx-controller
W1123 20:22:43.736907 7 controller.go:1241] Service "lab/web-typo" does not have any active Endpoint.
W1123 20:22:43.737009 7 controller.go:1241] Service "lab/notready" does not have any active Endpoint.
$ curl -i -H "Host: badport.lab" http://localhost:18081/
HTTP/1.1 502 Bad Gateway
$ kubectl describe ingress badport-nginx -n lab
badport.lab
/ web-badport:80 (10.42.0.10:8080)
$ kubectl logs -n ingress-nginx deploy/ingress-nginx-controller
2026/11/23 20:22:49 [error] 506#506: *2373 connect() failed (111: Connection refused) while connecting to upstream, client: 10.42.0.1, server: badport.lab, request: "GET / HTTP/1.1", upstream: "http://10.42.0.10:8080/", host: "badport.lab"
2026/11/23 20:23:26 [error] 502#502: *3905 upstream prematurely closed connection while reading response header from upstream, client: 10.42.0.1, server: closer.lab, request: "GET / HTTP/1.1", upstream: "http://10.42.0.13:8080/", host: "closer.lab"What it means
An ingress controller is a reverse proxy. It reads the Ingress, finds the Service and sends each request to a pod IP and port from that Service's EndpointSlices. It does not go through the Service's cluster IP. Which error you get depends on how far it got.
A 503 means the controller found no ready endpoint at all. ingress-nginx answers 503 Service Temporarily Unavailable itself and logs that the Service does not have any active Endpoint. A 502 means it did pick a pod, but the connection was refused, reset or closed before a full reply came back.
Controllers do not all answer the same way. In our lab the Traefik that ships with k3s returned 404 page not found for a Service with no endpoints, not 503. Its log said Skipping service: no endpoints found. It returned 502 Bad Gateway for the wrong port and the dropped connection, like ingress-nginx. Always read the status code next to the controller's log.
Common causes
The Service selector matches no pods
A typo in the selector, or pod labels changed by a new chart version, leaves the Service with nothing to send to. The pods can be healthy and still get no traffic.
How to tell: kubectl get endpoints shows <none> and the EndpointSlice has no addresses. ingress-nginx logs does not have any active Endpoint and returns 503.
No pod is ready
The Service selects pods, but none of them pass their readiness probe. Zero replicas, pods stuck in Pending and pods in a crash loop end the same way.
How to tell: The ENDPOINTS column is empty and the EndpointSlice lists the pod with ready: false. The pod shows 0/1 Running with Readiness probe failed events.
targetPort does not match the port the app listens on
The Service sends traffic to a port where nothing is listening. The pod is ready, so it is in the endpoints. Every request to it is refused.
How to tell: 502 with connect() failed (111: Connection refused) in the controller log. describe ingress shows the pod IP with a port that is not the containerPort.
The app closes the connection without a reply
The app crashes during the request, or it drops idle keep-alive connections that the proxy is about to reuse. The proxy gets no complete response and returns 502.
How to tell: upstream prematurely closed connection while reading response header from upstream, or recv() failed (104: Connection reset by peer).
Pods stop before they leave the endpoints
During a rollout or scale down, a pod can get SIGTERM and stop listening while the controller still has its IP. Requests sent in that gap fail.
How to tell: The 502 errors only show up during deploys, scale downs or node drains. They stop once the rollout is done.
How to diagnose it
kubectl describe ingress <ingress> -n <namespace>Read the Backends column. Empty brackets after service:port mean no ready endpoints. A pod IP with the wrong port points at targetPort.
kubectl get endpointslices -n <namespace> -l kubernetes.io/service-name=<service> -o yamlNo addresses means the selector matches nothing. Addresses with ready: false mean the pods exist but fail readiness. Also check the port number.
kubectl get service <service> -n <namespace> -o wideThe SELECTOR column shows the labels the Service looks for. Compare it with kubectl get pods -n <namespace> --show-labels.
kubectl logs -n ingress-nginx deploy/ingress-nginx-controller --since=10mdoes not have any active Endpoint points to a 503. connect() failed, Connection reset by peer or upstream prematurely closed connection point to a 502. Your controller's namespace and name may differ.
kubectl port-forward service/<service> -n <namespace> 8080:<service-port>Then run curl -i http://localhost:8080/ from your machine. If this fails too, the problem is the Service or the app, not the Ingress.
kubectl describe pod <pod> -n <namespace>Check Ready, the container ports and the Events. Readiness probe failed or a restart loop explains why the pod is not in the endpoints.
How to fix it
Make the selector match the pod labels
Change the Service selector, or the pod template labels, so they match. The endpoints fill in as soon as they do.
kubectl patch service <service> -n <namespace> -p '{"spec":{"selector":{"app":"<pod-label-value>"}}}'Trade-off: A selector that is too broad picks up pods from another Deployment, such as an old version. Check the labels on every pod in the namespace first.
Point targetPort at the container's named port
Give the container port a name and use that name as the Service targetPort. A port change in the pod then carries over to the Service.
# Deployment pod template
ports:
- name: http
containerPort: 80
---
# Service
ports:
- port: 80
targetPort: httpTrade-off: Every pod behind the Service must declare that port name. Pods without it are left out of the endpoints.
Get the pods ready
Fix the readiness probe or the app so pods pass it. Scale up from zero if needed. See the probe failed and CrashLoopBackOff pages for the pod side.
Trade-off: Do not delete the readiness probe to make a 503 go away. Traffic then reaches pods that cannot serve. You swap the 503 for 502 or 500 errors.
Shut down cleanly and keep connections open longer
Add a short preStop sleep so the pod leaves the endpoints before the app stops. Set the app's keep-alive idle timeout longer than the controller's upstream keep-alive timeout.
lifecycle:
preStop:
exec:
command: ["sleep", "5"]Trade-off: The preStop sleep makes every pod stop slower and has to fit inside terminationGracePeriodSeconds. Longer keep-alive holds more idle connections on the app.
How to stop it coming back
- Use named ports for the Service targetPort and the probes so they follow the container.
- Alert when a Service behind an Ingress has zero ready endpoints, not only when the 5xx count rises.
- Add a smoke test after each deploy that calls the real ingress host and fails on 502 or 503.
- Handle SIGTERM in the app and finish in-flight requests before it exits.
How KubeGraf investigates it
Specialist agents pull the logs, events, metrics, workloads and recent deploys for the failing workload into one view. How KubeGraf handles Kubernetes incidents.
Frequently asked questions
Why does nginx ingress return 502 Bad Gateway?
The controller reached a pod address but could not get a valid reply. The usual causes are a wrong targetPort, an app that is not listening yet or an app that closes the connection. The controller log shows connect() failed or upstream prematurely closed connection.
What does upstream prematurely closed connection mean?
The pod accepted the connection and then closed it before sending a full response. Look at the app's own logs for a crash or a timeout at the same moment.
Why do I get 503 Service Unavailable from Kubernetes?
The Service behind the Ingress has no ready pods. Either its selector matches no pods or every matching pod is failing its readiness probe.
How do I fix service has no endpoints?
Compare the Service selector with the pod labels and correct whichever is wrong. If the labels match, fix the readiness probe or the app so the pods become ready.
Last updated . Try KubeGraf Freemium.
Related
- Liveness probe failed / Readiness probe failed
- CrashLoopBackOff
- Pending (FailedScheduling)
- A 502 from the ingress: which hop actually failed
- The best Kubernetes troubleshooting tools in 2026