What is the difference between Label and Selector in kubernetes?

What is the difference between Label and Selector in kubernetes?

While playing around with kubernetes, you will come across terms like Selector and Label. These two concepts are something that can be easily confused as they are similar in nature and usage. Both of them are used to label objects or resources in a Kubernetes cluster, but they have their own use cases and characteristics. In this article, we will see the differences between these two terminologies and understand when to use one or the other.

What is a Selector? #

A Selector is a String expression that can be used to filter objects based on the value of specific fields. If you are familiar with database terminology, you can think of Selectors as the like clause in a database query. It can be used to filter out objects based on their label values. In a simple example, if we want to filter out pods based on their names, we can use Selector as follows

$ kubectl get pods --selector=name=nginx-pod

This will select only the pods whose name is “nginx-pod”.

There are a few different selectors that can be used to filter objects.

Name – This can be used to filter the objects based on the pod name.

Namespace – This can be used to filter the objects based on the namespace.

Type – This can be used to filter the objects based on type.

Labels – This can be used to filter the objects based on label values.

What is a Kubernetes Label? #

A label is a key/value pair that is attached to objects in a kubernetes cluster. These labels can be used to filter and identify the objects in the cluster. Labels can be attached to pods, services, replicasets, and other objects in the cluster. These labels are added as metadata to the objects in the cluster, and they can be used for a variety of different use cases.

In a simple example, if we want to filter out pods based on their labels, we can use Selector as follows

$ kubectl get pods --selector=label=app=nginx

This will select only the pods whose label has “app” with value as “nginx”.

There are a few different selectors that can be used to filter objects.

  • Name – This can be used to filter the objects based on the pod name.
  • Namespace – This can be used to filter the objects based on the namespace.
  • Type – This can be used to filter the objects based on type.
  • labels – This can be used to filter the objects based on label values.

Difference between Selector and Label in Kubernetes #

As we have seen above, both a selector and a label can be used to filter objects in a cluster. But there are a few key differences between the two.

Selectors are more efficient since they are pre-computed expressions. Labels have to be dynamically updated each time the label value changes.

Selectors are evaluated on the target node where a pod is scheduled. Labels are evaluated on the kube controller which means that all pods are evaluated against the label even if they don’t have that label.

Selectors can be evaluated on any node in the cluster whereas labels can only be evaluated against pods that are on the same node as the kube controller.

How to use Kubernetes Selector? #

As we have seen above, we can use the selector to filter out the objects in the kubernetes cluster. Let’s see how to use it in a few different scenarios

To filter out pods based on their names:

$ kubectl get pods --selector=name=nginx-pod

This will select only the pods whose name is “nginx-pod”.

To filter out pods based on the namespace:

$ kubectl get pods --selector=namespace=org-kube-example

This will select only the pods whose namespace is org-kube-example

To filter out pods based on their types:

$ kubectl get pods --selector=type=frontend

This will select only the pods whose type is frontend

How to use Label? #

As we have seen above, we can use the selector to filter out the objects in the kubernetes cluster.

Let’s see how to use it in a few different scenarios

To filter out pods based on their labels:

$ kubectl get pods --selector=label=app=nginx

This will select only the pods whose label has “app” with value as “nginx”

To filter out pods based on the namespace:

$ kubectl get pods --selector=namespace=org-kube-example

This will select only the pods whose namespace is org-kube-example

To filter out pods based on the type:

$ kubectl get pods --selector=type=frontend

This will select only the pods whose type is frontend

Conclusion #

In this article, we looked at the differences between a selector and a label in kubernetes. We learnt that both a selector and a label can be used to filter objects in a cluster. We also saw that there are a few key differences between the two. A selector is more efficient since it is pre-computed expressions. Labels have to be dynamically updated each time the label value changes. Selectors are evaluated on the target node where a pod is scheduled. Labels are evaluated on the kube controller which means that all pods are evaluated against the label even if they don’t have that label. Selectors can be evaluated on any node in the cluster whereas labels can only be evaluated against pods that are on the same node as the kube controller.

Get your employees up-to-date with online courses and certifications in web tech, software development, and IT from London School of Emerging Technology.

Powered by BetterDocs