Service Discovery

Difficulty: Intermediate

Question

How does service discovery work inside a Kubernetes cluster? How does one Pod find and talk to another service?

Answer

Kubernetes does service discovery mainly through cluster DNS. CoreDNS watches the API server and gives every Service a name like <service>.<namespace>.svc.cluster.local, which resolves to the Service's stable ClusterIP. kube-proxy (or the CNI's eBPF dataplane) then load balances traffic sent to that ClusterIP across the healthy Pod IPs listed in the Service's EndpointSlices.

The pieces that make a name resolve

  • Service: a stable virtual IP (ClusterIP) and port that fronts a set of Pods picked by a label selector.
  • EndpointSlice: the endpoints controller keeps a list of ready Pod IPs behind each Service. Pods that fail their readiness probe are marked not ready and stop receiving traffic.
  • CoreDNS: runs as a Deployment in kube-system, exposed through a Service usually still called kube-dns. It answers DNS queries from what it sees in the API.
  • kube-proxy: programs iptables, IPVS or nftables rules on every node so that packets to the ClusterIP are rewritten to one backing Pod IP. Cilium and similar CNIs can replace kube-proxy with eBPF.

So DNS gives you a stable name to IP mapping, and kube-proxy turns that one virtual IP into many real Pods.

What a Pod's DNS config looks like

With the default dnsPolicy: ClusterFirst, the kubelet writes /etc/resolv.conf in each Pod with the cluster DNS IP as nameserver and search domains such as <ns>.svc.cluster.local svc.cluster.local cluster.local, plus options ndots:5. This is why:

  • payments works from a Pod in the same namespace.
  • payments.billing works from any namespace.
  • payments.billing.svc.cluster.local is the fully qualified name and always works.

The ndots:5 setting means a name with fewer than five dots is tried against every search domain first, so a lookup for api.razorpay.com may generate several failed internal queries before going out. For chatty services, use a trailing dot (api.razorpay.com.) or tune dnsConfig to cut that overhead.

Record types you should know

Service typeDNS answer
Normal ClusterIPA/AAAA record pointing to the ClusterIP
Headless (clusterIP: None)A records for each ready Pod IP, no load balancing by kube-proxy
StatefulSet behind headless ServiceStable per-Pod names like mysql-0.mysql.db.svc.cluster.local
ExternalNameCNAME to an outside hostname, e.g. a managed database endpoint
Named portsSRV records like _http._tcp.payments.billing.svc.cluster.local

Kubernetes also injects environment variables such as PAYMENTS_SERVICE_HOST and PAYMENTS_SERVICE_PORT, but only for Services that existed before the Pod started. That ordering problem is why DNS is the recommended mechanism.

Common gotchas in real clusters

  • Wrong namespace: calling orders from the checkout namespace when the Service lives in orders-prod. Use orders.orders-prod.
  • No endpoints: the Service name resolves but connections fail because the selector does not match any Pod labels, or every Pod is failing readiness. Check kubectl get endpointslices -l kubernetes.io/service-name=orders.
  • Pinging a ClusterIP: a ClusterIP is virtual and usually does not answer ICMP, so ping fails even when HTTP works.
  • Client-side caching: some runtimes (older JVM settings, for example) cache DNS for a long time, which hurts with headless Services where Pod IPs change.
  • CoreDNS overload: at large scale, NodeLocal DNSCache is often added to run a DNS cache on each node and reduce latency and conntrack issues.

In a setup like Swiggy's order flow, the checkout service simply calls http://order-service.orders:8080. Pods behind it can be rescheduled, scaled from 3 to 30 or rolled to a new version, and the caller never changes its config.

How a call to a Service name reaches a Pod. DNS resolves the name to a ClusterIP, then kube-proxy rules route it to one ready Pod.
  1. Client Pod calls order-service: The app uses a DNS name, not an IP.
  2. CoreDNS resolves the name: Returns the Service's ClusterIP from API data.
  3. Packet sent to ClusterIP: The virtual IP does not belong to any real interface.
  4. kube-proxy rules on the node: iptables, IPVS or nftables pick one endpoint.
  5. Ready backend Pod: Chosen from the Service's EndpointSlices.

Code examples

Service plus DNS lookups from a debug Pod

# A simple ClusterIP Service in the "orders" namespace
kubectl -n orders expose deployment order-service --port=8080 --target-port=8080

# Start a throwaway Pod with DNS tools
kubectl run dns-test --rm -it --image=busybox:1.36 --restart=Never -- sh

# Inside the Pod:
cat /etc/resolv.conf                       # nameserver + search domains + ndots:5
nslookup order-service.orders              # short form across namespaces
nslookup order-service.orders.svc.cluster.local
wget -qO- http://order-service.orders:8080/health

# Back on your machine: which Pod IPs are behind the Service?
kubectl -n orders get endpointslices -l kubernetes.io/service-name=order-service

This is the standard way to debug discovery: confirm resolv.conf, confirm the name resolves to the ClusterIP, then confirm the Service actually has ready endpoints.

Headless Service for stable per-Pod DNS

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: db
spec:
  clusterIP: None        # headless: DNS returns Pod IPs directly
  selector:
    app: mysql
  ports:
    - name: mysql
      port: 3306
# With a StatefulSet using serviceName: mysql, each replica gets
# mysql-0.mysql.db.svc.cluster.local, mysql-1.mysql.db.svc.cluster.local, ...

Headless Services skip the ClusterIP and kube-proxy load balancing, which is what databases and other stateful systems need when clients must reach a specific replica such as the primary.

Key points

  • Service DNS format is `<service>.<namespace>.svc.cluster.local`; CoreDNS is the default DNS server.
  • DNS gives a stable ClusterIP; kube-proxy or an eBPF dataplane spreads traffic over ready Pods in EndpointSlices.
  • Headless Services (`clusterIP: None`) return Pod IPs and give StatefulSet Pods stable names.
  • Environment variable discovery only covers Services created before the Pod, so prefer DNS.
  • `ndots:5` can multiply DNS queries for external names; NodeLocal DNSCache helps at scale.

Common follow-up questions

  • What is the difference between a ClusterIP Service and a headless Service?
  • How would you debug a Service whose name resolves but returns connection refused?
  • How does kube-proxy in iptables mode differ from IPVS or nftables mode?
  • How do Pods in one cluster discover services in another cluster?
  • What does `ndots:5` do and why can it cause DNS latency?

Interview tips

  • Separate the two layers clearly: DNS maps name to ClusterIP, kube-proxy maps ClusterIP to Pod. Many candidates blur them.
  • Mention EndpointSlices and readiness probes; it shows you know why traffic stops going to unhealthy Pods.
  • Walk through your debugging order (resolv.conf, nslookup, endpoints, then the app) if asked about a broken Service.

Concepts covered

CoreDNS, ClusterIP, Headless Service, EndpointSlice, resolv.conf search domains

More Kubernetes & Orchestration questions