Search code examples
kubernetesdaemonset

From a container that is part of a DaemonSet, how can I look up the labels on "the node on which I am running"?


I have a service running as a DaemonSet across a number of kubernetes nodes. I would like to make some policy decisions based on the labels of the node on which each DaemonSet pod is running. From within a container, how do I know on which node this pod is running? Given this information, looking up the node labels through the API should be relatively easy.

What I'm doing right now is passing in the node name as an environment variable, like this:

          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName

Is that the correct way of getting at this information? More specifically, is there any sort of API call that will answer the question, "where am I?"?


Solution

  • Answering your question "How do I know what node I'm on?":

    Your approach seems to be the best one - to use the DownWard API which allows us to access some pod's or container's fields. In our case it is pod's field spec.nodeName which is accessible.

    You can use two options to expose pod information using this API:

    You can also access Kubernetes API from a pod and get this information from here but it is kind of workaround. It's better to take advantage of the fact that the name of the node is one of the pod's field and use previously described the DownWard API which is made for this purpose and officialy described in Kubernetes documentation.