Kubectl Get Worker Nodes Only

随声附和 提交于 2019-12-11 04:52:07

问题


Is there any shortcut or kubectl command or REST API call to get a list of worker nodes only. ( not including the master nodes )

Update: For the masters we can do like this:

kubectl get nodes --selector=node-role.kubernetes.io/master

for the workers I dont see any such label created by default. Can we do get by reversing or do != kind of thing on selector.

We can't grep it either:

C02W84XMHTD5:ucp iahmad$ kubectl get nodes | grep worker
C02W84XMHTD5:ucp iahmad$ 
C02W84XMHTD5:ucp iahmad$ kubectl get nodes -o wide| grep worker
C02W84XMHTD5:ucp iahmad$ 
C02W84XMHTD5:ucp iahmad$ kubectl get nodes -o yaml | grep worker
C02W84XMHTD5:ucp iahmad$ 
C02W84XMHTD5:ucp iahmad$ kubectl get nodes -o json | grep worker
C02W84XMHTD5:ucp iahmad$ 

My use case is that want to get this list every minute to update the external load balancer pools, in case new nodes are added, removed from the cluster. Indeed I can label them myself but if there is some default built in way of doing this would be useful


回答1:


You can get roles/labels of your nodes by

kubectl get nodes --show-labels

in my case, I do have three nodes each having the given roles and labels:

NAME        STATUS    ROLES                      AGE       VERSION   LABELS
host01      Ready     controlplane,etcd,worker   61d       v1.10.5   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=host01,node-role.kubernetes.io/controlplane=true,node-role.kubernetes.io/etcd=true,node-role.kubernetes.io/worker=true
host02      Ready     etcd,worker                61d       v1.10.5   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=host02,node-role.kubernetes.io/etcd=true,node-role.kubernetes.io/worker=true
host03      Ready     etcd,worker                61d       v1.10.5   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=host03,node-role.kubernetes.io/etcd=true,node-role.kubernetes.io/worker=true

Only host01 has the label controlplane, worker and etcd. The other two have etcd and worker (Scroll right to see the labels as well).

So I can get all worker nodes by

kubectl get nodes -l node-role.kubernetes.io/worker=true

NAME        STATUS    ROLES                      AGE       VERSION
host01      Ready     controlplane,etcd,worker   61d       v1.10.5
host02      Ready     etcd,worker                61d       v1.10.5
host03      Ready     etcd,worker                61d       v1.10.5

To exclude controlplanes, you can exclude them with a second label by !=true

kubectl get nodes -l node-role.kubernetes.io/worker=true,node-role.kubernetes.io/controlplane!=true

NAME        STATUS    ROLES         AGE       VERSION
host02      Ready     etcd,worker   61d       v1.10.5
host03      Ready     etcd,worker   61d       v1.10.5

Please adapt that to your labels or set labels accordingly to your cluster. In my case it is a Rancher 2.0 cluster. The labels are automatically created by Rancher when added a node.

The API for that is in Rancher at (with the filter already appended):

/v3/clusters/c-xxxxx/nodes?worker=true&controlPlane_ne=true


来源:https://stackoverflow.com/questions/52433599/kubectl-get-worker-nodes-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!