Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why labels are mentioned three times in a single deployment

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.

like image 201
Mugen Avatar asked Jan 30 '19 08:01

Mugen


People also ask

Why labels are used in Kubernetes?

Kubernetes labels are key-value pairs that can connect identifying metadata with Kubernetes objects. Kubernetes offers integrated support for using these labels to query objects and perform bulk operations on selected subsets.

How labels work in Kubernetes?

Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the core system. Labels can be used to organize and to select subsets of objects.

What are labels in OpenShift?

Labels are used in OpenShift® environments to label key pairs, such as pods. Labels are used to specify identifying attributes of objects that are meaningful and relevant within the environment, and are generally used to organize subsets of objects.

What is difference between labels and selectors in Kubernetes?

Labels are properties that we can attach to each item for example for their type, kind, and so on. Selectors help us in finding these items. You can think of a selector as a filter. We could label pods based on some attributes i.e. app name, front-end, back-end.


1 Answers

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.

like image 76
Logamani Gunasekaran Avatar answered Oct 24 '22 17:10

Logamani Gunasekaran