亲宝软件园·资讯

展开

如何给k8s集群里的资源打标签

IT运维成长笔记 人气:0

如何给k8s集群里的资源打标签

给节点添加角色:

k8s集群,节点如果有多个角色,需要标记出来,可以给对应的节点打上标签,方便后续了解节点的功能

命令:kubectl label nodes 节点名字 node-role.kubernetes.io/你想要的roles(=/-)

最后括号里的加减号,减号就是删除roles,等号就是增加roles

更新标签,在打标签命令后面添加参数--overwrite

举例:

给node节点添加角色:

#给node1节点增加jenkins角色
[root@k8s-master1 ~]# kubectl label nodes k8s-node1 node-role.kubernetes.io/jenkins=
node/k8s-node1 labeled

#给node2节点增加gitlab角色
[root@k8s-master1 ~]# kubectl label nodes k8s-node2 node-role.kubernetes.io/gitlab=
node/k8s-node2 labeled

#查询节点roles
[root@k8s-master1 ~]# kubectl get nodes
NAME          STATUS   ROLES          AGE   VERSION
k8s-master1   Ready    master         17d   v1.19.4
k8s-node1     Ready    jenkins,node   17d   v1.19.4
k8s-node2     Ready    gitlab,node    14d   v1.19.4

给deploymen控制器打标签:

更新标签,在打标签命令后面添加参数--overwrite

#查询标签
[root@k8s-master1 k8s]# kubectl get deploy -n my-ns-kcxm kcxm --show-labels 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
kcxm   2/2     2            2           8m47s   app=kcxm-gc

#打标签version=v1
[root@k8s-master1 k8s]# kubectl label deploy -n my-ns-kcxm kcxm version=v1
deployment.apps/kcxm labeled

#查询标签
[root@k8s-master1 k8s]# kubectl get deploy -n my-ns-kcxm kcxm --show-labels 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
kcxm   2/2     2            2           9m19s   app=kcxm-gc,version=v1

#更新标签,在打标签命令后面添加参数--overwrite 
[root@k8s-master1 k8s]# kubectl label deploy -n my-ns-kcxm kcxm version=v2 --overwrite 
deployment.apps/kcxm labeled

#查询标签
[root@k8s-master1 k8s]# kubectl get deploy -n my-ns-kcxm kcxm --show-labels 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
kcxm   2/2     2            2           13m   app=kcxm-gc,version=v2

#删除标签version=v2
[root@k8s-master1 k8s]# kubectl label deploy -n my-ns-kcxm kcxm version-
deployment.apps/kcxm labeled

#查询标签
[root@k8s-master1 k8s]# kubectl get deploy -n my-ns-kcxm kcxm --show-labels 
NAME   READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
kcxm   2/2     2            2           9m30s   app=kcxm-gc

给pod打标签:

[root@k8s-master1 k8s]# kubectl get pod -n my-ns-kcxm --show-labels 
NAME                    READY   STATUS    RESTARTS   AGE     LABELS
kcxm-84c56f9d5b-595qx   1/1     Running   0          2m42s   app=kcxm-gc,pod-template-hash=84c56f9d5b
kcxm-84c56f9d5b-6h4w2   1/1     Running   0          2m42s   app=kcxm-gc,pod-template-hash=84c56f9d5b
 
#给pod打标签version=1.23.1
[root@k8s-master1 k8s]# kubectl label pod -n my-ns-kcxm kcxm-84c56f9d5b-595qx version=1.23.1
pod/kcxm-84c56f9d5b-595qx labeled

#查询标签
[root@k8s-master1 k8s]# kubectl get pod -n my-ns-kcxm --show-labels 
NAME                    READY   STATUS    RESTARTS   AGE     LABELS
kcxm-84c56f9d5b-595qx   1/1     Running   0          3m30s   app=kcxm-gc,pod-template-hash=84c56f9d5b,version=1.23.1
kcxm-84c56f9d5b-6h4w2   1/1     Running   0          3m30s   app=kcxm-gc,pod-template-hash=84c56f9d5b

#给pod删除标签version=1.23.1
[root@k8s-master1 k8s]# kubectl label pod -n my-ns-kcxm kcxm-84c56f9d5b-595qx version-
pod/kcxm-84c56f9d5b-595qx labeled

#删除标签
[root@k8s-master1 k8s]# kubectl get pod -n my-ns-kcxm --show-labels 
NAME                    READY   STATUS    RESTARTS   AGE    LABELS
kcxm-84c56f9d5b-595qx   1/1     Running   0          4m7s   app=kcxm-gc,pod-template-hash=84c56f9d5b
kcxm-84c56f9d5b-6h4w2   1/1     Running   0          4m7s   app=kcxm-gc,pod-template-hash=84c56f9d5b

补充:k8s kubernetes给node节点添加标签和删除node节点标签

[root@k8s-master ~]# hostname #查看节点名称
k8s-master
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get nodes  --show-labels  #查看节点标签
NAME         STATUS   ROLES           AGE   VERSION   LABELS
k8s-master   Ready    control-plane   9d    v1.26.0   app=master,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
[root@k8s-master ~]# kubectl label nodes k8s-master env=env #给节点添加一个标签env=env
node/k8s-master labeled
[root@k8s-master ~]# kubectl get nodes  --show-labels  #再次查看节点标签,确定标签添加上了
NAME         STATUS   ROLES           AGE   VERSION   LABELS
k8s-master   Ready    control-plane   9d    v1.26.0   app=master,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,env=env,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
[root@k8s-master ~]# kubectl label nodes k8s-master env- #删除节点标签env=env
node/k8s-master unlabeled
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get nodes --show-labels  #再次查看确定标签已经删除了
NAME         STATUS   ROLES           AGE   VERSION   LABELS
k8s-master   Ready    control-plane   9d    v1.26.0   app=master,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node.kubernetes.io/exclude-from-external-load-balancers=
[root@k8s-master ~]# 

加载全部内容

相关教程
猜你喜欢
用户评论