- Take me to Practice Test
Solutions to practice test - node affinity
-
How many Labels exist on node node01?
kubectl describe node node01
Look under
Labels
section--- OR ---
kubectl describe node node01 --show-labels
-
What is the value set to the label key beta.kubernetes.io/arch on node01?
From the output of Q1, find the answer there.
-
Apply a label color=blue to node node01
kubectl label node node01 color=blue
-
Create a new deployment named blue with the nginx image and 3 replicas.
kubectl create deployment blue --image=nginx --replicas=3
-
Which nodes can the pods for the blue deployment be placed on?
Check if master and node01 have any taints on them that will prevent the pods to be scheduled on them. If there are no taints, the pods can be scheduled on either node.
kubectl describe nodes controlplane | grep -i taints kubectl describe nodes node01 | grep -i taints
-
Set Node Affinity to the deployment to place the pods on node01 only.
Now we edit in place the deployment we created earlier. Remember that we labelled `node01` with `color=blue`? Now we are going to create an affinity to that label, which will "attract" the pods of the deployment to it.-
$ kubectl edit deployment blue
- Add the YAML below under the template.spec section, i.e. at the same level as
containers
as it is a POD setting. The affinity will be considered only during scheduling stage, however this edit will cause the deployment to roll out again.
affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: color operator: In values: - blue
-
-
Which nodes are the pods placed on now?
$ kubectl get pods -o wide
-
Create a new deployment named red with the nginx image and 2 replicas, and ensure it gets placed on the controlplane node only.
-
Create a YAML template for the deploymemt
kubectl create deployment red --image nginx --replicas 2 --dry-run=client -o yaml > red.yaml
-
Edit the file
vi red.yaml
-
Add the toleration using the label stated in the question, and placing it as before for the
blue
deployment
affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: node-role.kubernetes.io/master operator: Exists
- Save, exit and create the deployment
kubectl create -f red.yaml
- Check the result
$ kubectl get pods -o wide
-