Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K8s - Significance of Spec.Template.metadata section

Tags:

kubernetes

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
like image 505
KitKarson Avatar asked Jan 01 '23 06:01

KitKarson


1 Answers

What makes you think it is not required?

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`

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

The yaml used is the below:

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

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

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.

Metadata

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

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.

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.

like image 185
Diego Mendes Avatar answered Jan 04 '23 00:01

Diego Mendes