K8s-Spec.Template.metadata部分的重要性 [英] K8s - Significance of Spec.Template.metadata section

查看:1563
本文介绍了K8s-Spec.Template.metadata部分的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拥有此部分-spec.template.metadata有什么意义?它似乎不是强制性的.但是我想知道它在哪里会非常有用!否则,重复所有选择器的意义何在?

What is the significance of having this section - spec.template.metadata? It does not seem to be mandatory. However I would like to know where it would be very useful! Otherwise what is the point of repeating all the selectors?

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  selector:
    matchLabels:
      app: hello
      tier: backend
      track: stable
  replicas: 7
  template:
    metadata:
      labels:
        app: hello
        tier: backend
        track: stable
    spec:
      containers:
        - name: hello
          image: "gcr.io/google-samples/hello-go-gke:1.0"
          ports:
            - name: http
              containerPort: 80

推荐答案

是什么让您认为这不是必需的?

What makes you think it is not required?

如果不为部署模板提供Metadata,它将失败,并显示以下消息:

If you don't provide the Metadata for a deployment template, it will fail with a message like this:

The Deployment "nginx" is invalid: spec.template.metadata.labels: 
Invalid value: map[string]string(nil): `selector` does not match template `lab
els`

或者如果元数据与选择器不匹配,将失败,并显示以下消息:

Or if the metadata does not match the selector, will fail with a message like this:

The Deployment "nginx" is invalid: spec.template.metadata.labels: 
Invalid value: map[string]string{"run":"nginxv1"}: `selector` does not match template `labels`

此外,如果您不提供selector,则会出现如下错误消息:

Also, if you do not provide the selector it will error with a message like this:

error validating "STDIN": error validating data: ValidationError(Deployment.spec): 
missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; 
if you choose to ignore these errors, turn validation off with --validate=false

使用的yaml如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: nginx
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      run: nginx
  strategy: {}
  template:
    metadata:
      labels:
        run: nginxv1
    spec:
      containers:
      - image: nginx
        name: nginx

当您阅读文档时, selector的说明说:

When you read the docs, the description for the selector says:

选择器字段定义了部署如何查找要管理的Pod.在这种情况下,您只需选择在Pod模板(应用程序:nginx)中定义的标签.但是,只要Pod模板本身满足该规则,就可以使用更复杂的选择规则.

The selector field defines how the Deployment finds which Pods to manage. In this case, you simply select a label that is defined in the Pod template (app: nginx). However, more sophisticated selection rules are possible, as long as the Pod template itself satisfies the rule.

元数据

Kubernetes中的大多数对象都有元数据,它负责存储有关资源的信息,例如名称,标签,注释等.

Metadata

Most objects in Kubernetes have a metadata, it is responsible to store information about the resource like, name, labels, annotations and so on.

创建部署时,创建/更新ReplicaSet和POD需要模板,在这种情况下,它们需要与选择器匹配,否则最终将导致集群周围的孤立资源,而元数据将存储用于链接它们的数据.

When you create a deployment, the template is needed for creation\update of ReplicaSet and PODs, in this case, they need to match the selector, otherwise you would end up with orphan resources around your cluster, and the metadata store the data used to link them.

通过这种方式设计,可以使资源彼此松散耦合,如果简单更改此Deployment \ replicaSet创建的Pod的标签,您会注意到旧的POD继续运行,但是新的POD仍在运行之所以创建一个,是因为旧的不再遵守选择器规则,而ReplicaSet创建了一个新的来保留所需副本的数量.

This was designed this way to make the resources are loosely coupled from each other, if you make simple change to the label of a pod created by this deployment\replicaSet, you will notice that the old POD keep running, but a new one is created, because the old one does not attend the selector rule anymore and ReplicaSet create a new one to keep the number of desired replicas.

这篇关于K8s-Spec.Template.metadata部分的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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