-In this video, we will go over the practice test for node affinity.
The first question is to identify the labels on node node01.
To count the number of labels on node01, so let's do kubectl describe node node01.
Let's look at the labels.
We have one, two, three, four, five, five labels.
We're going to select five.
The next question is what is the value set to the label beta.kubernetes.io/arch
on node01? We're already on node01, and it is amd64.
That's this one.
The next question is to apply a label called color=blue to node node01.
For that, we're going to use the kubectl label command.
Let's take a look at the help and here we have some examples,
so you have kubectl label, the pods or node,
what is the object that we want to apply the label to, and then the node name
and then just the label.
It's pretty straightforward.
We're going to do kubectl label node node01
and then color=blue.
Let's verify that it's set correctly.
We're going to go back to labels and we see the color=blue.
The next one is to create a new deployment named blue
with the nginx image and three replicas.
Let us create the deployment,
the name is blue, the image is nginx, and replicas is three.
The next one is, which nodes can the pods
for the blue deployment be placed on?
We've got to check the taints on both the nodes.
Let's do that. Let's check the taints on each one.
We're going to do a kubectl describe node.
Let's check node01 and let's just check for taints.
There are no taints set on node01 and the other node,
which I believe is the control plane node.
Let's check that too.
There are no taints on either of these nodes.
For now, the pods can't be scheduled on either of the nodes
because there are no taints on any of these nodes.
In this question, the task is to set node affinity
to the deployment and to place the pods on node01 only.
We're going to edit the deployments.
That's kubectl edit deployment blue.
We're here and what we need to do is on the pods specification,
now we've got to set the node affinity for this.
We have to use
the requiredDuringSchedulingIgnoredDuringExecution
and then change the color and the values to blue.
For this, let's go to Kubernetes documentation page
and search for node affinity.
Let's get this and here, let's look for the affinity spec.
We need to copy this
and paste it here.
Here basically this one is an example for a pod and so all of these are aligned
a bit more to the left but here, this is under the pod,
which is under the template section of deployment.
We have to move it all a little bit to the right.
For this, what I'm going to do here is, the first one is okay,
the first line is okay, but all these remaining lines,
we need to move it one step inside.
Press capital V and then select all the lines.
Then I'm going to do a greater than symbol, so that's a shift+dot
on the keyboard that I'm using. It moves everything to the right.
It's not really pretty but I think that will do the job for now.
I'm just going to set the key to color and the value to blue.
There are ways that we can set the VIM settings
so that this always gets indented with spaces, as opposed to multiple spaces
at a time that you can see here.
I'm going to save that and let's give this a shot.
Okay, so that's done.
Now, the question is to find out which nodes the pods are placed on.
Let's run our kubectl get pods -o wide.
That shows us that all the pods are on node01.
They're all on node01.
The next task is to create a new deployment named red
with the nginx image and two replicas and ensure it gets placed
on the control plane node only.
The recommendation is to use the label node kubernetes.io/master,
which is already set on the control plane out.
If you look at the control plane node,
we see that there is this label, right?
It doesn't have a value set so all we need to do is
to set a node affinity rule that says
the pods that are part of the deployment grid
should be placed on a node that has this label set.
The label does not have a value but if this label exists,
then that's where it should be placed on.
Let's try and do that.
We'll use the kubectl create deployment command.
Then the name is red, image is nginx, and replicas=2.
If we're not going to create it so we're going to do a dry run
because we need to get the yaml file to input the node affinity rules.
We're going to do a dry-run=client and then -o yaml.
That's going to give us the yaml output.
Then we're going to put it to a file named red.yaml.
Now we're going to edit that file.
Within the spec section,
we're going to do the same as we did before.
We're going to copy this node affinity rule
and paste it.
Now, you've got to select all of these lines,
then do a shift+dot on my keyboard.
That's basically the greater than symbol and it's going to move to the right.
Now we're going to change this to the key is the label, which is this.
I'm going to copy and paste.
As we realize that this label does not have value
so there's no point in checking the value here, so we're going to get rid of this.
Now, all we're going to do is see if the label exists.
The operator would be Exists with a capital E.
Let's save that.
Now we're going to create a deployment.
There seems to be some error, so let's go back
and see what the error is.
It's on line 26, did not find expected key.
Line 26 is this.
Actually, it's this line here.
This is not currently indented.
That's the problem.
We're going to move it two characters before.
Okay. We're going to save that.
Let's try that again.
Okay, so even though here it said line 26, the issue was a few lines above.
Okay, so let's check our solution.
All right, so that works.
If we look at pods now,
we'll see that the new pods are on node control plane, okay?
Okay, that's the end of this lab.