Ravichandra
Ravichandra

Reputation: 2332

how to inherit Node-Labels to Pod-Labels in kubernetes?

Can I inherit node labels to pod labels?

i.e I want to have zone and instance-type labels from node to pods

Upvotes: 4

Views: 2641

Answers (2)

I do this on the host

kubectl -n <namespace>  get pods -o wide | sed 1d  | awk '{gsub("[(][^)]*[)]","")}1' | tr -s ' ' |  cut -d' ' -f1,7  | while read -r pod  node ; do
echo "Pod: $pod, node: $node"
kubectl -n <namespace>  label po $pod nodename=$node --overwrite
done

But i can also set conditions in the loop:

kubectl -n <namespace> get pods -o wide | sed 1d  | awk '{gsub("[(][^)]*[)]","")}1' | tr -s ' ' |  cut -d' ' -f1,7  | while read -r pod  node ; do
echo "Pod: $pod, node: $node"
kubectl -n <namespace> label po $pod nodename=$node --overwrite

case $pod in
    *node1* )
         kubectl -n <namespace> label po ${pod} podloc=node1-${node} --overwrite
         ;;
esac

case $pod in
    *node2* )
         kubectl -n <namespace> label po ${pod} podloc=node2-${node}  --overwrite
         ;;
esac


done

Upvotes: 0

mchawre
mchawre

Reputation: 12228

This feature is not yet supported.

Here is an open feature request on kubernetes.

Though there are few workarounds.

You can also refers this, where they make use of initContainer to get node label and assign it to pod label.

Hope this helps.

Upvotes: 8

Related Questions