Pages

Sunday, January 9, 2022

Kubernetes - Labels, Selector and matchLabels

Labels are a mechanism that we use to organize objects in kubernetes.a K8s object can be anything from containers, pods to services and deployments. Labels are key-value pairs that we can attach to a resource for identification. The labels contain information and are used by kubernetes to query objects based on these labels.


A label can be applied to a pods as shown below,

[jagadishmanchala@Jagadish-theOne:k8s] cat simple-label.yml 

apiVersion: v1

kind: Pod

metadata:

  name: testing-service

  labels:

    env: dev

spec:

  containers:

    - name: test-ser

      image: docker.io/jagadesh1982/testing-service

      ports:

      - containerPort: 9876


From the above config we are actually creating a pod with the label “env=dev”. 


To see the label available for a pod we can use the command,

If we want to assign a label while a pod is running, we can use

Now we can see the labels attached to the pod using,


Consider the below deployment, 

[jagadishmanchala@Jagadish-theOne:k8s] cat simple-label.yml 

apiVersion: apps/v1

kind: Deployment

metadata:

  name: testing-service-v1

  labels:

    app: testing-service

spec:

  replicas: 3

  selector:

    matchLabels:

      app: testing-service

      version: "1.0"

  template:

    metadata:

      labels:

        app: testing-service

        version: "1.0"

    spec:

      containers:

      - name: testing-service

        image: docker.io/jagadesh1982/testing-service

        ports:

        - name: http

          containerPort: 9876

        env:

        - name: VERSION

          value: "1.0"


In the above config, we can see that there are labels defined in multiple places as 


************

metadata:

  name: testing-service-v1

  labels:

    app: testing-service

    tier: backend

spec:

  replicas: 3

  selector:

    matchLabels:

      app: testing-service

  template:

    metadata:

      labels:

        app: testing-service

        tier: backend

************

In the above config, we can see metadata.labels, selector.matchLabels and template.metadata.labels. What are these and in this article we will see what these do exactly?


The first metadata talks about the deployment itself. This means we are assigning a label “app: testing-service” and “tier: backed” to the deployment itself. This means we can delete the deployment using the 

“kubectl delete deploy -l app=testing-service,tier=backend”. This label is used to identify the kind that we are creating which is deployment here.


Now the selector is used for a whole different purpose. Once we create a deployment,replica set or replication controller, pods also get created along. Now we need to tell the deployment how to find the pods that are created.For instance, in future if we want to scale a pod from 1 to 3, the deployment needs to find first what pods are currently running. For this it needs a way to identify the specific pods that are created as a part of deployment. The labels defined in the selector field helps deployment to identify the pods that are created as a part of that deployment. Now from the above configuration it will find all pods that have labels “app: testing-service” and will manage the pods. We call this a selector label. 



To say exactly , to find out what pods are running or if we need to manage a group of pods we need to find them. The labels defined in the selector fields will help us to find the pods to manage them


But in order to manage or find pods running with a specific selector label, we need to assign them the labels. Once we assign the labels, then only we can find them using the selector. The labels that need to be assigned to the pods are defined in the  template.metadata.labels.


So

.metadata.labels is for labeling the kind that we are creating. In the above case, the kind is deployment. If we want to find the deployment, we can use the labels defined in the metadata.labels


.spec.selector tells the deployment or replica set or replication controller how to find pods to manage , scale or delete.


.spec.template.metadata.labels are used to create the pods with labels so that deployment or replica set or replication controller can find or manage the pods.


In the above case, labels defined in .spec.selector and .spec.template.metadata.labels should be same


Hope this helps in understanding the labels, selectors and podTemplate labels.

No comments :

Post a Comment