Managing Pods in Kubernetes: Using Labels and Selectors

In this blog, we will learn about Kubernetes Labels and Selectors, how it is used to manage pods in Kubernetes, and a simple sample using Labels and Selectors.

What are Labels on Kubernetes?

As the name says, Labels are identifiers that are attached to objects in Kubernetes such as pods, services, etc. Labels are key/value pairs, we assign each object with multiple labels.

Unlike a unique name given to an object, multiple objects can have the same labels which will be useful in grouping them.

Labels are not set by Kubernetes as default, the user who creates the object will be assigning Labels to each object.

What are Selectors on Kubernetes?

As the name says, Selectors are used to select objects based on their Labels.

Since multiple objects can have the same label we can select all the objects with the same Label by the Selectors.

There are two types of Selectors Equality-based and Set-based.

Equality-based – Selectors select objects with the same label you specified in key-value pair.

For example, if you specify the key-value pair as app=webserver, Equality-based selectors only select objects with webserver labels.

Set-based – Selectors select objects if they have any one of the labels you specified in the set.

For example, assume you have specified the set as environment in (production, staging), if the objects have either one of the label’s production or storage it will be selected.

How to use Labels and Selectors

First, let’s see about creating Pods with Labels.

In this example, we are going to create two pods with Labels using the following commands

k run frontend \
    --image=nginx \
    -l name=nginx,env=prod,tier=frontend,component=webserver

k run backend \
    --image=nginx \
    -l name=java,env=prod,tier=backend,component=appserver

Once you created the Pods using the command, run the following command to check the Labels

k get po --show-labels

You will get the following output

creating and listing labels

Now, let’s how we can use Selectors to query using Labels.

To show how a Selector query uses Labels, I am going to get the Pod with the Label component=appserver.

To find the Pod with the Labels component=appserver, run the following command

k get po -l 'component in (appserver)' -L tier

You will get the following output

Query labels using selectors

Next, let’s see how to add Labels to a Pod.

Run the following command to add a new Label to the Pod

k label po frontend version=1.0.1

Now, check the Labels of the Pod you can see a new Label has been added to the Pod frontend as shown below

added label

If you want to modify the Labels, run the above command you used to add a Label with an –overwrite flag as given below

k label po frontend version=1.0.2 --overwrite

If you list the Pods now you can see the label has been altered as shown below

modified label

You can delete any Label by specifying the Label name with a hyphen at the end, if you want to delete a Label run the following command

k label po frontend version-

If you check the labels the version Labels have been deleted from the Pod frontend

Deleted label

Conclusion

In summer, in this blog, you learn about Kubernetes Labels, Selectors, and how to use them.

I believe this blog gives you a basic understanding of Kubernetes Labels and Selectos and how it works.

Other Interesting Blogs

Leave a Comment

Share via
Copy link
Powered by Social Snap