K8S Label 和 Selector
1.Label
标签(labels):附加到 k8s 对象上的键值对。用于指定对用户有意义且相关的对象的标识属性。
Label是Kubernetes系列中另外一个核心概念。是一组绑定到K8s资源对象上的key/value对。同一个对象的labels属性的key必须唯一。label可以附加到各种资源对象上,如Node,Pod,Service,RC等。
通过给指定的资源对象捆绑一个或多个不用的label来实现多维度的资源分组管理功能,以便于灵活,方便地进行资源分配,调度,配置,部署等管理工作。
示例标签:
"release" : "stable", "release" : "canary"
"environment" : "dev", "environment" : "qa", "environment" : "production"
"tier" : "frontend", "tier" : "backend", "tier" : "cache"
"partition" : "customerA", "partition" : "customerB"
"track" : "daily", "track" : "weekly"
推荐使用的标签:
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: mysql
app.kubernetes.io/instance: mysql-abcxzy
app.kubernetes.io/version: "5.7.21"
app.kubernetes.io/managed-by: helm
app.kubernetes.io/component: database
app.kubernetes.io/part-of: wordpress
...
2. 语法
Label Key:
不超过63个字符
支持使用前缀,前缀必须是DNS子域,不超过253个字符。系统化组件创建的label必须指定前缀。kubernetes.io 和 k8s.io 由 kubernetes 保留
格式:A-Za-z0-9
Label Value:
不超过63个字符
格式:A-Za-z0-9
3. Label selector
Label 不是唯一的,很多对象可能有相同的label
通过 label selector,可指定一个object集合,通过 label selector 对 object 集合进行操作
两种类型:
equality-based: 使用=, ==, != 操作符,可使用逗号分隔多个表达式: environment=production,tier!=frontend
set-based:使用in, notin, !操作符。! 表示没有该 label 的 object
$ kubectl get pods -l environment=production,tier=frontend
$ kubectl get pods -l 'environment in (production),tier in (frontend)'
$ kubectl get pods -l 'environment in (production, qa)'
$ kubectl get pods -l 'environment,environment notin (frontend)'
4. API
Service
spec.selector = map[string]string
selector:
component: redis
Deployment, StatefulSet, DaemonSet, ReplicaSet, Job
selector:
matchLabels:
component: redis
matchExpressions:
- {key: tier, operator: In, values: [cache]}
- {key: environment, operator: NotIn, values: [dev]}
node affinity 和 pod affinity 中的 selector
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- k8s-node01
- k8s-node02
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: another-node-label-key
operator: In
values:
- another-node-label-value
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- kafka-container
topologyKey: kubernetes.io/hostname
weight: 100
版权声明:本文为CSDN博主「elihe2011」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/elihe2011/article/details/122168142