Sub topic 6
Reaching Services across namespaces
This chapter assumes you can create Pods, Deployments, and Services, run kubectl, and read simple YAML. It also assumes you know that a namespace scopes object names.
Why the name depends on where you are
A Service name is unique only inside its namespace. Two teams can each have a Service called orders, one in team-a and one in team-b. When a Pod calls orders, Kubernetes needs a way to decide which one is meant.
The cluster DNS server (usually CoreDNS) answers that question. Every Service gets a DNS record in this form:
<service>.<namespace>.svc.<cluster-domain>The default cluster domain is cluster.local. So the orders Service in team-b is orders.team-b.svc.cluster.local. This is its fully qualified domain name (FQDN).
How short names resolve
Kubernetes writes a search list into each Pod's /etc/resolv.conf. For a Pod in team-a, it looks like this:
search team-a.svc.cluster.local svc.cluster.local cluster.localWhen the Pod looks up a short name, the resolver appends each search suffix in order until one succeeds.
ordersbecomesorders.team-a.svc.cluster.local, so it finds the Service in the Pod's own namespace.orders.team-bbecomesorders.team-b.svc.cluster.local, so it finds the Service inteam-b.
Calling a Service in another namespace
From a Pod in any namespace, you can reach the orders Service in team-b by its FQDN:
curl http://orders.team-b.svc.cluster.localYou can test this without editing any workload. Start a temporary Pod in team-a, then call the Service from it:
kubectl run tmp --namespace team-a --rm -it --restart=Never \
--image=curlimages/curl --command -- \
curl -s http://orders.team-b.svc.cluster.localThe --rm flag deletes the Pod when the command ends. The URL has no port, so curl uses port 80. If the Service listens on another port, add it, for example :8080.
To see which Services exist in the target namespace:
kubectl get services --namespace team-bChoosing a name form
| Form | Example | Use when |
|---|---|---|
| Short name | `orders` | The caller and the Service share a namespace. |
| Namespace-qualified | `orders.team-b` | You cross namespaces and the cluster domain is the default. |
| FQDN | `orders.team-b.svc.cluster.local` | You want a name that does not depend on the search list. |
The FQDN is the most explicit choice. Check your cluster's domain before hardcoding it, because administrators can change it from cluster.local.
What namespaces do not block
Namespaces organize names. They do not stop traffic. By default, any Pod can reach any Service in any namespace, provided it knows the name or the cluster IP.
Common errors
Could not resolve host: The namespace or Service name is misspelled, or the Service does not exist yet. Runkubectl get services --namespace team-bto check.- Wrong Service answers: You used the short name
ordersand got the Service in your own namespace. Add the namespace to reach the other one. - Connection timeout after the name resolves: DNS worked, so the problem is the network. Look for a NetworkPolicy, or check that the Service has ready endpoints with
kubectl get endpoints orders --namespace team-b. - Connection refused: The Service port does not match the port in your URL.
Exercise
A Pod in namespace team-a runs curl http://billing and receives a response from a Service in team-a. A billing Service also exists in team-c. Which name should the Pod use to reach the one in team-c, and why does the short name not reach it?
Show answer
Use billing.team-c, or the FQDN billing.team-c.svc.cluster.local. The Pod's search list tries billing.team-a.svc.cluster.local first, and that record exists, so the short name always resolves to the Service in the Pod's own namespace.