Difficulty: Intermediate
How does service discovery work inside a Kubernetes cluster? How does one Pod find and talk to another service?
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.
kube-system, exposed through a Service usually still called kube-dns. It answers DNS queries from what it sees in the API.So DNS gives you a stable name to IP mapping, and kube-proxy turns that one virtual IP into many real Pods.
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.
| Service type | DNS answer |
|---|---|
| Normal ClusterIP | A/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 Service | Stable per-Pod names like mysql-0.mysql.db.svc.cluster.local |
| ExternalName | CNAME to an outside hostname, e.g. a managed database endpoint |
| Named ports | SRV 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.
orders from the checkout namespace when the Service lives in orders-prod. Use orders.orders-prod.kubectl get endpointslices -l kubernetes.io/service-name=orders.ping fails even when HTTP works.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.
# 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.
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.
CoreDNS, ClusterIP, Headless Service, EndpointSlice, resolv.conf search domains