Skip to content
LF Cert Trainer

Lesson 115 · Section 9: Networking

Lesson 115: DNS in Kubernetes: the names that Services and Pods get

Which objects get a DNS name in a cluster and how it is built: Service names with namespace, type and cluster domain, why short names work only inside a namespace, and the records for headless Services, StatefulSet Pods and plain Pods. · 18 min read · 2 min video

1/8DNS in Kubernetes
0:00 / 2:25

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why a cluster needs its own DNS

A Service solves the problem of changing Pod addresses by giving a group of Pods one stable IP. But that IP is still picked by the API server when the Service is created. You cannot know 10.96.14.20 in advance, it differs between the test and production clusters, and it changes if the Service is ever recreated. An application whose configuration says DB_HOST=10.96.14.20 is tied to one cluster on one day.

Names fix that. Every Kubernetes cluster runs a DNS server as an add-on, and that server knows every Service. The same manifest with DB_HOST=db works in every namespace and every cluster, because the name is resolved where the application runs.

The DNS lesson earlier in this section covered how name resolution works on Linux. This lesson is about which names exist in a cluster and how they are built. The next lesson opens up the server behind them, CoreDNS.

What the names look like

Anatomy of a Service name...resolves towebService nameappsnamespacesvckind of recordcluster.localcluster domain10.96.14.20the ClusterIP
Anatomy of a Service name. Read it from left to right: the Service, its namespace, the word svc, and the cluster domain. Together they resolve to the Service's ClusterIP.

Parts:

  • web (Service name)
  • apps (namespace)
  • svc (kind of record)
  • cluster.local (cluster domain)
  • 10.96.14.20 (the ClusterIP)

Connections:

  • web to apps: .
  • apps to svc: .
  • svc to cluster.local: .
  • cluster.local to 10.96.14.20: resolves to
PartExampleMeaning
HostnamewebThe name of the Service
NamespaceappsThe namespace the Service is in. Each namespace is a subdomain
TypesvcAll Services live under svc. Pod records live under pod
Cluster domaincluster.localThe root of the cluster's names. A setting of the cluster, the same for every Pod

Put together: web.apps.svc.cluster.local. This is the fully qualified domain name (FQDN) of the Service, and it resolves to the Service's ClusterIP from any Pod in the cluster.

The DNS server creates these records by itself. It watches the API server, so a new Service has a name within moments of kubectl expose, and the name disappears when the Service is deleted. You never edit a DNS zone.

How it works

Short names and namespaces

How much of the name you must typeweb + own namespaceweb.appsA recordnot askedPod in appscurl webPod in defaultcurl web.appsNode shellname not knownCluster DNSkube-dns ServiceService webnamespace apps
How much of the name you must type. A Pod's resolver completes short names with its own namespace first. From another namespace you must add the Service's namespace. Nodes do not use cluster DNS at all.

Parts:

  • Pod in apps (curl web)
  • Pod in default (curl web.apps)
  • Node shell (name not known)
  • Cluster DNS (kube-dns Service)
  • Service web (namespace apps)

Connections:

  • Pod in apps to Cluster DNS: web + own namespace
  • Pod in default to Cluster DNS: web.apps
  • Cluster DNS to Service web: A record
  • Node shell to Cluster DNS: not asked

Typing the full name each time would be tedious, and it would also defeat the idea that one manifest works in every namespace. So the kubelet configures each Pod's resolver with a search list: suffixes that are tried, in order, when a name is not fully qualified. For a Pod in namespace default:

search default.svc.cluster.local svc.cluster.local cluster.local

When that Pod looks up web.apps, the resolver tries web.apps.default.svc.cluster.local (no such name), then web.apps.svc.cluster.local, which exists. The result is a simple rule:

The client Pod is inName to use for Service web in appsWhy it works
apps (same namespace)webFirst search suffix is the Pod's own namespace
any other namespaceweb.appsSecond suffix adds svc.cluster.local
anyweb.apps.svcThird suffix adds cluster.local
anyweb.apps.svc.cluster.localAlready complete

Two forms that look plausible and do not work: web from another namespace (it is completed with the wrong namespace, and may even hit a different Service of the same name there), and web.apps.svc.cluster (no suffix turns that into a real name).

Records for Services

What a name resolves toAAAweb.apps.svcnormal Service10.96.14.20one ClusterIPdb.apps.svcclusterIP: None10.244.1.9, 2.4all ready Pod IPsdb-0.db.apps.svcStatefulSet Pod10.244.1.9that one Pod
What a name resolves to. A normal Service name gives one stable virtual IP. A headless Service name gives the Pod IPs themselves, and with a StatefulSet each Pod gets a name of its own.

Parts:

  • web.apps.svc (normal Service)
  • 10.96.14.20 (one ClusterIP)
  • db.apps.svc (clusterIP: None)
  • 10.244.1.9, 2.4 (all ready Pod IPs)
  • db-0.db.apps.svc (StatefulSet Pod)
  • 10.244.1.9 (that one Pod)

Connections:

  • web.apps.svc to 10.96.14.20: A
  • db.apps.svc to 10.244.1.9, 2.4: A
  • db-0.db.apps.svc to 10.244.1.9: A
Kind of ServiceWhat <service>.<namespace>.svc.cluster.local returns
ClusterIP, NodePort, LoadBalancerOne A record (AAAA for IPv6): the ClusterIP
Headless (clusterIP: None)One A record per ready Pod: the Pod IPs. The client chooses and connects straight to a Pod
ExternalNameA CNAME to the name in spec.externalName, for example db.example.com

Services also get SRV records for named ports, in the form _<port-name>._<protocol>.<service>.<namespace>.svc.cluster.local. An SRV answer carries the port number, so a client can discover the port as well as the address. And for each ClusterIP there is a reverse (PTR) record, which is why nslookup 10.96.14.20 answers with the Service's name.

Records for Pods

A plain Pod gets one kind of record, built from its IP address with the dots replaced by dashes:

10-244-1-9.apps.pod.cluster.local      ->  10.244.1.9

It is rarely useful: to build the name you must already know the IP. The important point is the negative one: the Pod's name is not in DNS. After kubectl run web --image=nginx, no Pod can resolve web.

Pods get a real, stable name in two ways, and both go through a Service:

  • A Service in front of the Pod. The normal case. Clients use the Service name.
  • hostname and subdomain with a headless Service. If a Pod sets spec.subdomain to the name of a headless Service in its namespace, it gets the record <hostname>.<subdomain>.<namespace>.svc.cluster.local. A StatefulSet does this for you: its Pods db-0, db-1 behind the headless Service db are reachable as db-0.db.apps.svc.cluster.local, and each keeps that name when it is recreated. That is how members of a database cluster find each other.

Who answers

Inside a Pod, lookups go to one address: the ClusterIP of the Service kube-dns in kube-system (10.96.0.10 on a kubeadm cluster). Behind it run the CoreDNS Pods. Names under the cluster domain are answered from what CoreDNS knows about Services and Pods. Any other name, such as github.com, is passed on to the DNS servers of the node. So one resolver setting gives a Pod both worlds.

Under the hood · Details worth knowing
  • The cluster domain can differ. cluster.local is a default, set on the kubelet as clusterDomain. Read it from a Pod's search list instead of assuming it when a task says "the cluster's domain".
  • Only ready Pods are in a headless answer, unless the Service sets publishNotReadyAddresses: true.
  • Service-scoped Pod records. CoreDNS also answers 10-244-1-9.web.apps.svc.cluster.local for a Pod that is an endpoint of Service web.
  • Environment variables are the older mechanism. A container also gets WEB_SERVICE_HOST and WEB_SERVICE_PORT for each Service that existed in its namespace when it started. They do not cross namespaces and do not update, which is why DNS is the method to use.
  • Names are valid DNS labels. Service and namespace names are limited to lowercase letters, digits and dashes precisely so that they can be used as parts of a DNS name.
  • Caching. Records for Services have a short lifetime (30 seconds with the kubeadm defaults). A deleted Service can stay resolvable for that long.

Using it

Example · Connect an application to a Service in another namespace

Task: "The webapp Deployment in namespace default cannot reach its database. The database is the Service mysql in namespace payroll. Fix the Deployment."

Look at what the application uses now:

kubectl get deploy webapp -o yaml | grep -A 1 DB_HOST
kubectl get svc -n payroll
        - name: DB_HOST
          value: mysql
NAME    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
mysql   ClusterIP   10.103.132.104  <none>        3306/TCP   9m

mysql alone is completed to mysql.default.svc.cluster.local, which does not exist. Add the namespace:

kubectl set env deploy/webapp DB_HOST=mysql.payroll
kubectl rollout status deploy/webapp
deployment.apps/webapp env updated
deployment "webapp" successfully rolled out

mysql.payroll, mysql.payroll.svc and mysql.payroll.svc.cluster.local are all correct. The shortest one that works is the least to mistype.

Example · Check what a name resolves to

Use a throwaway Pod, in the namespace of the client you are imitating:

kubectl run dns-test --rm -it --restart=Never --image=busybox:1.28 -- nslookup web.apps
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      web.apps
Address 1: 10.96.14.20 web.apps.svc.cluster.local
pod "dns-test" deleted

The first block names the server that answered: the kube-dns Service. The second gives the address and, after it, the full name that matched. Compare the address with kubectl get svc -n apps web.

The same against a name that does not exist:

nslookup: can't resolve 'web'

Example · See the difference a headless Service makes

A StatefulSet db with two replicas and its headless Service:

apiVersion: v1
kind: Service
metadata:
  name: db
  namespace: apps
spec:
  clusterIP: None            # headless: no virtual IP
  selector:
    app: db
  ports:
    - name: mysql
      port: 3306
kubectl -n apps get svc db
kubectl -n apps run dns-test --rm -it --restart=Never --image=busybox:1.28 -- nslookup db
NAME   TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
db     ClusterIP   None         <none>        3306/TCP   2m

Name:      db
Address 1: 10.244.1.9 db-0.db.apps.svc.cluster.local
Address 2: 10.244.2.4 db-1.db.apps.svc.cluster.local

One name, two answers, both Pod IPs, and each Pod has its own name. db-0.db (from inside apps) always means the first replica:

kubectl -n apps run dns-test --rm -it --restart=Never --image=busybox:1.28 -- nslookup db-0.db
Name:      db-0.db
Address 1: 10.244.1.9 db-0.db.apps.svc.cluster.local

Example · Write a lookup result to a file

A frequent exam shape: "Look up the Service web in namespace apps and its Pod, and save the output."

kubectl run dns-test --image=busybox:1.28 --restart=Never -- sleep 3600
kubectl exec dns-test -- nslookup web.apps > /root/web.svc
kubectl get pod -n apps -o wide          # the Pod IP is 10.244.1.9
kubectl exec dns-test -- nslookup 10-244-1-9.apps.pod > /root/web.pod
cat /root/web.pod
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      10-244-1-9.apps.pod
Address 1: 10.244.1.9 10-244-1-9.web.apps.svc.cluster.local

The redirect > runs in your shell, not in the Pod, so the file lands where the task wants it. Without -it, no terminal characters end up in the file.

Tricks

Common mistakes

  • Using the bare Service name across namespaces. It is completed with the client's namespace.
  • Using a Pod's name as a hostname. Pod names are not in DNS. Create a Service.
  • Using a Deployment's name. Only the Service name counts, and it can differ from the Deployment's.
  • Testing from the node. Nodes do not use cluster DNS.
  • Writing service.namespace.cluster.local. The svc part is missing. Stop at service.namespace or write the whole name.
  • Treating "resolves" as "works". DNS only gives the address. A wrong port or a Service without endpoints fails after a successful lookup.

Recap

  • The cluster's DNS creates a record for every Service automatically: <service>.<namespace>.svc.cluster.local.
  • A Pod's search list completes short names: service in the same namespace, service.namespace from anywhere.
  • Normal Services resolve to the ClusterIP. Headless Services resolve to the ready Pod IPs.
  • Plain Pods only get <ip-with-dashes>.<namespace>.pod.cluster.local. StatefulSet Pods get <pod>.<service>.<namespace>.svc.cluster.local.
  • Lookups from Pods go to the kube-dns Service. Nodes are not part of it.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Work out the DNS names of a Service, a StatefulSet pod and a plain pod, and repair a job that looks for a Service in the wrong namespace.

    Investigate · Level 2 · standard · target 15 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.