Skip to main content
This lab walks through practical tasks for using node affinity to control pod placement in Kubernetes. You’ll inspect node labels, apply labels, create Deployments, and add node affinity rules so pods schedule only on specific nodes. Key topics: Kubernetes node labels, kubectl label, node affinity (requiredDuringSchedulingIgnoredDuringExecution), and common YAML indentation pitfalls. References:

1) Inspect labels on node01

Describe the node and look at the Labels section:
Answer: node01 has 5 labels. The value of the label beta.kubernetes.io/arch on node01 is amd64. You can also view the label set in a concise table:

2) Add the label color=blue to node01

Use kubectl label to attach the new label:
Verify it appears in the node description:
Now node01 includes the color=blue label which we will use to constrain pod scheduling.

3) Create a Deployment blue (nginx, 3 replicas)

Create the Deployment with three nginx replicas:
Check node taints to confirm scheduling is not restricted by taints:
With no node taints, pods are eligible to schedule on any Ready node that meets the Pod’s requirements. At this point the blue pods may land on either node.

4) Add node affinity to the blue Deployment so pods run only on node01

We want the blue pods to schedule only on nodes labeled color=blue. To enforce this, add a nodeAffinity rule under spec.template.spec.affinity using requiredDuringSchedulingIgnoredDuringExecution (a hard requirement). Example fragment to add to the Deployment (under spec.template.spec):
A full example for the Deployment spec (context for placement):
Apply the change by editing the Deployment:
Verify the pods are scheduled on node01:
All blue pods are now constrained to node01 because it has color=blue.
Node affinity using requiredDuringSchedulingIgnoredDuringExecution enforces a hard scheduling requirement. If no matching node exists, affected Pods remain in Pending state until a matching node is available.

5) Create a Deployment red (nginx, 2 replicas) that schedules only on the control plane node

The control plane node has the label key node-role.kubernetes.io/master (with no value). Use an affinity rule with operator: Exists to require that label key be present on the node, regardless of value. Generate a YAML stub and edit it:
Edit red.yaml and add the affinity.nodeAffinity block under spec.template.spec. Important: use operator: Exists to match the presence of the label key. Example red.yaml fragment:
Apply the manifest:
Verify the red pods are scheduled on the control plane node:
YAML indentation is crucial. If you see errors like did not find expected key, check that the affinity and nested blocks align properly under template.spec.
When modifying YAML produced by --dry-run -o yaml, be careful to preserve consistent indentation. Mis-indented affinity/nodeAffinity blocks are the most common cause of parse errors.

Quick reference — Commands used


Summary
  • Use node labels to mark nodes (e.g., color=blue).
  • Use nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution to enforce hard placement rules.
  • Use operator: Exists when you only care about the presence of a label key (no value).
  • Always double-check YAML indentation when editing manifests.
Good job — you now know how to pin pods to specific nodes with Kubernetes node affinity.

Watch Video