Saiyam Pathak avatar
By Saiyam Pathak
Director of Technical Evangelism

Description

Understand the importance of pod labels and how they work, how to label a Kubernetes pod, and how to use them as “selectors”.


Transcription

What are pod labels?

In this video, we'll be discussing labels and selectors. Now, labels in Kubernetes are a set of key-value pairs attached to a Kubernetes object such as pods. It is basically used to provide attributes to create a meaningful Kubernetes object. So, the command to view the labels is kubectl get pods --show-labels.

Labeling a pod

So you can see this pod named nginx, and this label is attached. Now, you can attach a label during the pod creation, get the labels, attach the labels or add the labels even after creating a pod. So you can edit the manifest or use the command kubectl label pod nginx demo=true. If I rerun the command, we can see an addition of demo=true. So this particular label has been added. So this key-value type of label is called an equity-based label. Just let's look at a YAML file to understand it more.

So let's look at the YAML file to understand it more precisely. So, in this case, every Kubernetes object has a metadata section. Now, labels are defined in the metadata section. So there is a field called label and is the location where the labels are written.

What are pod selectors?

Now, another part where labels are used is the Selector Section. So there's a field called selectors in most of the Kubernetes objects, so they select on the basis of the labels. So, for example, in this particular case, the deployment uses the selector and matches the labels based on the app Nginx. So all the pods that are created using this deployment will have this label as well. And these are matched using the app Nginx. Now, this is an equity-based operator. So there is another equity-based label, and there is another set-based. It means you can do more complex operations, like, in, not in, or, exists, and both matched labels and match expressions, which is the set-based, can be used in conjunction. But if they both are specified, they both have to be true. So it's "and" between both match labels and match expressions.

Now, the select labels and selectors play a significant role in services. So say, you have a service that has to get attached to a particular port or has to be defined, whenever the traffic comes to that particular endpoint, particular service. So it has to route the traffic to a specific number of pods. Now, this happens based on the labels and selectors. So ports are labeled based on app Nginx.

And now, if the selector is also based on app Nginx, then that particular service will send the traffic to the ports of matching the label app Nginx. So that's how labels and selectors work on a very high-level overview. That's it for this lecture. Thank you for watching. See you in the next one.

Civo course complete badge

You've successfully completed our course on Kubernetes concepts

We hope you enjoyed learning and encourage you to check out our other courses to further expand your knowledge on Kubernetes