Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsHard
A cluster operator wants to ensure that a specific Pod is always scheduled on a node that has a GPU, which is identified by a custom label `gpu=true`. Which section of the Pod's YAML manifest should be configured to enforce this placement constraint?
- A`metadata.labels`
- B`spec.selector`
- C`spec.tolerations`
- D`spec.affinity.nodeAffinity`
Show answer & explanationAnswer & explanation
Correct answer: D. `spec.affinity.nodeAffinity`
`nodeAffinity` is a powerful scheduling constraint that allows you to instruct the scheduler to prefer or require Pods to be placed on nodes with specific labels. This is ideal for ensuring Pods land on nodes with specialized hardware like GPUs.
Why the other options are wrong
- A. `metadata.labels` are labels applied to the Pod itself, not a mechanism to select a node for the Pod.
- B. `selector` is typically used by controllers (like Deployments) to select Pods they manage, or by Services to select backend Pods, not for Pod-to-Node placement.
- C. `tolerations` allows Pods to be scheduled on nodes that have matching taints, rather than forcing them to a specific node type.
Kubernetes Node Affinity
A set of rules used by the Kubernetes scheduler to determine which nodes a Pod can be scheduled on, based on node labels.
- Can be 'required' (must match) or 'preferred' (try to match).
- Uses `nodeSelectorTerms` and `matchExpressions` to specify label requirements.
- Complements `nodeSelector` for more flexible scheduling.
Memory trick: Node Affinity 'attracts' Pods to specific node types, like a magnet.