How to schedule Pods on specific nodes?

· Category: Kubernetes

Short answer

Use nodeSelector for simple matching, affinity for complex rules, and taints with tolerations to repel or attract Pods to specific nodes.

Steps

  1. Label nodes: kubectl label nodes node1 disk=ssd.
  2. Use nodeSelector or affinity in the Pod spec.
  3. Use taints on nodes and tolerations on Pods for dedicated nodes.

Example

nodeSelector:
  disk: ssd

Affinity:

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: disk
          operator: In
          values:
          - ssd

Tips

  • Use preferredDuringScheduling for soft constraints.
  • Taints are useful for dedicated nodes like GPU or control plane nodes.
  • The scheduler also considers resource requests and pod affinity.

Common issues

  • Incorrect labels cause Pods to remain Pending.
  • Too many hard constraints can lead to unschedulable Pods.
  • Changing labels does not evict existing Pods.