Skip to main content
In this lesson, we will guide you through a practical lab exercise focused on implementing node affinity in Kubernetes. By following these steps, you will learn how to inspect node labels, add custom labels, and apply node affinity rules to your deployments for controlled pod scheduling.

Step 1. Identify Node Labels

Begin by inspecting the details of node “node01” to review its current labels. Run the following command:
In the output, you will see a list of labels similar to these:
Make sure to verify the label count and note the value of the label beta.kubernetes.io/arch (which is amd64).

Step 2. Apply a Label to node01

Next, add a new label called color with the value blue to the node. Execute the following command:
After applying the label, confirm the update by running:
You should now see an additional label in the output:
Labeling nodes correctly is essential for using node affinity effectively in your deployments.

Step 3. Create Deployment “blue” with Three Replicas

Now, create a deployment named blue using the nginx image and specify three replicas with the following command:
The command output should confirm:
Next, check the node assignments of the pods with:
At this stage, the pods may be scheduled on any available node because no node affinity rules have been applied yet.

Step 4. Apply Node Affinity to the “blue” Deployment

To ensure that the pods in the blue deployment only run on node01 (which now has the label color=blue), update the deployment with a node affinity rule. Edit the deployment YAML and incorporate the following affinity configuration under the template.spec section:
After saving the changes, the scheduler will enforce that all pods for the blue deployment are placed on node01. Verify the updated placement with:

Step 5. Create Deployment “red” with Two Replicas on the Control Plane

Now, create a deployment named red that targets the control plane node. The control plane is typically identified by the label node-role.kubernetes.io/master (which exists without a value). Start by generating the deployment YAML using a dry run:
Open the red.yaml file in an editor, and under the template.spec section, add the following node affinity block:
Save the file and then create the deployment with:
Confirm that the pods are scheduled on the control plane node by checking:
If there are any syntax or indentation issues in red.yaml, the deployment creation will fail. Make sure the YAML is correctly formatted.
Incorrect YAML formatting or indentation may lead to failure in deploying pods. Double-check your YAML syntax if you encounter any issues.

Summary

In this exercise, you learned how to:
  1. Inspect node labels and verify their values.
  2. Apply a new label (color=blue) to a node.
  3. Create a deployment (blue) and configure node affinity to restrict its pods to the node with the matching label.
  4. Use a dry run to generate a deployment YAML for red and apply a node affinity rule so that its pods are scheduled exclusively on the control plane node (by verifying the existence of node-role.kubernetes.io/master).
By following these steps, you can ensure optimal pod placement and efficient use of your Kubernetes cluster resources. Happy deploying! For more details, check out the following resources:

Watch Video