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.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Step 1. Identify Node Labels
Begin by inspecting the details of node “node01” to review its current labels. Run the following command:beta.kubernetes.io/arch (which is amd64).
Step 2. Apply a Label to node01
Next, add a new label calledcolor with the value blue to the node. Execute the following command:
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 namedblue using the nginx image and specify three replicas with the following command:
Step 4. Apply Node Affinity to the “blue” Deployment
To ensure that the pods in theblue 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:
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 namedred 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:
red.yaml file in an editor, and under the template.spec section, add the following node affinity block:
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:- Inspect node labels and verify their values.
- Apply a new label (
color=blue) to a node. - Create a deployment (
blue) and configure node affinity to restrict its pods to the node with the matching label. - Use a dry run to generate a deployment YAML for
redand apply a node affinity rule so that its pods are scheduled exclusively on the control plane node (by verifying the existence ofnode-role.kubernetes.io/master).