为什么在一次部署中三次提到标签 [英] Why labels are mentioned three times in a single deployment

查看:63
本文介绍了为什么在一次部署中三次提到标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经浏览了以下介绍页面: https://kubernetes.io /docs/concepts/workloads/controllers/deployment/

I've gone over the following docomentation page: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

示例部署yaml如下:

The example deployment yaml is as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

我们在这里可以看到三个不同的时间,其中提到了标签app: nginx.

We can see here three different times where the label app: nginx is mentioned.

为什么我们每个都需要?我很难从官方文档中了解它.

Why do we need each of them? I had a hard time understanding it from the official documentation.

推荐答案

第一个标签用于部署本身,它为该特定部署提供标签.假设您要删除该部署,然后运行以下命令:

The first label is for deployment itself, it gives label for that particular deployment. Lets say you want to delete that deployment then you run following command:

kubectl delete deployment -l app=nginx

这将删除整个部署.

第二个标签selector: matchLabels,它告诉资源(服务等)根据标签来匹配广告连播.因此,假设您要创建的服务的所有Pod的标签均标记为app=nginx,则可以提供以下定义:

The second label is selector: matchLabels which tells the resources(service etc) to match the pod according to label. So lets say if you want to create the service which has all the pods having labels of app=nginx then you provide following definition:

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: LoadBalancer
  ports:
    - port:  80
  selector:
    app: nginx

以上服务将查找matchLabel并绑定为其分配标签app: nginx的吊舱

The above service will look for the matchLabels and bind pods which have label app: nginx assigned to them

第三个标签podTemplate标签,template实际上是podTemplate.它描述了启动的容器.因此,假设您有两个副本部署,并且k8s将启动2个Pod,它们的标签在template: metadata: labels中指定.这是细微但重要的区别,因此您可以为部署和由该部署生成的Pod设置不同的标签.

The third label is podTemplate labels, the template is actually podTemplate. It describe the pod that it is launched. So lets say you have two replica deployment and k8s will launch 2 pods with the label specified in template: metadata: labels. This is subtle but important difference, so you can have the different labels for deployment and pods generated by that deployment.

这篇关于为什么在一次部署中三次提到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆