k8s怎么给node添加标签

作者:有用网 阅读量:180 发布时间:2024-01-09
关键字 k8s

这篇文章主要介绍“k8s怎么给node添加标签”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“k8s怎么给node添加标签”文章能帮助大家解决问题。

一、为什么需要标签?

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


#发表评论
提交评论