Kubernetes: Service Discovery and DNS
Service discovery is how pods find each other by name. A pod calls the Service name, and CoreDNS turns that name into the Service address.
Ask for a Service by name
The web pod asks for the name api. CoreDNS answers with the api Service address. The web pod never needs to know any pod address.
kubectl exec -it web-7d9f4b6c8d-abcde -- nslookup apiThe short name works too
Pods in the same namespace can use the short name. The full name adds the namespace and the cluster domain. Both names point at the same Service.
kubectl exec -it web-7d9f4b6c8d-abcde -- getent hosts apiThe name matches the Service
The lookup answer is the Service cluster address. The Service forwards the traffic to a live api pod. The name and the address agree.
kubectl get service apiExample
kubectl exec -it web-7d9f4b6c8d-abcde -- nslookup apiKey points
- Service discovery: the way pods find each other by name instead of by address
- CoreDNS: the cluster service that answers name lookups inside the cluster
- Use a Service name as the address when one pod calls another
- Use the full name service.namespace.svc.cluster.local when namespaces differ
- Use the short name when both pods share one namespace
In this example, the command runs nslookup inside the web pod for the name api. The answer is the api Service address, 10.96.130.78. The web pod can now reach the api app by name instead of by a changing pod address.
Note
pod addresses still change, and the Service name is what stays stable
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
How does one pod reach another app inside the cluster without knowing any pod address?