一、为什么需要标签?

k8s集群如果由大量节点组成,可将节点打上对应的标签,然后通过标签进行筛选及查看,更好的进行资源对象的相关选择与匹配

二、怎么查看目前node上具有的标签

[root@master01 ~]# kubectl get node --show-labels
NAME       STATUS   ROLES                  AGE     VERSION   LABELS
master01   Ready    control-plane,master   3d10h   v1.21.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=master01,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
worker01   Ready    <none>                 3d9h    v1.21.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker01,kubernetes.io/os=linux,region=huanan
worker02   Ready    <none>                 3d9h    v1.21.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker02,kubernetes.io/os=linux

三、设置节点标签信息

1、设置节点标签

[root@master01 ~]# kubectl label node worker02 region=guanzhou
node/worker02 labeled

2、查看 worker02的标签是否已经设置上

[root@master01 ~]# kubectl get nodes -L region
NAME       STATUS   ROLES                  AGE     VERSION   REGION
master01   Ready    control-plane,master   3d10h   v1.21.0   
worker01   Ready    <none>                 3d9h    v1.21.0   huanan
worker02   Ready    <none>                 3d9h    v1.21.0   guanzhou

 3、多维度标签

 3.1 设置多维度标签

也可以加其它的多维度标签,用于不同的需要区分的场景

如把worker01标签为华南区,A机房,测试环境,游戏业务

[root@master01 ~]# kubectl label node worker01 zone=A env=test bussiness=game
node/worker01 labeled

 3.2  显示节点的相应标签

[root@master01 ~]# kubectl get node -L zone,env
NAME       STATUS   ROLES                  AGE     VERSION   ZONE   ENV
master01   Ready    control-plane,master   3d10h   v1.21.0          
worker01   Ready    <none>                 3d9h    v1.21.0   A      test
worker02   Ready    <none>                 3d9h    v1.21.0  

 四、通过标签来查找node 

4.1 查找env=test的节点

[root@master01 ~]# kubectl get nodes -l env=test --show-labels
NAME       STATUS   ROLES    AGE    VERSION   LABELS
worker01   Ready    <none>   3d9h   v1.21.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,bussiness=game,env=test,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker01,kubernetes.io/os=linux,region=huanan,zone=A
 

五、修改标签

[root@master01 ~]# kubectl label node worker01 env=prod --overwrite=true
node/worker01 labeled

六、删除node的标签

[root@master01 ~]# kubectl label node worker01 env-
node/worker01 labeled
[root@master01 ~]# kubectl get node -L env
NAME       STATUS   ROLES                  AGE     VERSION   ENV
master01   Ready    control-plane,master   3d10h   v1.21.0   
worker01   Ready    <none>                 3d9h    v1.21.0   
worker02   Ready    <none>                 3d9h    v1.21.0   

七、标签选择器

[root@master01 ~]# kubectl label node worker01 env=test1
node/worker01 labeled
[root@master01 ~]# kubectl label node worker02 env=test2
node/worker02 labeled
[root@master01 ~]# kubectl get node -l 'env in(test1,test2)'
NAME       STATUS   ROLES    AGE    VERSION
worker01   Ready    <none>   3d9h   v1.21.0
worker02   Ready    <none>   3d9h   v1.21.0
[root@master01 ~]# kubectl get node -l 'env in(test1,test2)' --show-labels
NAME       STATUS   ROLES    AGE    VERSION   LABELS
worker01   Ready    <none>   3d9h   v1.21.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,bussiness=game,env=test1,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker01,kubernetes.io/os=linux,region=huanan,zone=A
worker02   Ready    <none>   3d9h   v1.21.0   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=test2,kubernetes.io/arch=amd64,kubernetes.io/hostname=worker02,kubernetes.io/os=linux,region=guanzhou
 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐