当前位置:网站首页>kubernetes集群之Label管理

kubernetes集群之Label管理

2022-07-04 06:56:00 江湖有缘

一、Label介绍

1.标签是k8s特色的管理方式,便于分类管理资源对象。
2.一个标签可以对应多个资源,一个资源也可以有多个标签,它们是多对多的关系。
3.一种特殊的资源对象,必须附件在具体的资源对象之上,用于标识资源对象的用途。
4. 一个资源拥有多个标签,可以实现不同维度的管理。

二、检查本地k8s集群状态

[[email protected]-master ~]# kubectl get nodes -owide
NAME         STATUS   ROLES                  AGE   VERSION   INTERNAL-IP     EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
k8s-master   Ready    control-plane,master   18h   v1.23.1   192.168.3.201   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node01   Ready    <none>                 18h   v1.23.1   192.168.3.202   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6
k8s-node02   Ready    <none>                 18h   v1.23.1   192.168.3.203   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   containerd://1.6.6

三、Label的创建

1.给工作节点新增标签

[[email protected]-master ~]# kubectl label nodes k8s-node02 fun=work02
node/k8s-node02 labeled

2.给pod打上标签

[[email protected]-master ~]# kubectl label pod cirror-28253 fun=ittest
pod/cirror-28253 labeled
[[email protected]-master ~]# kubectl label pod cirror-28253 env=proc
pod/cirror-28253 labeled

3.给工作节点打上角色标签

[[email protected]-master ~]# kubectl label nodes k8s-node01  node-role.kubernetes.io/worker=
node/k8s-node01 labeled

四、Label的修改

修改标签

[[email protected]-master ~]# kubectl label nodes k8s-node02 fun=node02  --overwrite 
node/k8s-node02 labeled

五、Label的查询

1.列出节点的所有标签

[[email protected]-master ~]# kubectl get nodes --show-labels 
NAME         STATUS   ROLES                  AGE   VERSION   LABELS
k8s-master   Ready    control-plane,master   18h   v1.23.1   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-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
k8s-node01   Ready    worker                 18h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node01,kubernetes.io/os=linux,node-role.kubernetes.io/worker=
k8s-node02   Ready    <none>                 18h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,fun=node02,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node02,kubernetes.io/os=linux,type=dell730

2.查询带有worker角色的节点

[[email protected]-master ~]# kubectl get nodes -l node-role.kubernetes.io/worker
NAME         STATUS   ROLES    AGE   VERSION
k8s-node01   Ready    worker   18h   v1.23.1

3.查询带有type标签的节点

[[email protected]-master ~]# kubectl get nodes -l type
NAME         STATUS   ROLES    AGE   VERSION
k8s-node02   Ready    <none>   18h   v1.23.1

六、Label的删除

[[email protected]-master ~]# kubectl label nodes k8s-node02 fun-
node/k8s-node02 unlabeled
[[email protected]-master ~]# kubectl get node -l fun
No resources found

七、kubernetes注解使用

1.给工作节点添加注解

[[email protected]-master ~]# kubectl annotate nodes k8s-node01 test="this is a work node"
node/k8s-node01 annotated

2.查看注解

[[email protected]-master ~]# kubectl describe nodes k8s-node01 |grep test
                    test: this is a work node

3.修改注解

 kubectl annotate nodes k8s-node01 test=11 --overwrite

4.删除注解

kubectl annotate nodes k8s-node01 test-

八、在yaml脚本的标签使用

[[email protected]-master ~]#  kubectl run web01 --image=nginx:1.21 -n k8s-test  -o yaml --dry-run=client
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: web01
  name: web01
  namespace: k8s-test
spec:
  containers:
  - image: nginx:1.21
    name: web01
    resources: {
    }
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {
    }

原网站

版权声明
本文为[江湖有缘]所创,转载请带上原文链接,感谢
https://blog.csdn.net/jks212454/article/details/125588038