Kubernetes: get the node pool name from inside the pod

7/12/2020

My Kubernetes cluster is running two node pools (called "A" and "B") who running on my Kubernetes cluster. My deployment is preferring (using "nodeAffinity->preferredDuringSchedulingIgnoredDuringExecution" feature) to run on node pool "B". But if not enough resources on node pool "B", so Kubernetes will allocate the new pod on node pool "A" (second choice).

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...

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.

How to find out the node pool of the node in which the pod is scheduled from within the pod?

Thanks.

-- No1Lives4Ever
kubernetes

1 Answer

7/12/2020

You could add specific labels to all nodes in a specific pool to distinguish the nodes from other nodes in another node pool.

In the pod define an environment like below which reads from spec.Nodename

env:
  - name: MY_NODE_NAME
    valueFrom:
      fieldRef:
        fieldPath: spec.nodeName

Using the value from the environment variable you can query below Kubernetes API and check the label to determine the node pool.

GET /api/v1/nodes/{MY_NODE_NAME}

To call above kubernetes API you can use the kubernetes client library in any supported language.

-- Arghya Sadhu
Source: StackOverflow