Sub topic 9
Service and Pod DNS names across namespaces
This chapter assumes you know basic kubectl usage, Pods, Deployments, and namespaces, YAML manifests, and what an A record is. It covers which DNS names Kubernetes creates for Services and Pods, and which name to use from another namespace.
Service names
Cluster DNS gives every Service an A record. The full name (the fully qualified domain name, or FQDN) has this form:
<service>.<namespace>.svc.<cluster-domain>The default cluster domain is cluster.local. The web Service in the default namespace is therefore web.default.svc.cluster.local. The record resolves to the Service's ClusterIP.
A headless Service (clusterIP: None) has no virtual IP. Its name resolves to the IPs of the ready Pods behind it, so a client gets several A records.
Pod names
Pods get DNS records too, but you rarely use them:
- A Pod's IP-based name replaces the dots in the IP with dashes. Pod
10.244.1.7indefaultis10-244-1-7.default.pod.cluster.local. - Pods behind a headless Service can have a stable name of the form
<hostname>.<service>.<namespace>.svc.cluster.local. This is how StatefulSet Pods get names likedb-0.db.default.svc.cluster.local.
Use Service names for application traffic. Pod IPs change every time a Pod is recreated.
Short names and namespaces
A short name works only when the search domains in the Pod's resolv.conf can complete it. The next chapter covers how that works. For now, remember that the first search domain is the Pod's own namespace.
| Name used | Resolves from the same namespace | Resolves from another namespace |
|---|---|---|
| `web` | Yes | No. It looks for `web` in the caller's own namespace. |
| `web.default` | Yes | Yes |
| `web.default.svc` | Yes | Yes |
| `web.default.svc.cluster.local` | Yes | Yes |
The most common namespace mistake is using a bare name across namespaces. If a Pod in staging calls http://web:8080, DNS looks for web.staging.svc.cluster.local. When that Service doesn't exist, you get NXDOMAIN, which means the name does not exist. The Service may be healthy in default the whole time.
Try it
Run these from a Pod in a namespace other than the Service's. This example assumes the web Service from the earlier chapters is in default.
kubectl create namespace staging
kubectl run tmp-debug -n staging --rm -it --restart=Never --image=busybox:1.36 -- sh
# inside the Pod:
nslookup web
nslookup web.default
nslookup web.default.svc.cluster.localnslookup web fails with NXDOMAIN. The other two return the ClusterIP of the web Service.
Diagnosing name problems
- Confirm the Service exists in the namespace you think it does:
kubectl get service web -n default. - Check which namespace the calling Pod runs in:
kubectl get pod <name> -o jsonpath='{.metadata.namespace}'. - Look at the name in the application's configuration. A bare name across namespaces is the usual fault.
- Query the full FQDN from inside the cluster. If the FQDN resolves and the short name doesn't, the problem is the name your application uses, not the cluster.
- If even the FQDN fails and the Service exists, move on to the
resolv.confand CoreDNS checks in the next chapters.
Exercise
A Pod in namespace payments calls http://ledger:9000. The ledger Service runs in namespace accounting, and kubectl get endpointslices -n accounting shows ready endpoints. The call fails with a name resolution error. What is the cause, and what should the Pod call instead?
Show answer
The bare name ledger is completed with the caller's namespace, so DNS looks up ledger.payments.svc.cluster.local, which does not exist. The Pod should call http://ledger.accounting:9000, or the full name ledger.accounting.svc.cluster.local.