Search code examples
kubernetesgoogle-kubernetes-enginekubernetes-podkubernetes-statefulsetgke-networking

Comunication multiple containers in multiple pods in kubernetes


I was left to a configuration of Kubernetes which contains two distinct instances of a master-replica service. The pods of both the master and the replicas contain both the container of the service and a metric collection container (therefore for each pod there are two different containers). In addition, I have a service headless that aims at all three that will then be used by other components. The point is that the "replicas" need to access the Master, and the classic DNS nomenclature "Master-Pod-Name.Service-Name.Namespace.Svc.Cluster.local" is not working (it was already set in this way in variables) I did some lookup tests etc and obviously it only works if I use "service-name.Namespace.svc.cluster.local", however, that service aims at all 3 instances and I have to point only to the master. My doubt is that I cannot solve it because there are several containers in that pod but I do not find references to how to indicate the correct DNS in the documentation.

I also tried to configure

dnsConfig:
        options:
        - name: ndots
          value: "6"

Then putting as DNS Container-Name.pod-Name.Service-Name.Namespace.Svc.Cluster.local but nothing has changed anyway.

The environment I am working on is a gke cluster (Kubernetes on Google Cloud).


Solution

  • Make sure that your application is deployed as a stateful set together with a headless service. Only stateful sets guarantee the stable network IDs that you want to use in your DNS lookups:

    https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id