How to get kubernetes Pod FQDN as container variable?

5/2/2021

I am looking for a environment variable which can give me FQDN for a pod. Generally they are of format 1-2-3-4.default.pod.cluster.local. I want to use this for inter pod communication ( essentially forwarding a request to master node)

I used the following in deployment yaml but how do i convert my IP ( 1.2.3.4) to 1-2-3-4 ? And Is there a direct field for pod FQDN which i can refer like status.podIP

- name: MY_POD_IP
  valueFrom:
    fieldRef:
      fieldPath: status.podIP  
- name: ADVERTISED_HOST_NAME
  value: "$(MY_POD_IP).default.pod.cluster.local"

It created a variable like 1.2.3.4.default.pod.cluster.local but the correct FQDN is 1-2-3-4.default.pod.cluster.local

Any suggestions ?

-- SunilS
azure-aks
kubernetes
kubernetes-helm
kubernetes-pod

2 Answers

5/3/2021

For SSL certs validation you might want to check cert manager for your use case or sign certs with wild cards. That ways you will not face this issue. For your exact issue, if you are using helm, you can use replace function in helm.

-- Abhi
Source: StackOverflow

5/4/2021

Is there a direct field for pod FQDN which i can refer like status.podIP

All pod resource fields are described in k8s api spec reference. I don't see anything that could be helpful to you. You can check it yourself.


Any suggestions ?

If what you want is a static FQDN per pod, you can use StatefulSet with so called stable network id:

Each Pod in a StatefulSet derives its hostname from the name of the StatefulSet and the ordinal of the Pod. The pattern for the constructed hostname is $(statefulset name)-$(ordinal). The example above will create three Pods named web-0,web-1,web-2. A StatefulSet can use a Headless Service to control the domain of its Pods. The domain managed by this Service takes the form: $(service name).$(namespace).svc.cluster.local, where "cluster.local" is the cluster domain. As each Pod is created, it gets a matching DNS subdomain, taking the form: $(podname).$(governing service domain), where the governing service is defined by the serviceName field on the StatefulSet.

So you can access the pod (example in docs) with it's name, e.g.: web-0.nginx.default.svc.cluster.local.

But note that Statefulset has some limitations, so I recommend to get familiar with them before you decide to use it.

There is really no other way to do it, that I know of.

Additinally, you might want to check k8s docs about k8s Cluster Networking model. Maybe you need to redefine the problem and solve it differently. It would have been much easier if you started with an actual problem you are trying to solve, and not with a solution.

-- Matt
Source: StackOverflow