Try Sailor Cloud - 25% off!

Claim Now
Back to all posts

Kubernetes Scheduling: Taints, Tolerations, and Node Affinity

Kubernetes Scheduling: Taints, Tolerations, and Node Affinity

Control where pods run in your cluster using scheduling constraints.

Node Selectors

Simple way to constrain pods to nodes with specific labels:

spec:
  nodeSelector:
    disktype: ssd
    zone: us-west-1a
# Label a node
kubectl label node worker-1 disktype=ssd

Taints and Tolerations

Taints are applied to nodes to repel pods. Tolerations are applied to pods to allow scheduling on tainted nodes.

Taint Effects

EffectDescription
NoScheduleDon’t schedule new pods
PreferNoScheduleTry to avoid scheduling
NoExecuteEvict existing + don’t schedule

Applying Taints

# Add taint
kubectl taint nodes worker-1 key=value:NoSchedule

# Remove taint
kubectl taint nodes worker-1 key=value:NoSchedule-

Adding Tolerations

spec:
  tolerations:
  - key: "key"
    operator: "Equal"
    value: "value"
    effect: "NoSchedule"

Node Affinity

More expressive than nodeSelector:

Required (Hard) Affinity

spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: zone
            operator: In
            values:
            - us-west-1a
            - us-west-1b

Preferred (Soft) Affinity

spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        preference:
          matchExpressions:
          - key: disktype
            operator: In
            values:
            - ssd

Pod Affinity/Anti-Affinity

Schedule pods relative to other pods:

Pod Affinity

spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchLabels:
            app: cache
        topologyKey: kubernetes.io/hostname

Pod Anti-Affinity

spec:
  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          labelSelector:
            matchLabels:
              app: web
          topologyKey: kubernetes.io/hostname

Common Scenarios

  1. GPU nodes: Taint GPU nodes, tolerate for ML workloads
  2. Zone spreading: Anti-affinity for HA
  3. Dedicated nodes: Taint for specific teams
  4. SSD preference: Node affinity for database pods

Practice Scheduling

Scheduling questions appear in CKA exams. Practice at Sailor.sh.

Start Free Practice