Skip to content
tezvyn:

Kubernetes DNS: How Pods Find Each Other

Source: kubernetes.ioEasyHow cards are made

Kubernetes DNS: How Pods Find Each Other

Kubernetes DNS gives services and pods stable, human-readable names so you don't have to track ephemeral IP addresses. It's how a frontend pod finds a backend service.

Why it exists

Pods in Kubernetes are ephemeral; they can be created, destroyed, and moved at any time. This means their IP addresses are constantly changing. Hardcoding IP addresses for communication between application components would be brittle and unmanageable. Kubernetes DNS was created to provide a stable, name-based service discovery mechanism inside the cluster.

The mental model

Think of Kubernetes DNS as your cluster's internal, automatic phonebook. When you create a Service named api-server in the production namespace, K8s DNS automatically creates an entry for it. Any pod in that namespace can now simply "call" api-server by name, and K8s will route the traffic to the correct, healthy backend pods, no matter what their current IPs are.

How it works

Kubernetes runs a dedicated DNS service within the cluster, usually CoreDNS. When a pod starts, its DNS configuration is automatically set to query this internal DNS service. For a Service named my-svc in namespace my-ns, Kubernetes creates DNS A/AAAA records. A pod in the same namespace can resolve my-svc directly. A pod in a different namespace must use the fully qualified domain name (FQDN): my-svc.my-ns.svc.cluster.local. Pods also get a DNS name, typically in the format pod-ip-address.namespace.pod.cluster.local.

When to use it

This is the default and recommended way for applications inside a cluster to communicate with each other. It's the foundation for most microservice architectures on Kubernetes, used for frontend-to-backend communication, inter-service calls, and connecting applications to databases running as Services within the same cluster.

When not to use it

You wouldn't use the internal cluster DNS for external clients trying to reach your services; that's the job of an Ingress or a Service of type LoadBalancer. It's also generally an anti-pattern to try to bypass it for static IP needs within the cluster, as this fights against Kubernetes's dynamic nature.

One canonical example

A webapp pod needs to talk to a database service. The database service is defined in YAML with the name db-main in the same namespace. The webapp's connection string can be configured to use the hostname db-main. Kubernetes DNS will resolve db-main to the Service's stable ClusterIP, which then load-balances traffic to the healthy database pods. If a database pod fails and is replaced, its new IP is updated automatically, but the db-main name remains the stable endpoint, requiring no changes to the webapp.

Interview question

What is the primary benefit of using Kubernetes DNS for internal service communication?

  • a.It ensures that external clients can easily discover and connect to services by name.
  • b.It encrypts all traffic between pods, enhancing security within the cluster.
  • c.It provides stable, human-readable names for services and pods, abstracting away their ephemeral IP addresses.Correct
  • d.It assigns permanent, unchanging IP addresses to all pods and services.
Why?

The core purpose of Kubernetes DNS is to provide stable, resolvable names for services and pods, which are inherently ephemeral and have constantly changing IP addresses. This allows applications to communicate reliably by name. Option A is incorrect because Kubernetes DNS is explicitly for internal cluster communication, not for external client access.

Just read this? Test yourself on what you have been reading.

Read the original → kubernetes.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kubernetes — each one lists the topics its interview covers.

See open roles