标签与选择器 #

一、标签概述 #

标签(Labels)是附加在Kubernetes对象上的键值对,用于组织和选择资源。

1.1 标签特点 #

text
标签特性
    │
    ├── 键值对形式
    │   └── key: value
    │
    ├── 可附加多种对象
    │   ├── Pod
    │   ├── Node
    │   ├── Service
    │   └── Deployment等
    │
    ├── 可用于查询筛选
    │   └── kubectl get pods -l app=nginx
    │
    └── 可用于调度决策
        └── nodeSelector, affinity

1.2 标签语法 #

text
标签键格式
    │
    ├── 可选前缀
    │   ├── DNS子域名格式
    │   └── 不超过253字符
    │
    └── 名称
        ├── 字母数字开头结尾
        ├── 可包含-_.和字母数字
        └── 不超过63字符

标签值格式
    │
    ├── 可以为空
    │
    ├── 字母数字开头结尾(可选)
    │
    ├── 可包含-_.和字母数字
    │
    └── 不超过63字符

二、标签操作 #

2.1 定义标签 #

yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
    env: production
    tier: frontend
    version: "1.25"
spec:
  containers:
  - name: nginx
    image: nginx:1.25

2.2 添加标签 #

bash
# 给Pod添加标签
kubectl label pod nginx env=production

# 给Node添加标签
kubectl label node node-1 disktype=ssd

# 给所有Pod添加标签
kubectl label pods -l app=nginx tier=frontend

# 查看标签
kubectl get pods --show-labels

2.3 修改标签 #

bash
# 修改标签(需要--overwrite)
kubectl label pod nginx env=staging --overwrite

# 修改Node标签
kubectl label node node-1 disktype=hdd --overwrite

2.4 删除标签 #

bash
# 删除标签(键名后加减号)
kubectl label pod nginx env-

# 删除Node标签
kubectl label node node-1 disktype-

2.5 查看标签 #

bash
# 显示所有标签
kubectl get pods --show-labels

# 输出示例
NAME    READY   STATUS    RESTARTS   AGE   LABELS
nginx   1/1     Running   0          1m    app=nginx,env=production,tier=frontend

# 显示特定标签列
kubectl get pods -L app,env

# 输出示例
NAME    READY   STATUS    RESTARTS   AGE   APP     ENV
nginx   1/1     Running   0          1m    nginx   production

三、标签选择器 #

3.1 等值选择器 #

bash
# 等于
kubectl get pods -l app=nginx

# 不等于
kubectl get pods -l app!=nginx

# 多个条件(AND)
kubectl get pods -l app=nginx,env=production

3.2 集合选择器 #

bash
# in操作符
kubectl get pods -l 'env in (production,staging)'

# notin操作符
kubectl get pods -l 'env notin (development)'

# 存在键
kubectl get pods -l 'app'

# 不存在键
kubectl get pods -l '!app'

3.3 YAML中的选择器 #

yaml
# Deployment选择器
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
    matchExpressions:
    - key: env
      operator: In
      values:
      - production
      - staging
    - key: tier
      operator: Exists
  template:
    metadata:
      labels:
        app: nginx
        env: production
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx

3.4 选择器操作符 #

操作符 说明 示例
In 值在集合中 env In (prod,staging)
NotIn 值不在集合中 env NotIn (dev)
Exists 键存在 app Exists
DoesNotExist 键不存在 env DoesNotExist

四、注解 #

4.1 注解概述 #

注解(Annotations)用于存储非标识性元数据。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  annotations:
    description: "This is a nginx pod"
    owner: "team-backend"
    prometheus.io/scrape: "true"
    prometheus.io/port: "9113"
spec:
  containers:
  - name: nginx
    image: nginx

4.2 注解与标签对比 #

特性 标签 注解
用途 标识、选择 元数据存储
长度限制 63字符 256KB
查询过滤 支持 不支持
典型场景 调度、服务发现 描述、配置

4.3 注解操作 #

bash
# 添加注解
kubectl annotate pod nginx description="nginx pod"

# 修改注解
kubectl annotate pod nginx description="updated" --overwrite

# 删除注解
kubectl annotate pod nginx description-

# 查看注解
kubectl describe pod nginx | grep -A 5 Annotations

五、节点选择器 #

5.1 nodeSelector #

nodeSelector是最简单的节点选择方式。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  nodeSelector:
    disktype: ssd
    zone: east
  containers:
  - name: nginx
    image: nginx
bash
# 给节点添加标签
kubectl label node node-1 disktype=ssd
kubectl label node node-1 zone=east

# 查看节点标签
kubectl get nodes --show-labels

5.2 常用节点标签 #

标签 说明
kubernetes.io/arch CPU架构(amd64, arm64)
kubernetes.io/os 操作系统(linux, windows)
kubernetes.io/hostname 节点主机名
topology.kubernetes.io/zone 可用区
topology.kubernetes.io/region 区域
node.kubernetes.io/instance-type 实例类型

六、节点亲和性 #

6.1 节点亲和性 #

节点亲和性提供更灵活的节点选择机制。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: kubernetes.io/arch
            operator: In
            values:
            - amd64
          - key: disktype
            operator: In
            values:
            - ssd
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 80
        preference:
          matchExpressions:
          - key: zone
            operator: In
            values:
            - east
      - weight: 20
        preference:
          matchExpressions:
          - key: instance-type
            operator: In
            values:
            - high-mem
  containers:
  - name: nginx
    image: nginx

6.2 亲和性类型 #

text
节点亲和性类型
    │
    ├── requiredDuringSchedulingIgnoredDuringExecution
    │   ├── 硬性要求
    │   └── 不满足则Pod无法调度
    │
    └── preferredDuringSchedulingIgnoredDuringExecution
        ├── 软性偏好
        ├── 权重打分
        └── 不满足也可以调度

6.3 操作符 #

操作符 说明
In 值在集合中
NotIn 值不在集合中
Exists 键存在
DoesNotExist 键不存在
Gt 值大于指定值(数值)
Lt 值小于指定值(数值)

七、Pod亲和性 #

7.1 Pod亲和性 #

Pod亲和性用于将Pod调度到特定Pod所在的节点。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: web-app
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: cache
        topologyKey: kubernetes.io/hostname
  containers:
  - name: web
    image: nginx

7.2 Pod反亲和性 #

Pod反亲和性用于将Pod分散到不同节点。

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: web
            topologyKey: kubernetes.io/hostname
      containers:
      - name: web
        image: nginx

7.3 拓扑键 #

拓扑键 说明
kubernetes.io/hostname 单个节点
topology.kubernetes.io/zone 可用区
topology.kubernetes.io/region 区域

7.4 完整亲和性示例 #

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 60
            preference:
              matchExpressions:
              - key: disktype
                operator: In
                values:
                - ssd
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 30
            podAffinityTerm:
              labelSelector:
                matchLabels:
                  app: cache
              topologyKey: kubernetes.io/hostname
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: web
            topologyKey: kubernetes.io/hostname
      containers:
      - name: web
        image: nginx

八、污点和容忍 #

8.1 污点(Taint) #

污点用于阻止Pod调度到特定节点。

bash
# 添加污点
kubectl taint nodes node-1 key=value:NoSchedule

# 添加NoExecute污点
kubectl taint nodes node-1 key=value:NoExecute

# 删除污点
kubectl taint nodes node-1 key:NoSchedule-

# 查看污点
kubectl describe node node-1 | grep Taints

8.2 污点效果 #

效果 说明
NoSchedule 不调度新Pod
PreferNoSchedule 尽量不调度
NoExecute 不调度新Pod,驱逐已有Pod

8.3 容忍(Toleration) #

容忍允许Pod调度到有污点的节点。

yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  tolerations:
  - key: "key"
    operator: "Equal"
    value: "value"
    effect: "NoSchedule"
  containers:
  - name: nginx
    image: nginx

8.4 容忍操作符 #

yaml
# Equal操作符
tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"

# Exists操作符(忽略value)
tolerations:
- key: "key"
  operator: "Exists"
  effect: "NoSchedule"

# 容忍所有污点
tolerations:
- operator: "Exists"

# 容忍特定效果的所有污点
tolerations:
- key: "key"
  operator: "Exists"
  effect: "NoSchedule"

8.5 NoExecute示例 #

yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  tolerations:
  - key: "node.kubernetes.io/unreachable"
    operator: "Exists"
    effect: "NoExecute"
    tolerationSeconds: 300
  containers:
  - name: nginx
    image: nginx

九、实际应用场景 #

9.1 环境隔离 #

yaml
# 生产环境节点
kubectl label node node-1 env=production
kubectl taint nodes node-1 env=production:NoSchedule

# 生产环境Pod
apiVersion: v1
kind: Pod
metadata:
  name: prod-app
spec:
  nodeSelector:
    env: production
  tolerations:
  - key: "env"
    operator: "Equal"
    value: "production"
    effect: "NoSchedule"
  containers:
  - name: app
    image: myapp

9.2 高可用部署 #

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: web
            topologyKey: topology.kubernetes.io/zone
      containers:
      - name: web
        image: nginx

9.3 GPU节点调度 #

yaml
# GPU节点
kubectl label node gpu-node hardware=gpu
kubectl taint nodes gpu-node nvidia.com/gpu=true:NoSchedule

# GPU Pod
apiVersion: v1
kind: Pod
metadata:
  name: gpu-app
spec:
  nodeSelector:
    hardware: gpu
  tolerations:
  - key: "nvidia.com/gpu"
    operator: "Exists"
    effect: "NoSchedule"
  containers:
  - name: gpu-app
    image: gpu-app
    resources:
      limits:
        nvidia.com/gpu: 1

十、总结 #

10.1 核心要点 #

要点 说明
标签 键值对,用于标识和选择
选择器 等值选择、集合选择
nodeSelector 简单节点选择
节点亲和性 灵活的节点选择
Pod亲和性 Pod间关系调度
污点容忍 节点排斥和Pod容忍

10.2 下一步 #

掌握了标签与选择器后,让我们学习 Deployment,开始管理工作负载资源。

最后更新:2026-03-28