第九章 kubernetes 核心技术Label

一、Label 概述

Label是Kubernetes系统中另外一个核心概念。一个Label是一个key=value的键值对,其中key与vaue由用户自己指定。Label可以附加到各种资源对象上,例如Node、Pod、Service、RC等,一个资源对象可以定义任意数量的Label,同一个Label也可以被添加到任意数量的资源对象上去,Label通常在资源对象定义时确定,也可以在对象创建后动态添加或者删除。

	我们可以通过指定的资源对象捆绑一个或多个不同的Label来实现多维度的资源分组管理功能,以便于灵活、方便地进行资源分配、调度、配置、部署等管理工作。例如:部署不同版本的应用到不同的环境中;或者监控和分析应用(日志记录、监控、告警)等。一些常用等label示例如下。
•	版本标签:"release" : "stable" , "release" : "canary"
•	环境标签:"environment" : "dev" , "environment" : "production"
•	架构标签:"tier" : "frontend" , "tier" : "backend" , "tier" : "middleware"
•	分区标签:"partition" : "customerA" , "partition" : "customerB"
•	质量管控标签:"track" : "daily" , "track" : "weekly"
	
	Label相当于我们熟悉的“标签”,給某个资源对象定义一个Label,就相当于給它打了一个标签,随后可以通过Label Selector(标签选择器)查询和筛选拥有某些Label的资源对象,Kubernetes通过这种方式实现了类似SQL的简单又通用的对象查询机制。
	
[root@k8s-master-01 ~]# kubectl  get  pods --show-labels
NAME                          READY   STATUS    RESTARTS   AGE   LABELS
test-nginx-84c57cc6c8-76ttk   1/1     Running   1          18h   app=test-nginx,pod-template-hash=84c57cc6c8
test-nginx-84c57cc6c8-7wg8p   1/1     Running   1          18h   app=test-nginx,pod-template-hash=84c57cc6c8

二、根据标签来查询Pod

#查看所有pod标签
[root@k8s-master-001 ~]# kubectl get pods --show-labels
NAME                          READY   STATUS    RESTARTS   AGE   LABELS
test-nginx-84c57cc6c8-76ttk   1/1     Running   1          18h   app=test-nginx,pod-template-hash=84c57cc6c8,task=test
test-nginx-84c57cc6c8-7wg8p   1/1     Running   1          18h   app=test-nginx,pod-template-hash=84c57cc6c8
test-nginx-84c57cc6c8-bl59z   1/1     Running   1          18h   app=test-nginx,pod-template-hash=84c57cc6c8


[root@k8s-master-001 ~]# kubectl get pod --show-labels -n docs
NAME                    READY   STATUS    RESTARTS   AGE   LABELS
docs-85686dc954-6jv56   1/1     Running   1          10d   app=docs,pod-template-hash=85686dc954
docs-85686dc954-hr427   1/1     Running   0          10d   app=docs,pod-template-hash=85686dc954

[root@k8s-master-001 ~]# kubectl get pods -l task=test
NAME                          READY   STATUS    RESTARTS   AGE
test-nginx-84c57cc6c8-76ttk   1/1     Running   1          18h

三、增加标签

[root@k8s-master-001 ~]# kubectl label pod -n docs docs-85686dc954-6jv56 dev=test
pod/docs-85686dc954-6jv56 labeled

[root@k8s-master-001 ~]# kubectl get pod --show-labels -n docs
NAME                    READY   STATUS    RESTARTS   AGE   LABELS
docs-85686dc954-6jv56   1/1     Running   1          10d   app=docs,dev=test,pod-template-hash=85686dc954
docs-85686dc954-hr427   1/1     Running   0          10d   app=docs,pod-template-hash=85686dc954

四、删除标签

[root@k8s-master-001 ~]# kubectl label pod -n docs docs-85686dc954-6jv56 dev-
pod/docs-85686dc954-6jv56 labeled

[root@k8s-master-001 ~]# kubectl get pod --show-labels -n docs
NAME                    READY   STATUS    RESTARTS   AGE   LABELS
docs-85686dc954-6jv56   1/1     Running   1          10d   app=docs,pod-template-hash=85686dc954
docs-85686dc954-hr427   1/1     Running   0          10d   app=docs,pod-template-hash=85686dc954

五、将标签显示为列

[root@k8s-master-001 ~]# kubectl get pods -L app -n docs
NAME                    READY   STATUS    RESTARTS   AGE   APP
docs-85686dc954-6jv56   1/1     Running   1          10d   docs
docs-85686dc954-hr427   1/1     Running   0          10d   docs
原文地址:https://www.cnblogs.com/jhno1/p/15594405.html