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: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:
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:
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):
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:
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:
red pods are scheduled on the control plane node:
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.requiredDuringSchedulingIgnoredDuringExecutionto enforce hard placement rules. - Use
operator: Existswhen you only care about the presence of a label key (no value). - Always double-check YAML indentation when editing manifests.