<p>I have a service running as a <code>DaemonSet</code> across a number of kubernetes nodes. I would like to make some policy decisions based on the labels of the node on which each <code>DaemonSet</code> 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.</p>
<p>What I'm doing right now is passing in the node name as an environment variable, like this:</p>
<pre><code>          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
</code></pre>
<p>Is that the correct way of getting at this information? More specifically, is there any sort of API call that will answer the question, &quot;where am I?&quot;?</p>

larsks
<p>Answering your question <em>&quot;How do I know what node I'm on?&quot;</em>:</p>
<p>Your approach seems to be the best one - to use <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#the-downward-api" rel="nofollow noreferrer">the DownWard API</a> which allows us to access some pod's or container's fields. In our case it is pod's field <code>spec.nodeName</code> which is accessible.</p>
<p>You can use two options to expose pod information using this API:</p>
<ul>
<li><a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">environment variable(s)</a></li>
<li><a href="https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/" rel="nofollow noreferrer">through file(s)</a></li>
</ul>
<p>You can also access <a href="https://kubernetes.io/docs/tasks/run-application/access-api-from-pod/" rel="nofollow noreferrer">Kubernetes API from a pod</a> 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.</p>
