How does the kube-scheduler decide which node a pod goes on?
Quick Answer
In two phases: filtering (predicates) removes nodes that can't run the pod — insufficient resources, unmatched nodeSelector/affinity, untolerated taints — then scoring ranks the survivors by fit, spread, and preferences, and the highest-scoring node wins.
Detailed Answer
The scheduler watches for pods with an empty nodeName. For each, phase one (filtering) discards any node that violates a hard constraint: not enough allocatable CPU/memory for the pod's requests, a nodeSelector/required affinity the node doesn't match, a taint the pod doesn't tolerate, unsatisfiable volume/topology requirements, or ports already in use. Phase two (scoring) ranks the remaining feasible nodes using plugins — spreading pods of the same Service across nodes/zones, honoring preferred affinity/anti-affinity, balancing resource utilization, and image locality (a node that already has the image scores higher). The pod is bound to the top-scoring node (ties broken randomly). It's extensible via the scheduling framework or a custom scheduler, and pod priority/preemption can evict lower-priority pods to make room for a higher-priority one. Key insight: requests drive filtering, so wrong requests wreck placement decisions.
Code Example
# Influence scoring: spread replicas across zones
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector: { matchLabels: { app: web } }Interview Tip
Two phases: filter (hard constraints) then score (soft preferences). Mention priority/preemption and topology spread. Note that resource *requests* drive filtering — a great segue to why accurate requests matter.