Skip to main content
In this lesson, we demonstrate a practical test for node affinity configuration in Kubernetes. You will learn how to identify node labels, assign a new label to a node, and configure deployments with node affinity rules that restrict pod scheduling to specific nodes. ──────────────────────────────

Step 1: Identify the Labels on node01

Begin by examining the labels on node01. Run the following command to obtain detailed node information:
From the output, note the label beta.kubernetes.io/arch with the value amd64. ──────────────────────────────

Step 2: Apply a New Label on node01

Next, assign a new label color=blue to node01. Execute the following command:
Verify that the label has been applied by inspecting the node details:
Labeling nodes strategically can help in managing pod placement and workload isolation across your Kubernetes cluster.
──────────────────────────────

Step 3: Create the “blue” Deployment with Node Affinity for node01

Create a deployment named blue utilizing the nginx image with three replicas:
Before adding node affinity, confirm that no taints are present that could interfere with pod scheduling. Check for taints on node01:
Also, inspect the node status:
Since there are no taints on the nodes, pods can be scheduled on any node by default. Now, update the blue deployment to enforce node affinity. Edit the deployment’s pod specification to include a node affinity rule that restricts pods to nodes labeled with color=blue. Integrate the following YAML snippet under the spec.template.spec section:
Save your changes. To verify that the pods are scheduled on node01, execute:
All blue deployment pods are now correctly placed on node01 as dictated by the node affinity rule. ──────────────────────────────

Step 4: Create the “red” Deployment with Node Affinity for the Control Plane

In the next step, we create a deployment named red using the nginx image with two replicas. The deployment is configured to run its pods exclusively on the control plane node by leveraging the node-role.kubernetes.io/master label. First, generate the deployment YAML file using a dry run:
Edit the generated red.yaml file to add the node affinity rule under the spec.template.spec section. Update the file to resemble the following:
Save the changes and create the deployment by running:
Verify that the pods of the red deployment are scheduled on the control plane by checking their node assignments:
Using a dry run to generate deployment YAML files allows you to safely modify pod specifications—such as adding node affinity—before applying the changes to your cluster.
──────────────────────────────

Additional Diagram Reference

The image below illustrates the process for creating the red deployment with the nginx image, two replicas, and node affinity targeting the control plane node by checking for the label node-role.kubernetes.io/master:
The image shows a task to create a Kubernetes deployment named "red" using the nginx image with 2 replicas, placed on the controlplane node.
──────────────────────────────

Conclusion

In this lesson, you learned how to:
  • Identify node labels with the kubectl describe node command.
  • Apply custom labels to nodes using the kubectl label command.
  • Configure node affinity in a deployment to restrict pod scheduling based on node labels.
  • Generate and modify deployment YAML files using a dry run to enforce node affinity settings for both worker nodes and control plane nodes.
This completes the lab on node affinity configuration in Kubernetes. For further reading and more advanced scenarios, you may refer to Kubernetes Documentation.

Watch Video