Kubernetes: Labels and Selectors

Updated: Sep 12, 2020

Hello Readers , In the last article we studied about manual scheduling of pods. Now continuing further let us discuss labels and selectors in kubernetes. Labels and selectors are in normal term a way to group things together.

Labels and selectors allow us to group things together and filter them based on our needs.

Labels are like properties of an object and selectors help in filtering these objects. This is a way we define labels and selectors in non-technical terms. Let us see how they are used in kubernetes.

Starting from our first article till now we have created a number of objects like pod , deployments, replica sets , services etc, now imagine a production environment where hundreds and thousands of such objects may be running and may be responsible for running different application components.Now what if we require pods related to a particular application out of hundreds of other pod running ? How to filter the required pods out of all pods ? The answer to this is Labels and Selectors.

We can specify labels under the metadata section of the object definition file . Below is an example to select all pods with a particular label on them.

Another very important use-case of labels and selectors is how replica-set works? Now how will a replica set know which pods to scale in and scale-out . A replica set identifies the pod to work on with the help of labels and selectors.

Let us take an example of a replica set configuration file.

Here a replica set configuration says that it has to scale up the pods with a matching label "tier: frontend" to a count of 3.

We see that there is a label set in the very beginning of the definition file. Don't worry about them, they are only related to replica set.

I hope this clears things about labels and selectors. In the next article we will study about taints and toleration.


329 views0 comments

Recent Posts

See All