Skip to main content
This lab walks through practical Kubernetes node affinity tasks: inspecting node labels, labeling a node, creating deployments, and constraining pods to specific nodes using nodeAffinity. The steps below show the commands, minimal YAML snippets, and verification commands you can run in a cluster.

1) Count labels on node01

List labels on a node with either kubectl describe or kubectl get --show-labels. Example using --show-labels:
Or view the full label list with:
Count the labels shown. (In the environment used here the node shows six labels.)

2) Check the value of beta.kubernetes.io/arch on node01

You can scan the --show-labels output for the label or inspect kubectl describe output:
In this cluster beta.kubernetes.io/arch=amd64.

3) Apply a label color=blue to node01

Add a label to the node using kubectl label:
Verify the label was applied:

4) Create a deployment named blue (nginx) with 3 replicas

Create the deployment:
Before adding affinity, check which nodes could schedule the pods. Look for taints (if no taints, pods may schedule to any node that meets resource/constraint requirements):
Because there are no taints, the scheduler could place the blue pods on either node by default.

5) Constrain blue deployment to node01 using nodeAffinity

Edit the blue Deployment and add a nodeAffinity rule under spec.template.spec.affinity.nodeAffinity so the scheduler only places pods on nodes with color=blue. Open the deployment for editing:
Inside the Deployment’s Pod template, add the following YAML under spec.template.spec (maintain correct indentation relative to the existing manifest):
A minimal view of the relevant part of the updated Deployment spec:
Save the edit and verify the pods are scheduled on node01:
All blue pods are now placed on node01 as required.
When using operator: In you must provide one or more values. The node must have the exact label key/value pair to match.

6) Create a deployment named red (nginx) with 2 replicas and ensure it runs on the control plane node

The control plane node has the label node-role.kubernetes.io/master (no value). To match nodes that have the label regardless of its value, use the Exists operator. Recommended workflow: generate the Deployment YAML with a dry-run, edit it to add the affinity block, then create it. Generate YAML:
Edit red.yaml and add the nodeAffinity under spec.template.spec:
Important: operator: Exists must be used without a values field. Create the deployment:
Verify the red pods are scheduled on the control plane node:
YAML indentation is significant. If you see errors like did not find expected key when creating from a file, check the spacing/indentation for the added affinity block relative to spec.template.spec.

Quick reference: common commands and affinity operators


You have now completed the lab tasks:
  • inspected node labels,
  • applied a node label,
  • created a deployment and constrained it to a specific node using nodeAffinity with In,
  • and created another deployment constrained to nodes that have a label using the Exists operator.

Watch Video