Video in progress

The pods are Running, but requests through the Service give "connection refused" or time out. How do you check?

First I check whether the Service has endpoints. If they are empty, the selector does not match the labels or the pods have not passed readiness. If the endpoints exist, I check targetPort, the address the app is listening on, and NetworkPolicy.

📖 Answer

From the bottom up, in order:

kubectl get endpointslices -l kubernetes.io/service-name=payment
kubectl get pods -l app=payment-service --show-labels
kubectl describe svc payment            # Selector, Port, TargetPort

The endpoints are empty. There are two causes:

  • the selector is wrong: the Service looks for app: payment-service, while the Deployment set the label app: payment. One word of difference, and traffic goes nowhere, with no error either;
  • the pods are Running but not Ready (READY 0/1). A pod whose readiness fails is not added to the endpoints, so look at the probe errors in kubectl describe pod.

The endpoints exist, but it does not connect.

  • targetPort does not match the app's real port: the Service sends to 8080, while Spring is on server.port=8081;
  • the app listens only on 127.0.0.1 (server.address is set): curl localhost works inside the pod, but not from outside;
  • there is a default-deny NetworkPolicy, and the calling namespace is not allowed.

Checking directly from inside the cluster splits the problem in two:

kubectl run tmp --rm -it --image=curlimages/curl --command -- sh
curl -v http://payment.default.svc.cluster.local/actuator/health
curl -v http://10.42.3.17:8080/actuator/health    # by pod IP

If the pod IP works and the Service does not, the problem is the selector or the port. If the pod IP does not work either, it is in the app or in NetworkPolicy. If the name does not resolve at all (Could not resolve host), the namespace name is wrong or there is a problem with CoreDNS.

In practice the first one is the most common: someone changed a label in the Helm chart and forgot the Service selector.