1) Count labels on node01
List labels on a node with eitherkubectl describe or kubectl get --show-labels.
Example using --show-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:
beta.kubernetes.io/arch=amd64.
3) Apply a label color=blue to node01
Add a label to the node using kubectl label:
4) Create a deployment named blue (nginx) with 3 replicas
Create the deployment:
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:
spec.template.spec (maintain correct indentation relative to the existing manifest):
node01:
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:
red.yaml and add the nodeAffinity under spec.template.spec:
operator: Exists must be used without a values field.
Create the deployment:
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
Existsoperator.