Issue#
The pods are Running, but requests to the Service fail. ingress-nginx returns 503 Service Temporarily Unavailable, and calls through the API server proxy return:
Error from server (ServiceUnavailable): no endpoints available for service "web"
kubectl describe shows an empty endpoints list:
kubectl describe svc web
Selector: app=web-app
Type: ClusterIP
Port: http 80/TCP
TargetPort: 8080/TCP
Endpoints: <none>
Cause#
A Service routes traffic only to pods that meet two conditions:
- Their labels match every key/value in
spec.selector. - They are Ready, meaning their readiness probe passes.
There are three common causes:
- A typo or mismatch between the Service selector and the pod template labels, such as
app=web-appagainstapp=web. - An extra selector key that the pods don't have, such as
tier=frontend. - Pods that match the selector but fail their readiness probe. Kubernetes marks them not ready and leaves them out of the Service.
Solution#
- Compare the Service selector with the pod labels:
kubectl get svc web -o jsonpath='{.spec.selector}{"\n"}'
kubectl get pods --show-labels
- Test the selector directly. If this returns nothing, the selector is wrong:
kubectl get pods -l app=web-app
- Fix the selector. Use a JSON patch, because it replaces the whole map. A plain merge patch would keep any wrong keys that are already there:
kubectl patch svc web --type=json \
-p '[{"op":"replace","path":"/spec/selector","value":{"app":"web"}}]'
Also update the selector in your manifest or Helm chart so the next deploy doesn't revert the fix:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web # must match spec.template.metadata.labels
ports:
- name: http
port: 80
targetPort: 8080 # must match the containerPort
- If the labels already match, check pod readiness.
0/1in theREADYcolumn means a failing probe:
kubectl get pods -l app=web
kubectl describe pod <pod-name> | grep -A5 -i readiness
Fix the probe path or port, or fix the app, so the probe passes.
Verify#
Check that the Service now has endpoints:
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl describe svc web | grep Endpoints
Endpoints: 10.244.1.12:8080,10.244.2.7:8080
Then test the Service from inside the cluster:
kubectl run tmp --rm -it --restart=Never --image=busybox -- \
wget -qO- http://web.default.svc.cluster.local
Tip: If endpoints are listed but connections are still refused, check that
targetPortmatches the port the container actually listens on.