kubernetes labels 标签简介

标签中的键名称通常由"键前缀"和键名"“组成,其格式形如"KEY PREFIX/KEY_NAME",键前缀为可选部分。键名至多能使用63个字符,支持字母、数字、连接号(-)、下划线(_)、点号(.)等字符,且只能以字母或数字开头。而键前缀必须为DNS子域名格式。且不能超过253个字符。省路键前缀时,键将被视为用户的私有数据。那些由Kubernetes系统组件或第三方组件自动为用户资源添加的键必须使用键前缀,kubernetes.io/和k8s.io/前缀预留给了kubernetes的核心组件使用,
例如:Node对象上常用的kubernetes.io/os、 kutbernetes.io/arch和kubernetes.io/hostname等。

标签的键值必须不能多于63个字符,它要么为空,要么是以字母或数字开头及结尾,且中间仅使用了字母、数字、连接号(-)、下划线(_)或点号(.)等字符的数据。

标签选择器用于表达标签的查询条件或选择标准,Kubernetes API目前支持两个选择器;基于等值关系(equality-based》的标签选项器以及基于集合关系(set-bsel)的标签选择器。同时指定多个选择器时需要以逗号将其分隔,各选择器之间遵循“与"逻辑,即必须要满足所有条件,而且空值的选择器将不选择任何对象。

基于等值关系的标签选择器的可用操作符有=、==和!= 三种,其中前两个意义相同,都表示"等值"关系,最后一个表示"不等”。例如env=dev和env!=prod都是基于等值关系的选择器,而tier in (frontend,backend)则是基于集合关系的选择器。

创建标签时:release=alpha,
标签选择器使用:release==alpha

常用标签:

  • 版本标签: "release" : "stable","release" : "canary", "release" : "beta".
  • 环境标签: "environment" : "dev", "environment" : "qa","environment" : "prod"
  • 应用标签: "app" : "ui","app" : "as","app" : "pc","app" : "sc"
  • 架构层级标签: "tier" : "frontend", "tier" : "backend" , "tier" : "cache"
  • 分区标签: "partition" : "customerA","partition" : "customerB"。
  • 品控级别标签:"track" : "daily", "track" : "weekly"。

匹配条件

  • KEY in (VALUE1,VALUE2,.….)∶指定的键名的值存在于给定的列表中即满足条件;
  • KEY notin (VALUE1,VALUE2,.)︰指定的键名的值不存在于给定列表中即满足条件;
  • KEY:所有存在此键名标签的资源;
  • !KEY:所有不存在此键名标签的资源。

示例1:手动添加标签

[root@k8s-master svc]# kubectl label  --help
[root@k8s-master svc]# kubectl get pod --show-labels   #查看标签
NAME                          READY   STATUS    RESTARTS   AGE   LABELS
stress-pod                    1/1     Running   1          25h   <none>

[root@k8s-master svc]# kubectl label pods stress-pod release=alpha   #添加标签
pod/stress-pod labeled

[root@k8s-master svc]# kubectl get pod --show-labels
NAME                          READY   STATUS    RESTARTS   AGE   LABELS
stress-pod                    1/1     Running   1          25h   release=alpha
[root@k8s-master svc]# kubectl label pods stress-pod release-   #删除标签
pod/stress-pod labeled
[root@k8s-master svc]# kubectl get pod --show-labels
NAME                          READY   STATUS    RESTARTS   AGE   LABELS
stress-pod                    1/1     Running   1          25h   <none>
  • 标签匹配选择 过滤
[root@k8s-master svc]# kubectl get pod --show-labels
NAME                          READY   STATUS              RESTARTS   AGE   LABELS
demoapp-66db74fcfc-9wkgj      1/1     Running             0          11m   app=demoapp,pod-template-hash=66db74fcfc,release=stable
demoapp-66db74fcfc-vzb4f      1/1     Running             0          11m   app=demoapp,pod-template-hash=66db74fcfc,release=stable
liveness-httpget-demo         0/1     ContainerCreating   0          25s   <none>
liveness-tcpsocket-demo       0/1     ContainerCreating   0          20s   <none>
my-grafana-7d788c5479-kpq9q   1/1     Running             4          27d   app.kubernetes.io/instance=my-grafana,app.kubernetes.io/name=grafana,pod-template-hash=7d788c5479


[root@k8s-master svc]# kubectl get pods -l app=demoapp  --show-labels    #过滤标签app=demoapp的pod
NAME                       READY   STATUS    RESTARTS   AGE   LABELS
demoapp-66db74fcfc-9wkgj   1/1     Running   0          12m   app=demoapp,pod-template-hash=66db74fcfc,release=stable
demoapp-66db74fcfc-vzb4f   1/1     Running   0          12m   app=demoapp,pod-template-hash=66db74fcfc,release=stable

[root@k8s-master svc]# kubectl get pods -l app!=demoapp  --show-labels    #所有不匹配app=demoapp 的pod 包含没有app标签的pod  
NAME                          READY   STATUS    RESTARTS   AGE     LABELS
liveness-httpget-demo         1/1     Running   2          3m28s   <none>
liveness-tcpsocket-demo       1/1     Running   2          3m23s   <none>
my-grafana-7d788c5479-kpq9q   1/1     Running   4          27d     app.kubernetes.io/instance=my-grafana,app.kubernetes.io/name=grafana,pod-template-hash=7d788c5479
stress-pod                    1/1     Running   1          27h     <none>

[root@k8s-master svc]# kubectl get pods -l app  --show-labels   #过滤所有含app标签的容器
NAME                       READY   STATUS    RESTARTS   AGE   LABELS 
demoapp-66db74fcfc-9wkgj   1/1     Running   0          16m   app=demoapp,pod-template-hash=66db74fcfc,release=stable
demoapp-66db74fcfc-vzb4f   1/1     Running   0          16m   app=demoapp,pod-template-hash=66db74fcfc,release=stable

[root@k8s-master svc]# kubectl get pods -l '!app'  --show-labels  #过滤所有不含app标签的容器
NAME                          READY   STATUS    RESTARTS   AGE     LABELS
liveness-httpget-demo         1/1     Running   3          6m33s   <none>
liveness-tcpsocket-demo       1/1     Running   3          6m28s   <none>
my-grafana-7d788c5479-kpq9q   1/1     Running   4          27d     app.kubernetes.io/instance=my-grafana,app.kubernetes.io/name=grafana,pod-template-hash=7d788c5479

[root@k8s-master svc]# kubectl label pods liveness-httpget-demo app=liveness    #添加app标签
pod/liveness-httpget-demo labeled

[root@k8s-master svc]# kubectl get pods -l 'app in (demoapp,liveness)'  --show-labels   #过滤app= demoapp或liveness的pod
NAME                       READY   STATUS    RESTARTS   AGE     LABELS
demoapp-66db74fcfc-9wkgj   1/1     Running   0          20m     app=demoapp,pod-template-hash=66db74fcfc,release=stable
demoapp-66db74fcfc-vzb4f   1/1     Running   0          20m     app=demoapp,pod-template-hash=66db74fcfc,release=stable
liveness-httpget-demo      1/1     Running   3          9m45s   app=liveness

[root@k8s-master svc]# kubectl label pods demoapp-66db74fcfc-vzb4f track=daily  #添加标签
pod/demoapp-66db74fcfc-vzb4f labeled

[root@k8s-master svc]# kubectl get pods -l 'app=demoapp,track=daily' --show-labels   #过滤app=demoapp,track=daily同时满足条件的pod
NAME                       READY   STATUS    RESTARTS   AGE   LABELS
demoapp-66db74fcfc-vzb4f   1/1     Running   0          26m   app=demoapp,pod-template-hash=66db74fcfc,release=stable,track=daily

[root@k8s-master svc]# kubectl get deployment  -l app=demoapp --show-labels   #标签过滤同样对其实资源有效
NAME      READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
demoapp   2/2     2            2           28m   app=demoapp,release=stable

Bigyong
28 声望13 粉丝