1
00:00:00,740 --> 00:00:04,880
Hello and welcome to this lecture on labels and selectors.

2
00:00:04,880 --> 00:00:12,790
My name is  Mumshad Mannambeth what do we know about labels and selectors already labels and selectors

3
00:00:12,820 --> 00:00:16,040
are a standard method to group things together.

4
00:00:16,120 --> 00:00:19,120
Say you have a set of different species.

5
00:00:19,120 --> 00:00:27,830
A user wants to be able to filter them based on different criteria such as based on their class or kind

6
00:00:28,110 --> 00:00:34,260
if they are domestic or wild or see by their color and not just group.

7
00:00:34,290 --> 00:00:42,510
You want to be able to filter them based on a criteria such as all green animals or with multiple criteria

8
00:00:42,600 --> 00:00:44,260
such as everything green.

9
00:00:44,280 --> 00:00:46,150
That is also a bird.

10
00:00:46,410 --> 00:00:53,400
Whatever that classification maybe you need the ability to group things together and filter them based

11
00:00:53,400 --> 00:01:02,190
on your needs and the best way to do that is with labels. Labels or properties attached to each item.

12
00:01:02,190 --> 00:01:11,210
So you add properties to each item for their class kind and color selectors help you filter these items.

13
00:01:11,210 --> 00:01:18,260
For example when you say class equals mammal we get a list of mammals and when you say color equals

14
00:01:18,260 --> 00:01:26,310
green we get the green mammals we see labels and selectors used everywhere such as the keywords you

15
00:01:26,310 --> 00:01:34,340
tag to YouTube videos or blogs that help users filter and find the right content we see labels added

16
00:01:34,340 --> 00:01:41,590
to items in an online store that help you add different kinds of filters to view your products.

17
00:01:41,610 --> 00:01:45,420
So how are labels and selectors used in Kubernetes?

18
00:01:45,480 --> 00:01:53,550
We have created a lot of different types of Objects in Kuberentes. Pods, Services, ReplicaSets and Deployments.

19
00:01:53,550 --> 00:01:58,750
etc. For Kubernetes, all of these are different objects.

20
00:01:58,860 --> 00:02:04,980
Over time you may end up having hundreds or thousands of these objects in your cluster.

21
00:02:04,980 --> 00:02:12,360
Then you will need a way to filter and view different objects by different categories such as to group

22
00:02:12,450 --> 00:02:22,200
objects by their type or view objects by application or by their functionality whatever it may be.

23
00:02:22,260 --> 00:02:31,190
You can group and select objects using labels and selectors for each object attach labels as per your

24
00:02:31,190 --> 00:02:34,300
needs, like app, function etc.

25
00:02:37,270 --> 00:02:42,350
Then while selecting specify a condition to filter specific objects.

26
00:02:42,490 --> 00:02:50,170
For example app == App1. So how exactly do you specify labels in kubernetes.

27
00:02:51,070 --> 00:02:54,070
In a pod-definition file, under metadata,

28
00:02:54,110 --> 00:03:01,110
create a section called labels. Under that add the labels in a key value format like this.

29
00:03:01,310 --> 00:03:09,800
You can add as many labels as you like. Once the pod is created, to select the pod with the labels

30
00:03:09,850 --> 00:03:16,540
use the kubectl get pods command along with the selector option, and specify the condition like

31
00:03:16,690 --> 00:03:20,860
app=App1.

32
00:03:20,920 --> 00:03:27,930
Now this is one use case of labels and selectors. Kubernetes objects use labels and selectors internally

33
00:03:28,290 --> 00:03:34,680
to connect different objects together. For example to create a replicaset consisting of 3 different

34
00:03:34,680 --> 00:03:35,610
pods,

35
00:03:35,610 --> 00:03:43,080
we first label the pod definition and use selector in a replicaset to group the pods . In the replica-set

36
00:03:43,080 --> 00:03:44,820
set definition file.

37
00:03:44,820 --> 00:03:48,240
You will see labels defined in two places.

38
00:03:48,240 --> 00:03:52,620
Note that this is an area where beginners attempt to make a mistake.

39
00:03:52,740 --> 00:03:59,250
The labels defined under the template section are the labels configured on the pods. The labels you see

40
00:03:59,250 --> 00:04:03,910
at the top are the labels of the replicas set itself.

41
00:04:03,900 --> 00:04:09,870
We're not really concerned about the labels of the replica set for now because we are trying to get

42
00:04:09,870 --> 00:04:12,940
the replica set to discover the pods.

43
00:04:13,050 --> 00:04:18,960
The labels on the replica set will be used if you were to configure some other object to discover the

44
00:04:18,960 --> 00:04:26,250
replica set in order to connect the replica set to the pod we configure the selector field under the

45
00:04:26,250 --> 00:04:34,230
replica set specification to match the labels defined on the pod a single label will do if it matches

46
00:04:34,230 --> 00:04:35,230
correctly.

47
00:04:35,250 --> 00:04:40,950
However if you feel there could be other pods with the same label but with a different function then

48
00:04:40,950 --> 00:04:48,670
you could specify both the labels to ensure that the right pods are discovered by the replica set on

49
00:04:48,670 --> 00:04:49,420
creation.

50
00:04:49,420 --> 00:04:56,970
If the labels match the replica set is created successfully it works the same for other objects like

51
00:04:56,970 --> 00:05:04,200
a service when a service is created it uses the selector defined in the service definition file to match

52
00:05:04,230 --> 00:05:09,130
the labels set on the pods in the replica set definition file.

53
00:05:09,150 --> 00:05:17,820
Finally let’s look at annotations. While labels and selectors are used to group and select objects, annotations

54
00:05:17,820 --> 00:05:21,960
are used to record other details for inflammatory purpose.

55
00:05:22,080 --> 00:05:30,570
For example tool details like name, version build information etc or contact details, phone numbers, email

56
00:05:30,570 --> 00:05:38,840
ids etc, that may be used for some kind of integration purpose. Well, that’s it for this lecture on

57
00:05:38,840 --> 00:05:41,320
labels and selectors and annotations.

58
00:05:41,360 --> 00:05:46,580
Head over to the coding exercises section and practice working with labels and selectors.
