How can containers in a pod refer to each other by name?

11/18/2016

In short I have two containers in a pod, named a and b, on ports 80 and 9000 respectively. They can reach each other through 127.0.0.1:80 and 127.0.0.1:9000. Can they also reach each other by name?

I've tried a.podname:90, b.podname:9000, podname:80 and other variations without luck.

There is http://kubernetes.io/docs/admin/dns/#a-records but those are based on the PODs assigned IP which isn't known beforehand.

There is also http://kubernetes.io/docs/admin/dns/#srv-records but they depend on the service, and thus the readiness criteria. Also SRV is a DNS extension that can sometimes not be used.

Background: To keep containers compatible with both Kubernetes and other docker environments you must avoid dependencies to localhost, but can depend on fixed port numbers. For example if there is a name that resolves to 127.0.0.1 or the POD's IP in Kubernetes, that same name can be used as a https://docs.docker.com/compose/compose-file/#/links alias in docker-compose.

In particular I'm trying to solve https://github.com/Reposoft/docker-svn/issues/8

-- solsson
kubernetes

1 Answer

11/19/2016

If you're always going to run the two containers in a pod, you can rely on localhost:xyz to be accessible, no matter where that pod gets scheduled.

If you want to use DNS names, you can create a headless service for the SRV records. You can override the readiness behavior using an annotation: service.alpha.kubernetes.io/tolerate-unready-endpoints. That should give you a DNS name to reach your pod.

-- Anirudh Ramanathan
Source: StackOverflow