<p>My Kubernetes cluster is running two node pools (called &quot;A&quot; and &quot;B&quot;) who running on my Kubernetes cluster. My deployment is <strong>preferring</strong> (using &quot;nodeAffinity-&gt;preferredDuringSchedulingIgnoredDuringExecution&quot; feature) to run on node pool &quot;B&quot;. But if not enough resources on node pool &quot;B&quot;, so Kubernetes will allocate the new pod on node pool &quot;A&quot; (<em>second choice</em>).</p>
<p>My logic is to check if pod is living on Node Pool A and no tasks to solved (tasks coming from the queue) - shutdown the pod. Hopefully, the next instance will be started on Node Pool B...</p>
<p>My application, running within the pod, is knowing when the queues are empty. In this case, I need to check if the pod is running on node pool A. If yes, shutdown.</p>
<p>How to find out the node pool of the node in which the pod is scheduled from within the pod?</p>
<p>Thanks.</p>

No1Lives4Ever
<p>You could add specific labels to all nodes in a specific pool to distinguish the nodes from other nodes in another node pool.</p>
<p>In the pod define an environment like below which reads from <code>spec.Nodename</code></p>
<pre><code>env:
  - name: MY_NODE_NAME
    valueFrom:
      fieldRef:
        fieldPath: spec.nodeName
</code></pre>
<p>Using the value from the environment variable you can query below Kubernetes API and check the label to determine the node pool.</p>
<pre><code>GET /api/v1/nodes/{MY_NODE_NAME}
</code></pre>
<p>To call above kubernetes API you can use the kubernetes <a href="https://kubernetes.io/docs/reference/using-api/client-libraries/" rel="nofollow noreferrer">client library</a> in any supported language.</p>
