Node affinity & selectors
Attracting pods to the right nodes.
In plain terms
If a taint says “keep off,” affinity says “come sit here.” It’s a magnet that pulls the right pods toward nodes with the right label — say, the ones with fast SSDs.
If taints repel, node affinity attracts. nodeSelector is the simple form — run only on nodes carrying a label. nodeAffinity is the expressive form: hard rules that filter and soft, weighted rules that merely nudge, both with set-based operators.
pod-spec.yaml
spec:nodeSelector: { disktype: ssd } # simple: a hard requirementaffinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution:- weight: 100preference:matchExpressions:- { key: zone, operator: In, values: [us-east-1a] }
requiredDuringScheduling is a filter — no matching node and the pod stays Pending. preferredDuringScheduling is a score — it biases placement but never blocks it. Pod affinity and anti-affinity extend the same idea to place pods relative to other pods: co-locate a cache with its app, or spread replicas across zones.
Affinity and taints are a pair
Affinity sends the right pods to special nodes; a matching taint keeps everyone else off. For true isolation of GPU or tenant nodes you almost always need both.