Skip to content

Service without endpoints: Could not connect to server

Kubernetestested on RKE2 v1.37.2

A Service is just a selector plus a virtual IP. Which Pods sit behind it is written into the EndpointSlice by the EndpointSlice controller. "Could not connect to server" only tells you that it fails – the PORTS and ENDPOINTS columns show which station is to blame.

Symptom

kubectl -n tag07 exec client -- curl -sS -m 5 backend
curl: (7) Failed to connect to backend port 80 after 3 ms: Could not connect to server
command terminated with exit code 7

The name resolves (otherwise you'd get "Could not resolve host"), and the connection fails within a few milliseconds. Nobody is waiting behind the Service IP.

Diagnosis

Look at the Service's EndpointSlice, then compare the selector with the Pod labels.

kubectl -n tag07 get endpointslices -l kubernetes.io/service-name=backend
kubectl -n tag07 get svc backend -o jsonpath='{.spec.selector}{"\n"}'
kubectl -n tag07 get pods --show-labels
NAME            ADDRESSTYPE   PORTS     ENDPOINTS   AGE
backend-zdtk5   IPv4          <unset>   <unset>     68s
{"app":"backend-api"}
NAME                       READY   STATUS    RESTARTS   AGE   LABELS
backend-6ddd778465-2tbqs   1/1     Running   0          68s   app=backend,pod-templa...
backend-6ddd778465-884qb   1/1     Running   0          68s   app=backend,pod-templa...
client                     1/1     Running   0          68s   app=client

Causes

ENDPOINTS <unset>The selector doesn't match

The Service looks for app=backend-api, the Pods carry app=backend. The EndpointSlice stays empty, and Kubernetes reports this nowhere – no event, no status.

PORTS 8080Wrong targetPort

The PORTS column shows the port in the Pod (targetPort), not the Service port. Nobody listens there; the Pod answers with a TCP reset.

kubectl -n tag07 get endpointslices -l kubernetes.io/service-name=backend
NAME            ADDRESSTYPE   PORTS   ENDPOINTS               AGE
backend-zdtk5   IPv4          8080    10.42.1.29,10.42.2.26   82s

PORTS <unset>The port name doesn't exist in the Pod

targetPort: web looks in every Pod for a container port named web. There is none, so the controller enters the Pods without a port, and kube-proxy treats the Service like one without endpoints.

kubectl -n tag07 get endpointslices -l kubernetes.io/service-name=backend
NAME            ADDRESSTYPE   PORTS     ENDPOINTS               AGE
backend-zdtk5   IPv4          <unset>   10.42.1.29,10.42.2.26   2m1s

ready: falseThe readiness probe fails

Then the EndpointSlice is not empty: the IPs are in it, but with ready false, and get pods shows 0/1. kube-proxy sends no traffic there.

kubectl -n tag05 get endpointslices -l kubernetes.io/service-name=nieready \
  -o jsonpath='{.items[0].endpoints[0].conditions}{"\n"}'
{"ready":false,"serving":false,"terminating":false}

Fix

Set the selector to the Pods' labels and point targetPort at the named port in the Pod template (targetPort: http). The port number then lives in one place only.
kubectl apply -f backend.yaml
kubectl -n tag07 get endpointslices -l kubernetes.io/service-name=backend
kubectl -n tag07 exec client -- curl -s backend | head -1
NAME            ADDRESSTYPE   PORTS   ENDPOINTS               AGE
backend-zdtk5   IPv4          80      10.42.1.29,10.42.2.26   80s
Hostname: backend-6ddd778465-2tbqs

Don't: Waiting for an event. Kubernetes writes none for any of these faults – the answer is only in the EndpointSlice.

▶
Break it yourself and find it – free
Debugging Kubernetes, day 7: follow a request from the name through Service and EndpointSlice to the port in the Pod, with a drill to reproduce each fault.
Go to day 7 →

FAQ

Why does curl fail within milliseconds instead of timing out?

For a Service without (port-capable) endpoints, kube-proxy sets a REJECT rule. The connection is refused right away.

Does kubectl get endpoints still work?

Yes, but since 1.33 it warns that v1 Endpoints is deprecated. Every EndpointSlice carries the label kubernetes.io/service-name.

Endpoints and port are right, still no answer?

Check with ss -ltnp in an ephemeral container what the process listens on. If it only listens on 127.0.0.1, it can't be reached through the Service.

Related error patterns

More