Cluster DNS & Service Discovery

Kubernetes runs an internal DNS so Pods can find Services by name instead of IP.

Every cluster runs a DNS service (usually CoreDNS) that turns Service names into IP addresses. This is how service discovery works with zero configuration.

The naming pattern

Each Service gets a DNS record of the form:

<service>.<namespace>.svc.cluster.local
  • Same namespace? Just use the short name: backend.
  • Different namespace? Add it: backend.payments.
  • Fully qualified: backend.payments.svc.cluster.local.

Why it matters

Because names are stable while Pod IPs are not, your code should always connect to Services by name. DNS keeps working as Pods are rescheduled.

Example

Example · bash
# From inside a Pod, resolve a Service by name
kubectl exec -it my-pod -- nslookup backend

# Cross-namespace call from application code:
# curl http://backend.payments.svc.cluster.local:8080/health

# Inspect the DNS service itself
kubectl get pods -n kube-system -l k8s-app=kube-dns

When to use it

  • A microservice connects to a database by name (postgres-svc.production.svc.cluster.local) instead of an IP, so the connection survives pod restarts and IP changes.
  • A developer troubleshoots a connection timeout by exec-ing into a pod and using nslookup to verify the target service's DNS resolves correctly.
  • An operator configures ndots in a pod's dnsConfig to reduce DNS lookup latency by avoiding unnecessary FQDN searches for short service names.

More examples

Resolve a service by DNS name

Runs nslookup inside a pod to verify that CoreDNS resolves a Service name to its cluster IP correctly.

Example · bash
kubectl exec -it my-pod -- nslookup api-svc
# Server: 10.96.0.10 (CoreDNS)
# Address: 10.96.0.10#53
# Name: api-svc.default.svc.cluster.local
# Address: 10.100.45.22

FQDN format for cross-namespace calls

Demonstrates the DNS format difference between same-namespace (short name) and cross-namespace (FQDN) Service resolution.

Example · bash
# Short name works in the same namespace
curl http://api-svc/health

# FQDN needed from a different namespace
curl http://api-svc.production.svc.cluster.local/health

# Pattern: <service>.<namespace>.svc.cluster.local

Custom DNS config in pod

Reduces ndots to 2 so short names are resolved as absolute DNS lookups faster, improving latency for service calls.

Example · yaml
spec:
  dnsConfig:
    options:
      - name: ndots
        value: "2"
      - name: timeout
        value: "5"
  dnsPolicy: ClusterFirst

Discussion

  • Be the first to comment on this lesson.