K8S Label 和 Selector

本文详细解读了Kubernetes中的标签(label)及其语法,包括推荐使用标签示例,以及LabelSelector的equality-based和set-based操作。还介绍了API中Service、Deployment等对象如何应用标签选择器,以及nodeAffinity和podAffinity中的选择器用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Label

标签(labels):附加到 k8s 对象上的键值对。用于指定对用户有意义且相关的对象的标识属性。

示例标签:

  • "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.iok8s.io 由 kubernetes 保留
  • 格式:[A-Za-z0-9][A-Za-z0-9_\-\.]

Label Value:

  • 不超过63个字符
  • 格式:[A-Za-z0-9][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

    sepc.selector.matchLabels = map[string]string

    sepc.selector.matchExpressions:

    • key: string
    • operator: In, NotIn, Exists or DoesNotExist
    • values: []string
    selector:
      matchLabels:
        component: redis
      matchExpressions:
        - {key: tier, operator: In, values: [cache]}
        - {key: environment, operator: NotIn, values: [dev]}
    
  • node affinity 和 pod affinity 中的 selector

    spec.template.spec.affinity.nodeAffinity:

    • requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms
    • preferredDuringSchedulingIgnoredDuringExecution.preference
      • matchExpressions: by node labels
        • key: string
        • operator: In, NotIn, Exists, DoesNotExist, Gt or Lt
        • values: []string
      • matchFields: by node fields
        • key: string
        • operator: In, NotIn, Exists, DoesNotExist, Gt or Lt
        • values: []string

    spec.template.spec.affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution

    spec.template.spec.affinity.podAffinity.preferredDuringSchedulingIgnoredDuringExecution.podAffinityTerm

    spec.template.spec.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution

    spec.template.spec.affinity.podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution.podAffinityTerm

    • labelSelector:
      • matchExpressions:
        • key: string
        • operator: In, NotIn, Exists or DoesNotExist
        • values: []string
      • matchLabels: map[string]string
    • namespaceSelector:
      • matchExpressions:
        • key: string
        • operator: In, NotIn, Exists or DoesNotExist
        • values: []string
      • matchLabels: map[string]string
    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
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值