-Hello, and welcome to this lecture on labels and selectors.
My name is Mumshad Mannambeth.
What do we know about labels and selectors already?
Labels and selectors are a standard method to group things together.
Say you have a set of different species,
a user wants to be able to filter them based on different criteria,
such as based on their class or kind,
if they are domestic or wild, or say by their color.
Not just group, you want to be able to filter them
based on a criteria such as all green animals,
or with multiple criteria, such as everything green,
that is also a bird.
Whatever that classification may be, you need the ability
to group things together and filter them based on your needs.
The best way to do that is with labels.
Labels are properties attached to each item.
You add properties to each item for their class,
kind, and color.
Selectors help you filter these items.
For example, when you say Class = Mammal, we get a list of mammals.
When you say Color = Green, we get the green mammals.
We see labels and selectors used everywhere,
such as the keywords you tag to YouTube videos
or blogs that help users filter and find the right content.
We see labels added to items in an online store that help you add
different kinds of filters to view your products.
How are labels and selectors used in Kubernetes?
We have created a lot of different types of objects
in Kubernetes,
Pods, services, ReplicaSets, deployments, et cetera.
For Kubernetes, all of these are different objects.
Over time, you may end up having hundreds or thousands
of these objects in your cluster.
Then you will need a way to filter and view different objects
by different categories,
such as to group objects by their type,
or view objects by application, or by their functionality.
Whatever it may be, you can group and select objects using labels
and selectors.
For each object, attach labels as per your needs,
like app, function, et cetera.
Then while selecting, specify a condition to filter specific objects.
For example, app = App1.
How exactly do you specify labels in Kubernetes?
In a Pod definition file, under metadata, create a section called labels.
Under that, add the labels in a key-value format like this.
You can add as many labels as you like.
Once the Pod is created, to select the Pod with the labels,
use the kube control get pod command along with the selector option
and specify the condition like app = App1.
Now, this is one use case of labels and selectors.
Kubernetes objects use labels and selectors internally
to connect different objects together.
For example, to create a ReplicaSet consisting of three different Pods,
we first label the Pod definition and use selector in a ReplicaSet
to group the Pods.
In a ReplicaSet definition file, you will see labels defined in two places.
Note that this is an area where beginners tend to make a mistake.
The labels defined under the template section
are the labels configured on the Pod.
The labels you see at the top are the labels of the ReplicaSet itself.
We're not really concerned about the labels of the ReplicaSet for now
because we're trying to get the ReplicaSet to discover the Pods.
The labels on the ReplicaSet will be used if you were to configure
some other object to discover the ReplicaSet.
In order to connect the ReplicaSet to the Pod,
we configure the selector field under the ReplicaSet specification
to match the labels defined on the Pod.
A single label will do if it matches correctly.
However, if you feel there could be other parts with the same label,
but with a different function, then you could specify both the labels
to ensure that the right Pods are discovered by the ReplicaSet.
On creation, if the labels match, the ReplicaSet is created successfully.
It works the same for other objects like a service.
When a service is created.
It uses the selector defined in the service definition file
to match the labels set on the Pods in the ReplicaSet definition file.
Finally, let's look at annotations.
While labels and selectors are used to group and select objects,
annotations are used to record other details for informatory purpose.
For example, two details like name, version, build information,
et cetera, or contact details, phone numbers,
email IDs, et cetera, that may be used for some kind
of integration purpose.
That's it for this lecture on labels and selectors and annotations.
Head over to the coding exercises section and practice working with labels
and selectors