0

I have a pod running in a statefulset but it needs to know the hostname or address of all pods running in another statefulset to communicate with them. The second statefulset is being created by a separate helm chart. Can the pod work this out dynamically? Can I inject this information into the pod through an env similar to setting .Status.ip?

Edit: Each statefulSet has its own headless service

atlantis.pd
  • 68
  • 1
  • 9
  • Not sure if I understood your requirment correctly, but: Did you try using a service for the other statefulset? (https://kubernetes.io/docs/concepts/services-networking/service/) This will give you a static domain within the cluster to reach all those pods. – David Losert Aug 14 '20 at 15:09
  • If it is a helm chart, it will most likely already create a service for that statefulset - but without knowing what helm chart that is only a guess. You can check with `kubectl get service` – David Losert Aug 14 '20 at 15:09
  • Yes both statefulsets have a headless service attached to them – atlantis.pd Aug 14 '20 at 15:28
  • Is there a way to inject the full static domain into another pod through an env? – atlantis.pd Aug 14 '20 at 15:29
  • Yes sure - just use the `env` config within your pod-spec (https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/). The DNS of the service is basically it's name if it is in the same namespace, or something like `..svc.cluster.local` if it's in another namespace. – David Losert Aug 14 '20 at 16:37
  • @DavidLosert Will the end of the DNS always be ```svc.cluster.local```? I want to make it able to work dynamically if its running on different K8S cloud services in which case I imagine they have their own service network? Sorry if these seem like basics questions - new to this – atlantis.pd Aug 15 '20 at 11:49
  • Don't be sorry, it's perfectly fine to ask those questions :) And well, no it does not always have to be like this, at least not the `cluster.local`. This is the so called cluster domain, and this can be configured. However, if you don't explicitly do this, it will be `cluster.local` (which is the default). But for those cases where you have to deploy to different environments where you have to deal with slight config differences, I'd suggest to dynamically change them with something like Kustomize (https://kustomize.io/) which is exactly for this. – David Losert Aug 15 '20 at 15:42
  • @DavidLosert can you paste your comments as an answer ? – Malgorzata Aug 17 '20 at 09:20
  • Sure thing - just added it. – David Losert Aug 17 '20 at 09:36

1 Answers1

1

As discussed in the comments, the way to go here is to use a service-resource as this will give you a static DNS within the cluster to reach all the pods that a targeted by that service.

The DNS for the service is:

  • the services name if you access it from within the same namespace
  • <my-service-name>.<namespace-name>.svc.cluster.local if you access it from another namespace, and where cluster.local is the clusters domain that might differ from cluster to cluster depending on the clusters configuration

If you further need more configuration options, e.g. when you want to deploy your chart into different cloud environments where the clusters-domain might actually differ, you can use kustomize.io to adjust your configuration at apply time.

David Losert
  • 4,652
  • 1
  • 25
  • 31