Cloud Native › Kubernetes

How Kubernetes Services and CoreDNS route traffic to Pods

Compare ClusterIP, NodePort, and Headless Services with EndpointSlice and CoreDNS on a 3-node kind cluster running Kubernetes v1.37.0.

How Kubernetes Services and CoreDNS route traffic to Pods

Kubernetes replaces Pods with new IPs whenever they crash or roll out, so hardcoding Pod IPs breaks service-to-service traffic immediately. This post explains how ClusterIP, NodePort, and Headless Services work with EndpointSlice and CoreDNS, backed by hands-on runs on a 3-node kind cluster.

All commands and outputs below ran on a local 3-node kind v0.33.0 cluster (Kubernetes v1.37.0, kubectl v1.36.1, CoreDNS v1.14.6, containerd 2.3.4). type: LoadBalancer and externalTrafficPolicy: Local require cloud controller or external load balancer integration, so they were not executed in this standalone kind setup and appear only in the conceptual comparison.

Why Pod IPs alone cannot sustain traffic

On a single Docker host, a user-defined bridge network provides built-in DNS at 127.0.0.11 so containers can reach each other by name, as covered in How Docker bridge networks and DNS service discovery work. Across a multi-node Kubernetes cluster, that single-host model is no longer enough.

Kubernetes gives every Pod a unique cluster-wide IP address, but Pods are ephemeral. When a Deployment replaces a failed Pod or rolls out a new image, the old IP disappears and the replacement Pod receives a different IP. Any upstream client holding the previous IP fails on the next request.

Kubernetes solves this by separating traffic delivery into three decoupled layers: a stable entry point (Service), a dynamic backend list (EndpointSlice), and cluster name resolution (CoreDNS). Clients talk to a fixed DNS name or virtual IP while the control plane and node proxies track Pod churn underneath.

How Service, EndpointSlice, and CoreDNS work under the hood

When a client Pod requests http://echo-svc, traffic moves through four steps inside the cluster.

flowchart TD
    A["Client Pod"] -->|"1. DNS query (10.96.0.10)"| B["CoreDNS"]
    B -->|"2. Return ClusterIP"| A
    A -->|"3. Send to ClusterIP"| C["kube-proxy rules"]
    D["EndpointSlice"] -.->|"Sync Ready Pod IPs"| C
    C -->|"4. DNAT to target"| E["Worker Node Pod"]
  1. Pod /etc/resolv.conf and CoreDNS: kubelet populates each Pod’s /etc/resolv.conf with the cluster DNS IP (kube-system/kube-dns, typically 10.96.0.10), namespace search domains, and options ndots:5. Inside the same namespace, calling the short name echo-svc automatically expands to echo-svc.<namespace>.svc.cluster.local.
  2. Stable virtual IP (ClusterIP): Creating a Service allocates a virtual IP from the Service CIDR (such as 10.96.0.0/12). That IP doesn’t belong to a physical interface; it acts as a stable destination matched by node-level forwarding rules.
  3. Dynamic tracking via EndpointSlice: The EndpointSlice controller (discovery.k8s.io/v1) watches Pods matching the Service’s selector and records only Ready Pod IPs and ports. Splitting endpoints into slices (defaulting to 100 endpoints per slice) avoids the scaling bottleneck of updating one monolithic Endpoints object in large clusters.
  4. Packet forwarding by kube-proxy: On every node, kube-proxy watches Service and EndpointSlice changes and updates kernel forwarding rules (iptables, IPVS, or nftables). When a packet hits the ClusterIP, the kernel applies DNAT to rewrite the destination to one of the Ready Pod IPs and routes it to the target worker node.

A Headless Service (clusterIP: None) skips both virtual IP allocation and kube-proxy load balancing. Instead, CoreDNS returns multiple A records pointing directly to every Ready Pod IP in the EndpointSlice.

Comparing Service types and when not to use them

Here is how the four main Service types differ in behavior and trade-offs.

TypeVirtual IP (ClusterIP)CoreDNS A recordReachabilityForwarding & SNATPrimary use case
ClusterIP (default)AllocatedSingle ClusterIPCluster-internal onlykube-proxy L4 distributionInternal microservice communication
NodePortAllocated + port on every node (30000–32767)Single ClusterIPExternal via <NodeIP>:<NodePort>Cross-node hop + SNAT under default Cluster policyLocal testing, backend target for external LBs
Headless (clusterIP: None)NoneOne A record per Ready Pod IPCluster-internal onlyNo proxy; client connects to Pod IP directlyStatefulSets (databases, Kafka), client-side LB (gRPC)
LoadBalancer (conceptual)Allocated + external IP provisionedSingle ClusterIPVia cloud/external LBExternal LB → NodePort or direct Pod routingCloud L4 entry point or Gateway front door

Knowing when not to pick a Service type prevents most early networking mistakes:

  • Don’t expose production HTTP/HTTPS traffic directly through NodePort. You’re stuck with the 30000–32767 port range, clients have to track node IPs manually, and you get no TLS termination or path-based routing. Place a load balancer or Gateway API in front and keep application Services as ClusterIP.
  • Don’t use a Headless Service for general stateless HTTP load balancing. CoreDNS hands back the list of Pod IPs, but client-side DNS caching (such as JVM defaults) and HTTP keep-alive pools often pin traffic to a single Pod. Reserve Headless Services for StatefulSets that need stable per-Pod identity or clients with built-in load balancing like gRPC.

Verifying ClusterIP, NodePort, and Headless on kind

The test cluster runs three nodes: k8s-lab-control-plane (172.22.0.4), k8s-lab-worker (172.22.0.2), and k8s-lab-worker2 (172.22.0.3). Port 30080 on the control-plane node is mapped to localhost:18880 on the host.

1. Deploy the workload and three Services

This manifest deploys two replicas of traefik/whoami:v1.10.3 (which prints the serving Pod’s hostname, IP, and incoming RemoteAddr) behind three Services targeting the same app: echo-app selector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# service-network.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: lab-d4-network
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-app
  namespace: lab-d4-network
spec:
  replicas: 2
  selector:
    matchLabels:
      app: echo-app
  template:
    metadata:
      labels:
        app: echo-app
    spec:
      containers:
        - name: whoami
          image: traefik/whoami:v1.10.3
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: echo-svc
  namespace: lab-d4-network
spec:
  type: ClusterIP
  selector:
    app: echo-app
  ports:
    - port: 80
      targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: echo-nodeport
  namespace: lab-d4-network
spec:
  type: NodePort
  selector:
    app: echo-app
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080
---
apiVersion: v1
kind: Service
metadata:
  name: echo-headless
  namespace: lab-d4-network
spec:
  clusterIP: None
  selector:
    app: echo-app
  ports:
    - port: 80
      targetPort: 80

Apply the manifest and inspect the resulting Pods, Services, and EndpointSlice objects:

1
2
3
kubectl apply -f service-network.yaml
kubectl -n lab-d4-network rollout status deployment/echo-app --timeout=90s
kubectl -n lab-d4-network get pod,svc,endpointslice -o wide
1
2
3
4
5
6
7
8
9
10
11
12
13
NAME                            READY   STATUS    RESTARTS   AGE   IP            NODE
pod/echo-app-5996465d86-frpm7   1/1     Running   0          4s    10.244.1.8    k8s-lab-worker2
pod/echo-app-5996465d86-xvvwv   1/1     Running   0          4s    10.244.2.10   k8s-lab-worker

NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service/echo-headless   ClusterIP   None           <none>        80/TCP         4s    app=echo-app
service/echo-nodeport   NodePort    10.96.79.97    <none>        80:30080/TCP   4s    app=echo-app
service/echo-svc        ClusterIP   10.96.187.60   <none>        80/TCP         4s    app=echo-app

NAME                                                 ADDRESSTYPE   PORTS   ENDPOINTS                AGE
endpointslice.discovery.k8s.io/echo-headless-d4hrx   IPv4          80      10.244.2.10,10.244.1.8   19s
endpointslice.discovery.k8s.io/echo-nodeport-5wc8g   IPv4          80      10.244.2.10,10.244.1.8   19s
endpointslice.discovery.k8s.io/echo-svc-ddwn6        IPv4          80      10.244.1.8,10.244.2.10   19s

The scheduler placed the two Pods (10.244.1.8 and 10.244.2.10) on separate worker nodes. All three EndpointSlice objects track the same two Pod IPs, while only echo-headless shows CLUSTER-IP: None.

2. Compare CoreDNS lookups and ClusterIP distribution

Launch a temporary busybox:1.36 Pod in the same namespace to inspect /etc/resolv.conf, query CoreDNS for both echo-svc and echo-headless, and send 10 requests through ClusterIP:

1
2
3
4
5
6
7
8
kubectl -n lab-d4-network run net-debug --image=busybox:1.36 --restart=Never -- sleep 300
kubectl -n lab-d4-network wait --for=condition=Ready pod/net-debug --timeout=90s

kubectl -n lab-d4-network exec net-debug -- cat /etc/resolv.conf
kubectl -n lab-d4-network exec net-debug -- nslookup echo-svc.lab-d4-network.svc.cluster.local
kubectl -n lab-d4-network exec net-debug -- nslookup echo-headless.lab-d4-network.svc.cluster.local
kubectl -n lab-d4-network exec net-debug -- sh -c \
  'for i in $(seq 1 10); do wget -qO- http://echo-svc.lab-d4-network.svc.cluster.local | grep -E "^Hostname:"; done'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# /etc/resolv.conf
search lab-d4-network.svc.cluster.local svc.cluster.local cluster.local
nameserver 10.96.0.10
options ndots:5

# ClusterIP Service: returns one virtual IP
Server:		10.96.0.10
Address:	10.96.0.10:53
Name:	echo-svc.lab-d4-network.svc.cluster.local
Address: 10.96.187.60

# Headless Service: returns two Pod A records directly
Server:		10.96.0.10
Address:	10.96.0.10:53
Name:	echo-headless.lab-d4-network.svc.cluster.local
Address: 10.244.2.10
Name:	echo-headless.lab-d4-network.svc.cluster.local
Address: 10.244.1.8

# 10 requests via ClusterIP (6 to frpm7, 4 to xvvwv)
Hostname: echo-app-5996465d86-frpm7
Hostname: echo-app-5996465d86-xvvwv
Hostname: echo-app-5996465d86-frpm7
Hostname: echo-app-5996465d86-xvvwv
Hostname: echo-app-5996465d86-frpm7
Hostname: echo-app-5996465d86-frpm7
Hostname: echo-app-5996465d86-xvvwv
Hostname: echo-app-5996465d86-frpm7
Hostname: echo-app-5996465d86-frpm7
Hostname: echo-app-5996465d86-xvvwv

echo-svc resolves to the single virtual IP 10.96.187.60 and spreads the 10 wget requests 6:4 across both Pods. echo-headless bypasses the virtual IP and hands back both Pod IPs directly.

3. Test NodePort from the host and observe SNAT

Next, send six requests from the host to http://localhost:18880, which maps to port 30080 on k8s-lab-control-plane (172.22.0.4). Remember that zero echo-app Pods run on the control-plane node.

1
2
3
4
for i in $(seq 1 6); do
  curl -sS http://localhost:18880 | grep -E "^(Hostname|IP|RemoteAddr):"
  echo "---"
done
1
2
3
4
5
6
7
8
9
Hostname: echo-app-5996465d86-xvvwv
IP: 10.244.2.10
RemoteAddr: 172.22.0.4:47043
---
... (all 6 returned HTTP 200: 5 from xvvwv, 1 from frpm7) ...
---
Hostname: echo-app-5996465d86-frpm7
IP: 10.244.1.8
RemoteAddr: 172.22.0.4:26342

Even though the request enters a node with no local echo-app Pods, kube-proxy forwards it to both worker nodes. Notice RemoteAddr: both Pods see 172.22.0.4 (the control-plane node’s internal IP) instead of the original client IP. Under the default externalTrafficPolicy: Cluster, Kubernetes applies SNAT before forwarding cross-node traffic so return packets route back through the entry node—the same trade-off seen in Inspecting Docker Swarm overlay networks, ingress routing mesh, and VIP vs DNSRR.

4. Delete a Pod and watch EndpointSlice and DNS update

Delete echo-app-5996465d86-frpm7 (10.244.1.8), wait for the ReplicaSet to replace it, and re-check the Service, EndpointSlice, Headless DNS, and short-name requests:

1
2
3
4
5
6
kubectl -n lab-d4-network delete pod echo-app-5996465d86-frpm7
kubectl -n lab-d4-network wait --for=condition=Ready pod -l app=echo-app --timeout=90s
kubectl -n lab-d4-network get pod,svc,endpointslice -o wide
kubectl -n lab-d4-network exec net-debug -- nslookup echo-headless.lab-d4-network.svc.cluster.local
kubectl -n lab-d4-network exec net-debug -- sh -c \
  'for i in $(seq 1 6); do wget -qO- http://echo-svc | grep -E "^Hostname:"; done'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Pod replaced: 10.244.1.8 (frpm7) -> 10.244.1.11 (4z6ng)
NAME                            READY   STATUS    RESTARTS   AGE   IP            NODE
pod/echo-app-5996465d86-4z6ng   1/1     Running   0          1s    10.244.1.11   k8s-lab-worker2
pod/echo-app-5996465d86-xvvwv   1/1     Running   0          33s   10.244.2.10   k8s-lab-worker

# ClusterIP (10.96.187.60) stays unchanged; EndpointSlices update to 10.244.1.11
NAME                    TYPE        CLUSTER-IP     PORT(S)
service/echo-svc        ClusterIP   10.96.187.60   80/TCP

NAME                                                 ADDRESSTYPE   PORTS   ENDPOINTS
endpointslice.discovery.k8s.io/echo-headless-d4hrx   IPv4          80      10.244.2.10,10.244.1.11
endpointslice.discovery.k8s.io/echo-nodeport-5wc8g   IPv4          80      10.244.2.10,10.244.1.11
endpointslice.discovery.k8s.io/echo-svc-ddwn6        IPv4          80      10.244.2.10,10.244.1.11

# Headless DNS reflects the new Pod IP immediately
Name:	echo-headless.lab-d4-network.svc.cluster.local
Address: 10.244.1.11
Name:	echo-headless.lab-d4-network.svc.cluster.local
Address: 10.244.2.10

# Short service name (http://echo-svc): all 6 requests succeed across both Pods
Hostname: echo-app-5996465d86-xvvwv
Hostname: echo-app-5996465d86-4z6ng
Hostname: echo-app-5996465d86-xvvwv
Hostname: echo-app-5996465d86-4z6ng
Hostname: echo-app-5996465d86-xvvwv
Hostname: echo-app-5996465d86-xvvwv

The replacement Pod 4z6ng came up with IP 10.244.1.11 in about one second. echo-svc kept its ClusterIP (10.96.187.60), all three EndpointSlice objects and echo-headless DNS switched to 10.244.1.11, and all six requests to http://echo-svc succeeded without client-side changes.

Production pitfalls and checklist

Before rolling Services into production, keep two operational behaviors in mind.

External DNS lookup amplification from ndots:5: Because /etc/resolv.conf defaults to options ndots:5, any external hostname with fewer than five dots (such as api.example.com) triggers search-domain queries against lab-d4-network.svc.cluster.local, svc.cluster.local, and cluster.local first. That produces up to three NXDOMAIN round-trips per lookup before reaching upstream DNS. For external-heavy services, append a trailing dot (api.example.com.) or lower ndots via Pod dnsConfig.

ClusterIP balances L4 connections, not L7 requests: kube-proxy picks a backend Pod when a TCP connection opens. Long-lived HTTP/1.1 keep-alive pools or HTTP/2 (gRPC) streams stay pinned to the original Pod even after you scale out. Use an L7 proxy (Gateway API or a service mesh) or Headless Service with client-side load balancing when per-request distribution matters.

Run through this quick checklist before deploying a new Service:

  • Verify selector matches Pod labels: A typo leaves EndpointSlice ENDPOINTS empty (<none>) and causes immediate connection refused errors.
  • Check whether you need real client IPs: If audit logs or allowlists depend on source IP, default externalTrafficPolicy: Cluster SNAT hides it; plan externalTrafficPolicy: Local together with load balancer node health checks.
  • Match Service type to workload state: Use ClusterIP for stateless internal APIs and Headless Services with StatefulSets when peers need stable per-Pod DNS records.
  • Watch CoreDNS QPS on external API callers: Monitor NXDOMAIN spikes and tune ndots or use trailing-dot FQDNs where external lookups dominate.

References

Expose an application running in your cluster behind a single outward-facing endpoint, even when the workload is split across multiple backends.
Your workload can discover Services within your cluster using DNS; this page explains how that works.
The EndpointSlice API is the mechanism that Kubernetes uses to let your Service scale to handle large numbers of backends, and allows the cluster to update its list of healthy backends efficiently.
This post is licensed under CC BY 4.0 by the author.