Kubernetes Tutorial · Networking · Lesson 14 of 26
DNS and Service Discovery
How pods find Services by name: cluster DNS names, namespaces and search domains.
Names
Cluster DNS (CoreDNS) gives every Service a name:
webfrom pods in the same namespace,web.shopfrom other namespaces,web.shop.svc.cluster.localfully qualified.
Pods' /etc/resolv.conf contains search domains, which is why short names work. Applications should use these
names (from configuration), never pod IPs.
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?
By the name cacheBy the name cache.data (or cache.data.svc.cluster.local)Only by IPIt 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.