Open In App

How To Use Kubernetes Labels and Selectors?

Kubernetes (also known as K8s) is an open-source Container Management tool. It helps us automate all the processes like deployment, load balancing, rolling update, etc. We can basically deploy, scale, and manage containerized applications.

In Kubernetes, labels and selectors provide a flexible and effective approach to facilitate selection for managing resources like restart/delete/etc. operations, and also for human readability. They are mentioned in the configuration file i.e., yaml files for deployments. They are used to connect services with a pod.



A briefer understanding of Labels and Selectors could be as follows:

Labels are key-value pairs that are used to identify K8s resources like deployments, pods, services. They are completely user-defined, may be tailored to meet our particular needs, and are used to identify and classify resources.



Selectors are utilized to find and categorize resources according to their labels. They can be used to specify criteria for choosing resources that correspond to particular label combinations. They also help in specifying the set of resources to be targeted or managed in the Kubernetes components, including services, deployments, replica sets, and stateful sets.

Prerequisite: Kubernetes Deployments, Kubernetes – Labels & Selectors

Using Labels and Selectors

To use labels and selectors in our applications, we need to specify them in the yaml file.

First of all, for adding labels we could add something like this:

“metadata”:
{
“labels”:
{
“app” : “demo-app”,
“env” : “uat”,
“branch”: “${CI_COMMIT_BRANCH}”
“release”: “1.2.2”
}
}

In the above, we added four labels to the specific replica set in the deployment. We can add as many labels as we want depending on our use case and how we choose to visualize it on the dashboard and select it via selectors. We can have many meaningful labels like version, some Gitlab default variables, etc.

The complete deployment/manifest yaml example can be viewed in the image below:

Here, we apply the yaml to create the resource – in this case Deployment. Since we have added labels to this deployment, we can view the pods with the labels and also view pods by specifying the labels we want by these commands:

kubectl get pods –show-labels

kubectl get pods -l app=demo-app

Based on the labels we add, we can then add selectors to filter the resources.

For example, we can have the following selectors to select one particular pod:

“selector”:
{
“app” : “demo_app”,
“env” : “uat”,
“pod-template-hash”: “3478fsh”
}

We can select the deployments/resources we want to view with the selectors using a kubectl command:

kubectl get pods –selector=”env=uat”

We can have two types of selectors:

  1. Equality-based – As the name suggests is based on operators – “=”, “!=” and “==” which represent equality or inequality. This will help filter the resources. Example -> env=uat
  2. Set-based – These are based on operators – “in”,”not in” and “exists” which will represent present/ not-present in the set. Example -> env in (uat).
  3. Wildcard – These can help in wildcard selection of the pods and we could select multiple resources with this. Wildcard characters used are * and ?. * would mean any number of characters could be present.

Advantages of using Labels and Selectors:

As mentioned earlier labels and selectors are used to select and filter the resources in Kubernetes, but this also has many added advantages. Some of them include:

kubectl delete deployments -l ‘branch = demo’

kubectl get pods -l ‘env=uat’

Good Practices

app.kubernetes.io/name: demo_app

app.kubernetes.io/instance: exception-fix-2

app.kubernetes.io/version: “1.2.2”

app.kubernetes.io/component: mongodb

app.kubernetes.io/managed-by: kubernetes

Conclusion

Labels and selectors are very useful if utilized properly. By using labels, we can maintain our resources very efficiently. They also act as metadata in Kubernetes which gives useful insights about the data (application). This can be quite valuable for people working with the system, as demonstrated by features like human-readable annotations, as it helps them navigate the resources and gives them a sense of how everything fits together.


Article Tags :