Skip to content
Loopedin Labs

Kubernetes networking

Pods, Services, DNS and ports: how a request actually finds your app.

Mini Rush · practice

Three questions before you read

Guessing first makes the answers stick. No sign-in.

Most 'network' bugs in Kubernetes are labels, names or ports. Here's the path a request takes.

The mental model

Pod IPs change every time a pod is rescheduled. A Service gives you a stable name and a stable virtual IP (the ClusterIP) in front of a changing set of pods. Three parts make it work:

  1. Selector to endpoints. The control plane watches pods that match the Service's selector and records their IPs in EndpointSlices. Only pods that pass their readiness probe are marked ready.
  2. kube-proxy. It runs on every node and programs iptables, IPVS or nftables rules that DNAT ClusterIP:port to a ready pod's IP:targetPort. A ClusterIP is a rule, not a machine.
  3. Cluster DNS (CoreDNS). It gives every Service a name: <service>.<namespace>.svc.cluster.local. A pod can use the short name db inside its own namespace. To reach another namespace, use db.data.
spec:
  selector:
    app: api
  ports:
    - port: 80          # what clients call on the Service
      targetPort: 8080  # what the container listens on

Debug in this order

# 1. Does the Service have ready endpoints?
kubectl get endpointslices -l kubernetes.io/service-name=api -n prod

# 2. If it has none, compare the selector with the pod labels
kubectl get svc api -n prod -o jsonpath='{.spec.selector}'
kubectl get pods -n prod --show-labels

# 3. Is the pod Ready, not just Running?
kubectl get pods -n prod -o wide

# 4. Test from inside the cluster, by name and port
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- \
  wget -qO- http://api.prod.svc.cluster.local:80

If the Service has ready endpoints and step 4 still fails, check targetPort against the port the app actually listens on. Then check that the app binds to 0.0.0.0 and not to 127.0.0.1.

Gotchas

  • NetworkPolicy allows everything by default. Nothing is isolated until a policy selects a pod. Once a policy selects a pod for Ingress, only the traffic it lists gets in. A default-deny egress policy also blocks DNS unless you allow UDP and TCP port 53 to CoreDNS. And policies do nothing unless your CNI enforces them.
  • targetPort defaults to port. If you change the container port and leave out targetPort, the Service keeps sending traffic to the old port.
  • Named ports. targetPort: http looks up the container port named http in each pod, so pods can change port numbers without breaking the Service.
  • Don't ping a ClusterIP. In iptables mode nothing answers ICMP on it. Test the port with curl or nc.
  • Headless Services (clusterIP: None) return pod IPs straight from DNS, with no load balancing by kube-proxy.

Common wrong fix

The Service has no endpoints, so someone restarts CoreDNS or deletes the pods. Neither helps: DNS resolves fine, and the new pods come back with the same labels. Endpoints are empty for one of two reasons. Either the selector doesn't match the pod labels (often a typo such as app: api vs app: api-server), or the pods aren't Ready. Run kubectl describe pod and read the readiness probe events before you change anything.