WEBVTT

00:00.773 --> 00:06.200
-In this video, we will go
over the practice test for node affinity.

00:07.400 --> 00:13.067
The first question is to identify
the labels on node node01.

00:13.290 --> 00:20.400
To count the number of labels on node01,
so let's do kubectl describe node node01.

00:21.467 --> 00:23.333
Let's look at the labels.

00:23.500 --> 00:26.500
We have one, two, three,
four, five, five labels.

00:27.390 --> 00:28.741
We're going to select five.

00:32.933 --> 00:39.233
The next question is what is the value set
to the label beta.kubernetes.io/arch

00:41.500 --> 00:46.833
on node01?
We're already on node01, and it is amd64.

00:48.633 --> 00:49.633
That's this one.

00:51.233 --> 00:57.467
The next question is to apply
a label called color=blue to node node01.

00:58.600 --> 01:02.300
For that, we're going to use
the kubectl label command.

01:02.670 --> 01:06.053
Let's take a look at the help
and here we have some examples,

01:06.220 --> 01:10.267
so you have kubectl label,
the pods or node,

01:10.561 --> 01:17.010
what is the object that we want to apply
the label to, and then the node name

01:17.310 --> 01:18.666
and then just the label.

01:18.833 --> 01:20.233
It's pretty straightforward.

01:20.882 --> 01:27.400
We're going
to do kubectl label node node01

01:28.800 --> 01:31.933
and then color=blue.

01:34.167 --> 01:40.133
Let's verify that it's set correctly.

01:40.867 --> 01:44.967
We're going to go back to labels
and we see the color=blue.

01:50.067 --> 01:53.520
The next one is to create
a new deployment named blue

01:53.730 --> 01:55.900
with the nginx image and three replicas.

01:56.567 --> 01:58.900
Let us create the deployment,

02:01.933 --> 02:11.067
the name is blue, the image is nginx,
and replicas is three.

02:18.333 --> 02:22.080
The next one is,
which nodes can the pods

02:22.410 --> 02:24.467
for the blue deployment be placed on?

02:25.380 --> 02:28.233
We've got to check
the taints on both the nodes.

02:28.933 --> 02:33.533
Let's do that.
Let's check the taints on each one.

02:33.720 --> 02:37.167
We're going to do a kubectl describe node.

02:38.250 --> 02:45.967
Let's check node01
and let's just check for taints.

02:46.410 --> 02:50.400
There are no taints set on node01
and the other node,

02:52.560 --> 02:54.660
which I believe is the control plane node.

02:55.740 --> 02:56.833
Let's check that too.

03:02.400 --> 03:05.300
There are no taints
on either of these nodes.

03:05.880 --> 03:10.133
For now, the pods can't be scheduled
on either of the nodes

03:10.300 --> 03:13.900
because there are no taints
on any of these nodes.

03:18.733 --> 03:22.273
In this question,
the task is to set node affinity

03:22.440 --> 03:26.700
to the deployment and to place
the pods on node01 only.

03:27.200 --> 03:29.733
We're going to edit the deployments.

03:29.900 --> 03:34.700
That's kubectl edit deployment blue.

03:36.867 --> 03:41.070
We're here and what we need to do
is on the pods specification,

03:41.700 --> 03:45.400
now we've got to set
the node affinity for this.

03:45.567 --> 03:46.533
We have to use

03:46.700 --> 03:50.133
the requiredDuringSchedulingIgnoredDuringExecution

03:50.300 --> 03:53.366
and then change the color
and the values to blue.

03:53.533 --> 03:56.533
For this, let's go
to Kubernetes documentation page

03:56.700 --> 03:59.467
and search for node affinity.

04:01.767 --> 04:08.967
Let's get this and here,
let's look for the affinity spec.

04:10.200 --> 04:11.533
We need to copy this

04:13.333 --> 04:18.667
and paste it here.

04:20.200 --> 04:24.800
Here basically this one is an example
for a pod and so all of these are aligned

04:24.967 --> 04:28.723
a bit more to the left but here,
this is under the pod,

04:28.890 --> 04:32.067
which is under the template section
of deployment.

04:32.820 --> 04:35.221
We have to move it
all a little bit to the right.

04:35.520 --> 04:37.566
For this, what I'm going to do here is,
the first one is okay,

04:37.733 --> 04:40.000
the first line is okay,
but all these remaining lines,

04:40.167 --> 04:44.033
we need to move it one step inside.

04:46.133 --> 04:49.950
Press capital V
and then select all the lines.

04:50.490 --> 04:55.302
Then I'm going to do a greater
than symbol, so that's a shift+dot

04:55.470 --> 04:59.233
on the keyboard that I'm using.
It moves everything to the right.

04:59.520 --> 05:04.467
It's not really pretty but I think
that will do the job for now.

05:05.133 --> 05:14.700
I'm just going to set the key
to color and the value to blue.

05:16.107 --> 05:19.123
There are ways
that we can set the VIM settings

05:19.290 --> 05:28.047
so that this always gets indented
with spaces, as opposed to multiple spaces

05:28.230 --> 05:30.600
at a time that you can see here.

05:30.767 --> 05:36.133
I'm going to save that
and let's give this a shot.

05:38.970 --> 05:40.020
Okay, so that's done.

05:41.130 --> 05:45.300
Now, the question is to find out
which nodes the pods are placed on.

05:45.467 --> 05:48.867
Let's run our kubectl get pods -o wide.

05:50.000 --> 05:54.067
That shows us
that all the pods are on node01.

05:57.541 --> 05:59.067
They're all on node01.

06:03.300 --> 06:07.153
The next task is to create
a new deployment named red

06:07.320 --> 06:09.883
with the nginx image
and two replicas and ensure it gets placed

06:10.050 --> 06:12.400
on the control plane node only.

06:12.567 --> 06:17.567
The recommendation is to use
the label node kubernetes.io/master,

06:18.090 --> 06:20.113
which is already set
on the control plane out.

06:20.280 --> 06:24.633
If you look at the control plane node,

06:29.400 --> 06:33.266
we see that there is this label, right?

06:33.433 --> 06:36.800
It doesn't have a value set so all we need
to do is

06:38.720 --> 06:41.767
to set a node affinity rule that says

06:42.816 --> 06:46.533
the pods that are part
of the deployment grid

06:46.700 --> 06:50.833
should be placed on a node
that has this label set.

06:51.180 --> 06:54.523
The label does not have
a value but if this label exists,

06:54.690 --> 06:58.167
then that's where it should be placed on.

06:58.470 --> 06:59.570
Let's try and do that.

07:00.120 --> 07:03.667
We'll use the kubectl
create deployment command.

07:05.637 --> 07:17.467
Then the name is red,
image is nginx, and replicas=2.

07:19.033 --> 07:21.463
If we're not going to create it
so we're going to do a dry run

07:21.630 --> 07:27.960
because we need to get the yaml file
to input the node affinity rules.

07:28.170 --> 07:33.329
We're going to do
a dry-run=client and then -o yaml.

07:33.496 --> 07:35.667
That's going to give us the yaml output.

07:35.850 --> 07:41.567
Then we're going to put it
to a file named red.yaml.

07:42.900 --> 07:44.600
Now we're going to edit that file.

07:47.550 --> 07:49.533
Within the spec section,

07:52.800 --> 07:55.733
we're going to do the same
as we did before.

07:55.900 --> 07:58.300
We're going to copy
this node affinity rule

08:01.300 --> 08:02.300
and paste it.

08:02.910 --> 08:07.410
Now, you've got to select
all of these lines,

08:07.980 --> 08:13.200
then do a shift+dot on my keyboard.

08:13.367 --> 08:19.320
That's basically the greater than symbol
and it's going to move to the right.

08:20.010 --> 08:25.866
Now we're going to change this
to the key is the label, which is this.

08:26.033 --> 08:27.467
I'm going to copy and paste.

08:31.230 --> 08:33.633
As we realize that this label
does not have value

08:33.800 --> 08:38.500
so there's no point in checking the value
here, so we're going to get rid of this.

08:38.667 --> 08:43.533
Now, all we're going to do
is see if the label exists.

08:43.700 --> 08:47.833
The operator
would be Exists with a capital E.

08:49.980 --> 08:51.167
Let's save that.

08:52.333 --> 08:56.567
Now we're going to create a deployment.

08:59.190 --> 09:01.800
There seems to be some error,
so let's go back

09:03.767 --> 09:05.310
and see what the error is.

09:05.520 --> 09:10.067
It's on line 26,
did not find expected key.

09:13.167 --> 09:15.200
Line 26 is this.

09:22.667 --> 09:25.233
Actually, it's this line here.

09:27.930 --> 09:29.778
This is not currently indented.

09:29.945 --> 09:31.033
That's the problem.

09:31.200 --> 09:36.367
We're going
to move it two characters before.

09:37.500 --> 09:39.233
Okay.
We're going to save that.

09:41.640 --> 09:42.690
Let's try that again.

09:44.550 --> 09:50.100
Okay, so even though here it said line 26,
the issue was a few lines above.

09:52.260 --> 09:54.700
Okay, so let's check our solution.

09:54.867 --> 09:56.467
All right, so that works.

09:56.760 --> 10:00.433
If we look at pods now,

10:02.200 --> 10:08.767
we'll see that the new pods
are on node control plane, okay?

10:10.133 --> 10:12.033
Okay, that's the end of this lab.

