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
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 controlplane
s, 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