部署规范和模板中的标签

ove*_*nge 3 kubernetes kubernetes-pod

在下面的 yaml 中:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: my-nginx # Line 6
spec:             # Line 7  
  replicas: 2
  selector:
    matchLabels:
      app: my-nginx    # line 11
  template:
    metadata:
      labels:
        app: my-nginx   # Line 15
    spec:                # Line 16
      containers:
      - name: my-nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: "128Mi" #128 MB
            cpu: "200m" #200 millicpu (.2 cpu or 20% of the cpu)
Run Code Online (Sandbox Code Playgroud)

app: nginx第 6 行为部署指定了标签 ( )。

第 7 行的部署规范使用第 16 行中提到的 Pod 规范


  1. selector字段 with的目的是什么matchLabels

  2. template字段 with的目的是什么labels

P..*_*... 5

尝试添加注释来解释标签的作用:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: my-nginx # LABEL-A: <--this label is to manage the deployment itself. this may be used to filter the deployment based on this label. 
spec:              
  replicas: 2
  selector:
    matchLabels:
      app: my-nginx    #LABEL-B:  <--  field defines how the Deployment finds which Pods to manage.
  template:
    metadata:
      labels:
        app: my-nginx   #LABEL-C: <--this is the label of the pod, this must be same as LABEL-B
    spec:                
      containers:
      - name: my-nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: "128Mi" #128 MB
            cpu: "200m" #200 millicpu (.2 cpu or 20% of the cpu)
Run Code Online (Sandbox Code Playgroud)

LABEL-A: <--该标签用于管理部署本身。这可用于根据此标签过滤部署。示例用法LABEL-A用于部署管理,例如过滤。

k get deployments.apps -L app=my-nginx
Run Code Online (Sandbox Code Playgroud)

LABEL-B:<-- 必须有某个地方让我们告诉复制控制器管理 Pod。该字段定义 Deployment 如何查找要管理的 Pod。根据 Pod 的这些标签,复制控制器确保它们已准备就绪。

LABEL-C: <--这是 LABEL-B 用于监控的 pod 的标签。这必须与 LABEL-B 相同