Networking: Services, DNS, Ingress and policies · 6 MIN READ

Services: a stable virtual IP and DNS name in front of changing pods

Expose a Deployment through a ClusterIP Service, trace a request from selector to endpoints to targetPort, and fix a wrong targetPort and a selector that matches no pods.

Every pod gets its own IP address and loses it when the pod is replaced by a rollout, a crash or a node drain. Clients cannot chase those addresses. A Service gives a changing set of pods one stable virtual IP and one DNS name, and Kubernetes keeps the list of real backends current as pods come and go.

kubernetes · A MAP OF THE IDEA

  1. Label the pod template
  2. Select it in the Service
  3. Map port to targetPort
  4. Check endpoints first

Useful when

  • Clients use one name and one IP that outlive every pod.
  • Only ready pods receive new connections.

Watch out

  • A wrong targetPort still lists endpoints and fails every connection.
  • Balancing is per connection, so long-lived connections stick to one pod.

Implementation & operation cost: One extra object per workload, and the habit of checking endpoints before blaming the application.

01

Pod IPs are real but short-lived

The network plugin (CNI) gives each pod an IP from its node's pod range, 10.244.x.y in kind, and every pod can reach every other pod directly without NAT. That flat network is what makes Services possible, but a pod IP lives only as long as its pod. A Deployment replaces pods on every rollout, a failed node takes its pods with it, and the replacements come up with new addresses. A client that stored a pod IP keeps talking to an address that is gone or, worse, now belongs to a different pod. Hardcoding pod IPs in configuration, or resolving them once at startup, works in a demo and breaks on the first rollout. Clients need a name and an address that outlive any single pod.

02

A Service object: selector, port and targetPort

A Service has two jobs. Its selector picks the backend pods by label, and its ports say how traffic arrives and where it goes. port is what clients connect to on the Service's cluster IP; targetPort is the port inside the pod where the process listens. If you omit targetPort it defaults to port, a common silent mismatch when the app listens on 8080 and the Service on 80. targetPort may also be a name that refers to a named containerPort. Each pod then resolves the name itself, so an image that moves the app to another port only needs a new pod template, not a Service change. The API server allocates the ClusterIP from the service range, 10.96.x.y in kind, and the address stays fixed for the life of the Service.

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: web
        image: gopherforge/web:v1
        ports:
        - name: http
          containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
  - name: http
    port: 80
    targetPort: http
Read the code

The pod template names its container port http, and the Service refers to that name instead of 8080. Clients use web:80 and each connection is sent to a pod IP on 8080. The Service selects on app: web only, so pods from any workload with that label become backends. A numeric targetPort works even if containerPort is not declared, but a named targetPort only resolves in pods that declare the name; a pod without it is left out of the endpoints.

03

From selector to EndpointSlices

The Service object holds no pod addresses. The EndpointSlice controller in kube-controller-manager watches Services and pods and, for every Service with a selector, writes EndpointSlice objects listing each matching pod's IP, the resolved targetPort and its conditions: ready, serving and terminating. Only ready pods receive new connections, which is why a readiness probe controls traffic. A Service without a selector gets no automatic endpoints; that form is for backends you manage yourself. The older Endpoints API carries the same data in one object per Service and has been deprecated since Kubernetes 1.33, so kubectl get endpoints prints a deprecation warning on current clusters, and kubectl get endpointslices -l kubernetes.io/service-name=web is the modern view. In the GopherForge lab, kubectl get endpointslices -l kubernetes.io/service-name=web shows them, and kubectl get endpoints shows the same data in the older form.

04

kube-proxy turns the virtual IP into a pod

No network interface owns a ClusterIP and no process listens on it. kube-proxy runs on every node, watches Services and EndpointSlices, and programs the kernel, with iptables rules by default or nftables in the newer mode, so that a connection to 10.96.x.y:80 is rewritten (DNAT) to one ready pod's IP and targetPort. The choice is made once per connection, not per request: a client that keeps one HTTP/2 or gRPC connection open talks to a single pod for the life of that connection. The rewrite happens on the client's own node, so the client never sees the pod address. When a Service has no ready endpoints, kube-proxy installs a reject rule, and clients get an immediate connection refused instead of a hang.

05

Two classic bugs: wrong targetPort and an empty selector

A wrong targetPort looks healthy from the API: the Service exists, the pods are Running and Ready, and endpoints are listed. Every connection is still refused, because the endpoints point at a port where nothing listens. A selector that matches nothing fails differently: endpoints are <none>, usually because of a typo, a label set on the Deployment instead of the pod template, or a Service in another namespace than its pods. kubectl port-forward svc/web is the quick check from your laptop. It tunnels through the API server and the kubelet to one pod chosen when it starts, so it bypasses kube-proxy, does not balance load and does not follow a replaced pod. It is a debugging tool, not a way to serve users.

bash
kubectl get svc web -o wide
kubectl get pods -l app=web --show-labels
kubectl get endpoints web
kubectl describe svc web
Read the code

Read the four outputs together. The SELECTOR column of get svc must match the labels that --show-labels prints; if the -l query returns no pods, the selector is the bug. get endpoints lists pod-ip:port pairs: <none> means no ready pod matches, while a port that differs from the containerPort means a wrong targetPort. describe shows TargetPort and Endpoints side by side.

06

Try it in the lab terminal

Deploy two replicas of gopherforge/web:v1, which listens on 8080, and expose them with a deliberately wrong targetPort of 9090. A curl client pod stands in for another service in the cluster. Call the Service from it and see the failure, read the endpoints to find out why, and patch the Service. Then call it again from the pod, and reach the same Service from your own terminal through kubectl port-forward, the way you would check a Service on a real cluster from a laptop.

bash
kind create cluster
kubectl create deployment web --image=gopherforge/web:v1 --replicas=2 --port=8080
kubectl expose deployment web --port=80 --target-port=9090
kubectl run client --image=curlimages/curl -- sleep 3600
kubectl rollout status deploy/web
kubectl wait --for=condition=Ready pod/client
kubectl exec client -- curl -sS http://web
kubectl get endpoints web
kubectl get pods -l app=web -o wide
kubectl patch svc web --patch '{"spec":{"ports":[{"port":80,"targetPort":8080}]}}'
kubectl get endpoints web
kubectl exec client -- curl -sS http://web
kubectl port-forward svc/web 8080:80
curl -s http://localhost:8080/version
Read the code

get endpoints shows 10.244.0.10:9090,10.244.0.9:9090: both pods are listed, ready and wrong. The first curl from the client fails with "Connection refused" because nothing listens on 9090, and get pods -o wide confirms that those IPs belong to the web pods. After the patch the same IPs appear on :8080 and curl prints "gopherforge web v1 served by web-<hash>-<id>". port-forward reports "Forwarding from 127.0.0.1:8080 -> 8080", already resolved to the targetPort, and /version answers v1.

TAKE THIS WITH YOU

A Service is a selector plus a port mapping: the EndpointSlice controller turns the selector into ready pod addresses, and kube-proxy turns the ClusterIP into one of them per connection. When traffic fails, read the endpoints first: none means selector or readiness, the wrong port means targetPort.

Check your understanding

Service web lists two endpoints and both pods are Ready, yet every curl to http://web from another pod gets "Connection refused". What is the most likely cause?

Go to the source

Progress stays in this browser. No account needed.