Kubernetes Service not working: why it cannot reach your pods

Reproduced on k3s v1.31.5 (k3d) with kubectl v1.36.1, kube-proxy in iptables mode and the network policy controller that ships with k3s.

What you see

$ kubectl get pods -n svc --show-labels
NAME                        READY   STATUS    RESTARTS   AGE   LABELS
notready-6d64675779-t24k5   0/1     Running   0          58s   app=notready,pod-template-hash=6d64675779
web-64c6969c8b-vn7qq        1/1     Running   0          58s   app=web,pod-template-hash=64c6969c8b

$ kubectl get service -n svc -o wide
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
notready      ClusterIP   10.43.115.208   <none>        80/TCP    58s   app=notready
web           ClusterIP   10.43.192.226   <none>        80/TCP    58s   app=web
web-badport   ClusterIP   10.43.191.102   <none>        80/TCP    58s   app=web
web-typo      ClusterIP   10.43.39.128    <none>        80/TCP    58s   app=web-frontend

$ kubectl get endpoints -n svc
NAME          ENDPOINTS         AGE
notready                        58s
web           10.42.0.14:80     58s
web-badport   10.42.0.14:8080   58s
web-typo      <none>            58s

$ kubectl exec client -n svc -- wget -T 5 -qO- http://web-typo
wget: can't connect to remote host (10.43.39.128): Connection refused
$ kubectl exec client -n svc -- wget -T 5 -qO- http://web-badport
wget: can't connect to remote host (10.43.191.102): Connection refused
$ kubectl exec client -n svc -- wget -T 5 -qO- http://web:8080
wget: download timed out
$ kubectl exec client2 -n default -- wget -T 5 -qO- http://web
wget: bad address 'web'

$ kubectl get endpointslices -n svc -l kubernetes.io/service-name=notready -o yaml
  endpoints:
  - addresses:
    - 10.42.0.15
    conditions:
      ready: false
      serving: false
      terminating: false

$ kubectl describe pod notready-6d64675779-t24k5 -n svc
  Warning  Unhealthy  3s (x12 over 46s)  kubelet  spec.containers{nginx}: Readiness probe failed: HTTP probe failed with statuscode: 404

# app bound to 127.0.0.1 only
$ kubectl get endpoints lo -n svc
NAME   ENDPOINTS         AGE
lo     10.42.0.20:8080   4s
$ kubectl exec client -n svc -- wget -T 5 -qO- http://lo
wget: can't connect to remote host (10.43.58.56): Connection refused
$ kubectl exec localhost-app -n svc -- netstat -tln
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN

What it means

A Service is a stable name and virtual IP in front of a set of pods. The EndpointSlice controller lists the ready pods that match the Service selector, with the targetPort. kube-proxy on each node sends traffic for the cluster IP to one of those addresses.

If that list is empty, there is nowhere to send traffic. In our lab, with kube-proxy in iptables mode, a Service with no endpoints refused the connection straight away. A wrong targetPort was also refused, this time by the pod. From the client the two look the same, so read the endpoints before anything else.

Some failures look different. A port the Service does not expose timed out in our lab. A short name called from another namespace failed with bad address, because DNS tries the client's own namespace. This page is about traffic inside the cluster. For errors from an Ingress, see the 502 and 503 page.

Common causes

The selector matches no pods

The Service selector has a typo, or the pod labels changed in a new chart or manifest. The pods can be Running and ready and still get no traffic from this Service.

How to tell: kubectl get endpoints shows <none> and describe service shows an empty Endpoints line. The SELECTOR column does not match any labels from kubectl get pods --show-labels.

No matching pod is ready

The selector matches pods, but none pass their readiness probe. They stay in the EndpointSlice marked not ready and get no traffic.

How to tell: The ENDPOINTS column is blank rather than <none>. The EndpointSlice lists the pod with ready: false and the pod has Readiness probe failed events.

targetPort does not match the container port

The Service sends traffic to a port where nothing listens. The pod is ready, so it is listed as an endpoint. Every connection to it is refused.

How to tell: kubectl get endpoints shows the pod IP with a port that is not the containerPort. A request straight to the pod IP on that port is refused as well.

The app listens on 127.0.0.1 only

The app binds to localhost instead of all interfaces. It works from inside its own container but refuses connections from every other pod.

How to tell: The endpoints look correct but the connection is refused. netstat -tln inside the pod shows the port on 127.0.0.1 rather than 0.0.0.0.

A NetworkPolicy blocks the client

A policy selects the server pods and does not allow this client. The Service and its endpoints look fine.

How to tell: kubectl get networkpolicy shows a policy that selects the pods. In our lab the k3s policy controller rejected the traffic, which showed as Connection refused. Many network plugins drop it instead, which shows as a timeout.

Wrong name, namespace or Service port

The client calls a short name from another namespace, or a port the Service does not expose. A short name only resolves in the client's own namespace.

How to tell: bad address or NXDOMAIN for the name. A timeout on a port that is not in the Service's PORT(S) column.

How to diagnose it

  1. kubectl get endpoints <service> -n <namespace>

    <none> means the selector matches no pods. A blank value means pods match but none are ready. An address shows the exact pod IP and port traffic goes to.

  2. kubectl get service <service> -n <namespace> -o wide

    Note the SELECTOR and PORT(S). Compare the selector with the labels from kubectl get pods -n <namespace> --show-labels.

  3. kubectl get endpointslices -n <namespace> -l kubernetes.io/service-name=<service> -o yaml

    Check each address, its ready condition and the port number. ready: false points at the readiness probe, a wrong port points at targetPort.

  4. 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 a ready endpoint.

  5. kubectl exec <client-pod> -n <namespace> -- wget -T 5 -qO- http://<service>:<port>

    Calls the Service from inside the cluster. Connection refused, a timeout and bad address each point at a different cause above. The image needs wget or curl.

  6. kubectl exec <pod> -n <namespace> -- netstat -tln

    Shows the address and port the app listens on. 127.0.0.1 means only the pod itself can connect. Use ss -tln if the image has no netstat.

  7. kubectl get networkpolicy -n <namespace>

    Any policy whose podSelector matches the server pods limits who can connect. Check that the client's labels and namespace are allowed on that port.

How to fix it

Fix the selector or the pod labels

Change the Service selector or the pod template labels so they match. In our lab the endpoints filled in within seconds of the patch and the request worked.

kubectl patch service <service> -n <namespace> -p '{"spec":{"selector":{"app":"<pod-label-value>"}}}'

Trade-off: A selector that is too wide sends traffic to pods from other workloads, such as an old version still running. List every pod and its labels before you change it.

Use the named container port as targetPort

Name the port in the pod template and use that name as the Service targetPort. If the app's port changes later, the Service follows it.

# Pod template
ports:
- name: http
  containerPort: 8080
---
# Service
ports:
- port: 80
  targetPort: http

Trade-off: Pods that do not declare the port name are left out of the endpoints. Roll out the pod change before the Service change.

Make the app listen on all interfaces

Set the app to bind to 0.0.0.0 or to the pod IP, not 127.0.0.1. Many frameworks bind to localhost by default in development mode.

Trade-off: The port is then open to every pod your network policies allow. Add a NetworkPolicy if it should not be reachable from the whole cluster.

Fix readiness

Fix the probe path or port, or the app, so the pods pass readiness. See the probe failed page for the pod side.

Trade-off: Removing the readiness probe fills the endpoints, but traffic then reaches pods that cannot serve yet. You trade a refused connection for failed requests.

Allow the client in the NetworkPolicy

Add an ingress rule that lets the client's pods reach the server pods on the Service's targetPort.

ingress:
- from:
  - podSelector:
      matchLabels:
        app: <client-label>
  ports:
  - port: 8080
    protocol: TCP

Trade-off: Every rule opens a path. Allow the specific client labels and port, not the whole namespace or every port.

How to stop it coming back

  • Use named ports for targetPort so the Service follows the container.
  • Add a CI check that each Service selector matches the labels in its workload's pod template.
  • Alert when a Service has zero ready endpoints, not only when error rates rise.
  • Call Services in other namespaces by their full name, such as web.shop.svc.cluster.local.

How KubeGraf investigates it

Agents propose competing explanations and cite the evidence behind each one. How KubeGraf handles Kubernetes incidents.

Frequently asked questions

What does service has no endpoints mean?

No ready pod matches the Service selector, so there is nowhere to send traffic. Either the selector matches no pods or every matching pod fails its readiness probe.

How do I fix a Kubernetes service selector mismatch?

Compare the SELECTOR from kubectl get service with the labels from kubectl get pods with the show labels flag. Fix whichever is wrong and the endpoints fill in.

Why do I get connection refused when calling a Kubernetes service?

With kube-proxy in iptables mode, a Service with no endpoints refuses the connection. A wrong targetPort or an app that listens only on 127.0.0.1 gives connection refused too. Check the endpoints and the port the app listens on.

Why does my service name not resolve from another namespace?

A short name only resolves inside the client's own namespace. Use the Service name with its namespace, such as web.shop, or the full name web.shop.svc.cluster.local.

Last updated . Try KubeGraf Freemium.

Related

Kubernetes documentation