Label标签管理k8s资源
为什么需要Label
Section titled “为什么需要Label”当资源变得非常多的时候,如何分类管理就非常重要了,Kubernetes提供了一种机制来为资源分类,那就是Label(标签)。Label非常简单,但是却很强大,Kubernetes中几乎所有资源都可以用Label来组织。Label的具体形式是key-value的标记对,可以在创建资源的时候设置,也可以在后期添加和修改。
以Pod为例,当Pod变得多起来后,就显得杂乱且难以管理,如下图所示。

label 的增加和删除
Section titled “label 的增加和删除”分别创建一个pod/deployment,查看其对应的labels值,并通过label值过滤对应的pod
[root@testcce-26771 ~]# kubectl run nginx --image=nginx:alpinepod/nginx created[root@testcce-26771 ~]# kubectl create deployment web --image=nginx:alpinedeployment.apps/web created[root@testcce-26771 ~]# kubectl get pods --show-labelsNAME READY STATUS RESTARTS AGE LABELSnginx 1/1 Running 0 16s node-local-dns-injection=injected,run=nginxweb-5bf684665b-dzz9l 1/1 Running 0 8s app=web,node-local-dns-injection=injected,pod-template-hash=5bf684665b[root@testcce-26771 ~]# kubectl get pods -l appNAME READY STATUS RESTARTS AGEweb-5bf684665b-dzz9l 1/1 Running 0 47s添加Label
Section titled “添加Label”Label的形式为key-value形式,使用非常简单,如下,为Pod设置了app=nginx和env=prod两个Label。
apiVersion: v1kind: Podmetadata: name: nginx labels: # 为Pod设置两个Label app: nginx env: prodspec: containers: - image: nginx:alpine name: container-0 resources: limits: cpu: 100ms memory: 200Mi requests: cpu: 100m memory: 200Mi imagePullSecrets: - name: default-secretPod有了Label后,在查询Pod的时候带上—show-labels就可以看到Pod的Label。
$ kubectl get pod --show-labelsNAME READY STATUS RESTARTS AGE LABELSnginx 1/1 Running 0 50s app=nginx,env=prod还可以使用-L只查询某些固定的Label。
$ kubectl get pod -L app,envNAME READY STATUS RESTARTS AGE APP ENVnginx 1/1 Running 0 1m nginx prod对已存在的Pod,可以直接使用kubectl label命令直接添加Label。
$ kubectl label pod nginx creation_method=manualpod/nginx labeled
$ kubectl get pod --show-labelsNAME READY STATUS RESTARTS AGE LABELSnginx 1/1 Running 0 50s app=nginx, creation_method=manual,env=prod
# 列出所有不包含 env 标签的pod,无论其值如何:$ kubectl get pod -L '!env'
# 列出所有不包含 env!=prod 标签键值的pod:$ kubectl get pod -l 'env!=prod'修改Label
Section titled “修改Label”对于已存在的Label,如果要修改的话,需要在命令中带上—overwrite,如下所示。
$ kubectl label pod nginx env=debug --overwritepod/nginx labeled
$ kubectl get pod --show-labelsNAME READY STATUS RESTARTS AGE LABELSnginx 1/1 Running 0 50s app=nginx,creation_method=manual,env=debug删除Label
Section titled “删除Label”对于已存在的Label,如果要删除的话,只需要在对应的lable key值后面加上减号就可以了
$ kubectl label pod nginx env-pod/nginx labeled
$ kubectl get pod --show-labelsNAME READY STATUS RESTARTS AGE LABELSnginx 1/1 Running 0 50s app=nginx,creation_method=manual通过标签管理调度
Section titled “通过标签管理调度”设置标签的一个是作用是方便管理查询,另一个作用是可以更好的进行管理调度。
# 给 node 节点设置label标签[root@testcce-26771 ~]# kubectl get nodesNAME STATUS ROLES AGE VERSION192.168.0.26 Ready <none> 4h v1.25.3-r0-25.1.23192.168.0.35 Ready <none> 19h v1.25.3-r0-25.1.23[root@testcce-26771 ~]# kubectl label nodes 192.168.0.26 disk-node/192.168.0.26 unlabeled[root@testcce-26771 ~]# kubectl get nodes -l diskNo resources found[root@testcce-26771 ~]# kubectl label nodes 192.168.0.26 disk=ssdnode/192.168.0.26 labeled[root@testcce-26771 ~]# kubectl get nodes -l diskNAME STATUS ROLES AGE VERSION192.168.0.26 Ready <none> 4h1m v1.25.3-r0-25.1.23
# 通过给deployement更新nodeSelector选择标签选择对应的node节点[root@testcce-26771 ~]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx 1/1 Running 0 18m 172.16.0.9 192.168.0.35 <none> <none>web-7564c4fd95-grvxp 1/1 Running 0 6s 172.16.0.14 192.168.0.35 <none> <none>[root@testcce-26771 ~]# kubectl patch deployments web --type merge -p '{"spec":{"template":{"spec":{"nodeSelector":{"disk":"ssd"}}}}}'deployment.apps/web patched[root@testcce-26771 ~]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx 1/1 Running 0 19m 172.16.0.9 192.168.0.35 <none> <none>web-7fcb4cc99c-6fgfb 1/1 Running 0 5s 172.16.0.136 192.168.0.26 <none> <none>上面kubectl patch命令执行的内容和更新以下yaml文件的效果相同:
apiVersion: apps/v1kind: Deploymentmetadata: name: web namespace: default labels: app: webspec: selector: matchLabels: app: web replicas: 1 template: metadata: labels: app: web spec: containers: - name: web image: "swr.la-north-2.myhuaweicloud.com/hcie/nginx:v1" ports: - containerPort: 80 name: web imagePullSecrets: - name: default-secret nodeSelector: disk: ssd