3

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

4
  • Try Petset: kubernetes.io/docs/user-guide/petset
    – Shahriar
    Nov 18, 2016 at 14:38
  • 2
    Why don't you load the address as an external config (e.g. env var). In the Kubernetes env you can pass in localhost and in the other Docker env you can pass in the correct name. Nov 18, 2016 at 15:42
  • 2
    You need to realize that in K8S, a container is not like a container in Compose: the Pod is the abstraction K8S deals with. The Pod contains one container used to maintain the Pod's IP, and then the other containers are linked to it but have the same 'outside' IP. When you create a Pod that is meant to talk to other pods, they usually refer to each other using a Service., which would be the equivalent of the 'container name' in compose, except it's not. As suggested, best is to pass external target services as ENV params, and that'd work for any image whatever way it is linked to others.
    – MrE
    Nov 18, 2016 at 23:25
  • basically inter-container communication within one pod is like IPC. and you can use shared volume in the context of k8s, or just localhost:port since all containers in one pod shares the same network namespace. You would not want to use a DNS to lookup some process on your localhost. See this excellent article about this topic: mirantis.com/blog/…
    – shawnzhu
    Jul 24, 2018 at 14:07

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.