Kubernetes: How to allow two pods running in same/different namespace communicate irrespective of the protocol using a servicename?
By default, pods can communicate with each other by their IP address, regardless of the namespace they're in.
You can see the IP address of each pod with:
kubectl get pods -o wide --all-namespaces
However, the normal way to communicate within a cluster is through Service resources.
A Service also has an IP address and additionally a DNS name. A Service is backed by a set of pods. The Service forwards requests to itself to one of the backing pods.
The fully qualified DNS name of a Service is:
<service-name>.<service-namespace>.svc.cluster.local
This can be resolved to the IP address of the Service from anywhere in the cluster (regardless of namespace).
For example, if you have:
- Namespace
ns-a
: Servicesvc-a
→ set of pods A - Namespace
ns-b
: Servicesvc-b
→ set of pods B
Then a pod of set A can reach a pod of set B by making a request to:
svc-b.ns-b.svc.cluster.local
You can put the Pods behind Services and use Service DNS for communication. Calls to service-name
allow Pods in the same namespace to communicate. Calls to service-name.namespace
allow Pods in different namespaces to communicate.