Why labels are mentioned three times in a single deployment
Asked Answered
L

5

23

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

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

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.

Lodicule answered 30/1, 2019 at 8:53 Comment(0)
M
21

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

This will delete the entire deployment.

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

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

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.

Mentor answered 30/1, 2019 at 10:45 Comment(4)
I assume then that the second one is required and the other two are optional?Lodicule
Where do we use the deployment label? (the first label)Brynnbrynna
The first label is added on the deployment object to select the deployment using labels. For example you have a group of deployment which you want to select from a single command. You can label all the deployments with one label and select from one command : kubectl get deployment -l app=nginxMentor
Is there a use case for a Deployment definition where the labels listed in .spec.selector.matchLabels differ from the ones listed in the same Deployment's .spec.template.metadata.labels section?Delusive
B
9

First label:

It is deployment label which is used to select deployment. You can use below command using first label:

kubectl get deployment -l app=nginx

Second Label:

It is not a label . It is label selector to select pod with labels nginx. It is used by ReplicaSet.

Third Label:

It is pod label to identify pods. It is used by ReplicaSet to maintain desired num of replica and for that label selector is used. Also it is used to selects pod with below command:

kubectl get pods -l app=nginx
Beutler answered 30/1, 2019 at 9:0 Comment(0)
U
3

As we know it, the labels are to identify the resources,

  • First label identifies the Deployment itself
  • Third one is falls under the Pod template section. So, this one is specific to the Pod.
  • Second one i.e the matchLabels is used to tell Services, ReplicaSet and other resources to act on the resources on the specified label conditions.

While first and third ones are label assignment to Deployment and Pods respectively, the second one is matching condition expression rather than assignment.

Though all 3 have same labels in the real world examples, First one can be different than second and third ones. But, second and third one usually to be identical as the second is the conditional expression that acts upon third one.

Unexperienced answered 10/8, 2021 at 1:27 Comment(0)
B
2

.metadata.labels is for labeling the deployment object itself, you don't necessarily need it, but like other answers said, it helps you organize objects.

.spec.selector tells the deployment(under the hood it is the ReplicaSet object) how to find the pods to manage. For your example, it will manage pods with label app: nginx.

But how do you tell the ReplicaSet controller to create pods with that label in the first place? You define that in the pod template, .spec.template.metadata.labels.

Broadleaved answered 10/7, 2020 at 22:42 Comment(0)
O
-1

TL;DR

1st label (.metadata.labels): the label for the Deployments

2nd label (.spec.selector.matchLabels): tell ReplicaSets what pods to govern.

3rd label (.spec.template.metadata.labels): the label for the Pod so that it can be found by the ReplicaSets.

Ouidaouija answered 30/3, 2023 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.