Kubernetes - container communication within a pod using names instead of 'localhost'? Kubernetes - container communication within a pod using names instead of 'localhost'? kubernetes kubernetes

Kubernetes - container communication within a pod using names instead of 'localhost'?


localhost is just a name for the network loopback device (usually 127.0.0.1 for IPv4 and ::1 for IPv6). This is usually specified in your /etc/hosts file.

A pod has its own IP, so each container inside shares that IP. If these containers should be independent (i.e. don't need to be collocated), they should each be in their own pod. Then, you can define a service for each that allows DNS lookups as either "$SERVICENAME" from pods in the same namespace, or "$SERVICENAME.$NAMESPACE" from pods in different namespaces.


docker-compose deploys individual containers, linking them together so they know each other's name and IP.

a Pod in Kubernetes is similar, but this is not the purpose of a Pod to hold multiple external services and link them together.

A Pod is for containers that must be running on the same host, and interact among themselves only. The containers communicate internally via localhost.

Most Pods are in fact a single container.

A Pod communicates with the outside using Services. In essence a Pod appears as if it was just one container.

under the hood, a Pod is at least 2 containers: the pause container manages the IP of the Pod, and then your attached container. This allows your container to crash, restart, and be relinked in the Pod without changing IP, allowing to manage container crashes without involving the scheduler, and making sure the Pod stays on a single node during its lifetime, so restart is fast.

If containers we rescheduled each time they crash, they would potentially end up on a different host, routing would have to be updated etc...


Generally, Containers running inside a pod, shares pod's IP and Port space. The communication between the containers will happen through localhost by default. To communicate between the containers using the name(like DNS), the containers should run in the independent POD and expose it as a service to rest of application world.