Kubernetes Tutorial · Networking · Lesson 14 of 26

DNS and Service Discovery

How pods find Services by name: cluster DNS names, namespaces and search domains.

Create a free account to save this page, take notes and track your progress. Sign in

Names

Cluster DNS (CoreDNS) gives every Service a name:

  • web from pods in the same namespace,
  • web.shop from other namespaces,
  • web.shop.svc.cluster.local fully qualified.

Pods' /etc/resolv.conf contains search domains, which is why short names work. Applications should use these names (from configuration), never pod IPs.

Short and fully qualified names
kubectl create namespace demo-dns > /dev/null
trap 'kubectl delete namespace demo-dns --wait=false > /dev/null' EXIT
kubectl -n demo-dns create deployment db --image=redis:8-alpine > /dev/null
kubectl -n demo-dns expose deployment db --port=6379 > /dev/null
kubectl -n demo-dns rollout status deployment/db --timeout=120s > /dev/null
kubectl -n demo-dns run client --restart=Never --image=redis:8-alpine -- sh -c '
  grep ^search /etc/resolv.conf
  for name in db db.demo-dns db.demo-dns.svc.cluster.local; do
    echo "redis-cli -h $name ping -> $(redis-cli -h $name ping)"
  done' > /dev/null
kubectl -n demo-dns wait --for=jsonpath='{.status.phase}'=Succeeded pod/client --timeout=120s > /dev/null
kubectl -n demo-dns logs client

Output

search demo-dns.svc.cluster.local svc.cluster.local cluster.local
redis-cli -h db ping -> PONG
redis-cli -h db.demo-dns ping -> PONG
redis-cli -h db.demo-dns.svc.cluster.local ping -> PONG

Run this on your computer — see “Get Started” for setup.

Test yourself

How does a pod in namespace web reach Service cache in namespace data?

  1. By the name cache
  2. By the name cache.data (or cache.data.svc.cluster.local)
  3. Only by IP
  4. It can't
Show answer

Answer: By the name cache.data (or cache.data.svc.cluster.local)

Short names resolve within the pod's own namespace only.

Was this page helpful?