WEBVTT

00:00.690 --> 00:04.570
-Hello, and welcome to this lecture
on labels and selectors.

00:04.740 --> 00:06.420
My name is Mumshad Mannambeth.

00:07.570 --> 00:11.200
What do we know about labels
and selectors already?

00:11.540 --> 00:15.550
Labels and selectors are a standard method
to group things together.

00:16.110 --> 00:18.550
Say you have a set of different species,

00:19.060 --> 00:23.210
a user wants to be able to filter them
based on different criteria,

00:24.670 --> 00:27.600
such as based on their class or kind,

00:28.120 --> 00:31.770
if they are domestic or wild,
or say by their color.

00:32.930 --> 00:35.960
Not just group,
you want to be able to filter them

00:36.120 --> 00:40.150
based on a criteria
such as all green animals,

00:40.440 --> 00:43.780
or with multiple criteria,
such as everything green,

00:43.940 --> 00:45.730
that is also a bird.

00:46.410 --> 00:50.330
Whatever that classification may be,
you need the ability

00:50.490 --> 00:54.020
to group things together
and filter them based on your needs.

00:54.820 --> 00:57.680
The best way to do that is with labels.

00:58.720 --> 01:01.840
Labels are properties attached
to each item.

01:02.170 --> 01:05.450
You add properties
to each item for their class,

01:05.740 --> 01:07.130
kind, and color.

01:08.280 --> 01:10.600
Selectors help you filter these items.

01:11.160 --> 01:16.140
For example, when you say Class = Mammal,
we get a list of mammals.

01:16.310 --> 01:20.650
When you say Color = Green,
we get the green mammals.

01:21.720 --> 01:24.480
We see labels
and selectors used everywhere,

01:24.650 --> 01:27.750
such as the keywords you tag
to YouTube videos

01:27.920 --> 01:32.150
or blogs that help users filter
and find the right content.

01:33.130 --> 01:37.450
We see labels added to items
in an online store that help you add

01:37.620 --> 01:40.640
different kinds of filters
to view your products.

01:41.550 --> 01:45.010
How are labels and selectors
used in Kubernetes?

01:45.370 --> 01:48.110
We have created a lot
of different types of objects

01:48.280 --> 01:49.600
in Kubernetes,

01:49.890 --> 01:53.970
Pods, services, ReplicaSets,
deployments, et cetera.

01:54.820 --> 01:58.010
For Kubernetes,
all of these are different objects.

01:58.710 --> 02:02.020
Over time, you may end up
having hundreds or thousands

02:02.190 --> 02:04.100
of these objects in your cluster.

02:04.870 --> 02:08.760
Then you will need a way
to filter and view different objects

02:09.010 --> 02:10.210
by different categories,

02:11.300 --> 02:13.910
such as to group objects by their type,

02:14.700 --> 02:19.640
or view objects by application,
or by their functionality.

02:20.880 --> 02:25.140
Whatever it may be, you can group
and select objects using labels

02:25.310 --> 02:26.790
and selectors.

02:27.880 --> 02:31.550
For each object,
attach labels as per your needs,

02:31.920 --> 02:34.010
like app, function, et cetera.

02:37.160 --> 02:42.100
Then while selecting, specify
a condition to filter specific objects.

02:42.450 --> 02:45.250
For example, app = App1.

02:46.590 --> 02:50.260
How exactly do you specify labels
in Kubernetes?

02:51.010 --> 02:56.060
In a Pod definition file, under metadata,
create a section called labels.

02:56.700 --> 03:00.390
Under that, add the labels
in a key-value format like this.

03:01.240 --> 03:03.740
You can add as many labels as you like.

03:05.840 --> 03:09.300
Once the Pod is created,
to select the Pod with the labels,

03:09.730 --> 03:13.860
use the kube control get pod command
along with the selector option

03:14.240 --> 03:18.320
and specify the condition like app = App1.

03:20.870 --> 03:23.830
Now, this is one use case
of labels and selectors.

03:24.390 --> 03:27.880
Kubernetes objects use labels
and selectors internally

03:28.250 --> 03:30.170
to connect different objects together.

03:30.540 --> 03:35.050
For example, to create a ReplicaSet
consisting of three different Pods,

03:35.470 --> 03:40.340
we first label the Pod definition
and use selector in a ReplicaSet

03:40.580 --> 03:41.660
to group the Pods.

03:42.350 --> 03:47.490
In a ReplicaSet definition file,
you will see labels defined in two places.

03:48.140 --> 03:51.910
Note that this is an area
where beginners tend to make a mistake.

03:52.670 --> 03:55.250
The labels defined
under the template section

03:55.420 --> 03:57.490
are the labels configured on the Pod.

03:58.290 --> 04:02.850
The labels you see at the top are
the labels of the ReplicaSet itself.

04:03.770 --> 04:08.080
We're not really concerned
about the labels of the ReplicaSet for now

04:08.480 --> 04:12.200
because we're trying to get
the ReplicaSet to discover the Pods.

04:12.990 --> 04:16.470
The labels on the ReplicaSet
will be used if you were to configure

04:16.640 --> 04:19.710
some other object
to discover the ReplicaSet.

04:20.680 --> 04:23.400
In order to connect the ReplicaSet
to the Pod,

04:23.820 --> 04:27.890
we configure the selector field
under the ReplicaSet specification

04:28.240 --> 04:30.740
to match the labels defined on the Pod.

04:31.810 --> 04:34.690
A single label will do
if it matches correctly.

04:35.230 --> 04:38.580
However, if you feel there could be
other parts with the same label,

04:39.090 --> 04:43.100
but with a different function,
then you could specify both the labels

04:43.260 --> 04:47.300
to ensure that the right Pods
are discovered by the ReplicaSet.

04:48.320 --> 04:53.210
On creation, if the labels match,
the ReplicaSet is created successfully.

04:54.490 --> 04:57.510
It works the same
for other objects like a service.

04:57.820 --> 04:59.300
When a service is created.

04:59.550 --> 05:03.270
It uses the selector defined
in the service definition file

05:03.610 --> 05:08.220
to match the labels set on the Pods
in the ReplicaSet definition file.

05:09.100 --> 05:11.860
Finally, let's look at annotations.

05:12.270 --> 05:16.310
While labels and selectors are used
to group and select objects,

05:16.960 --> 05:21.390
annotations are used to record
other details for informatory purpose.

05:22.030 --> 05:26.690
For example, two details like name,
version, build information,

05:26.860 --> 05:29.920
et cetera, or contact details,
phone numbers,

05:30.080 --> 05:33.320
email IDs, et cetera,
that may be used for some kind

05:33.490 --> 05:35.120
of integration purpose.

05:36.640 --> 05:40.880
That's it for this lecture
on labels and selectors and annotations.

05:41.320 --> 05:45.310
Head over to the coding exercises section
and practice working with labels

05:45.480 --> 05:47.340
and selectors

