Kubectl仅获取工作程序节点 [英] Kubectl Get Worker Nodes Only
问题描述
是否有任何快捷方式或kubectl命令或REST API调用仅获取工作节点列表. (不包括主节点)
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.
我们也不能grep:
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
推荐答案
您可以通过
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
仅host01的标签为controlplane, worker
和etcd
.其他两个具有etcd
和worker
(也可以向右滚动以查看标签).
Only host01 has the label controlplane, worker
and etcd
. The other two have etcd
and worker
(Scroll right to see the labels as well).
所以我可以通过
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
要排除controlplane
,您可以通过!=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
请使其适应您的标签,或根据您的集群设置标签.就我而言,它是一个 Rancher 2.0 群集.添加节点后,Rancher会自动创建标签.
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.
Rancher的API位于(已附加过滤器):
The API for that is in Rancher at (with the filter already appended):
/v3/clusters/c-xxxxx/nodes?worker=true&controlPlane_ne=true
这篇关于Kubectl仅获取工作程序节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!