QA
Intermediate LevelDocker & Deployment

What is the difference between Node Affinity and Taints/Tolerations?

❓ Interview Prompt: Compare Node Affinity (pod attraction), Taints (node repulsion), and Tolerations (pod exception) for advanced Kubernetes scheduling control.

Kubernetes scheduling has three mechanisms for controlling where Pods run:

1. Node Selector (Simple):

nodeSelector: diskType: ssd — schedule Pod only on nodes with that label. Simple but limited.

2. Node Affinity (Flexible Attraction):

Node Affinity is an expressive way to attract Pods to specific nodes based on node labels. Two types:

•
`requiredDuringSchedulingIgnoredDuringExecution`: Hard requirement — pod won't be scheduled if no matching node exists.
•
`preferredDuringSchedulingIgnoredDuringExecution`: Soft preference — scheduler tries to find matching node but will place elsewhere if needed.

3. Taints (Node Repulsion — Node-Side):

A Taint is applied to a node to repel most Pods. Format: key=value:Effect.

•
NoSchedule: New pods without toleration won't be scheduled here.
•
PreferNoSchedule: Soft version — prefer not to schedule here.
•
NoExecute: Evicts existing pods too.

4. Tolerations (Pod Exception — Pod-Side):

A Toleration on a Pod allows it to be scheduled on tainted nodes. Tolerations don't attract pods — they merely permit.

Common Pattern: Dedicated GPU Nodes:

Taint GPU nodes: kubectl taint node gpu-node-1 gpu=true:NoSchedule. Only GPU workload pods with matching tolerations will be scheduled there.

Code Implementation Example

yaml
# Taint a GPU node (repels all pods without toleration)
# kubectl taint nodes gpu-node-1 gpu=true:NoSchedule

spec:
  # Toleration: allow this pod to schedule on tainted GPU nodes
  tolerations:
    - key: "gpu"
      operator: "Equal"
      value: "true"
      effect: "NoSchedule"
  
  # Node Affinity: REQUIRE a node with GPU label (hard requirement)
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: accelerator
                operator: In
                values:
                  - nvidia-a100     # must have this GPU type
      
      # Prefer us-east-1a zone (soft preference)
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 100
          preference:
            matchExpressions:
              - key: topology.kubernetes.io/zone
                operator: In
                values:
                  - us-east-1a
💡Toleration allows GPU node placement; Node Affinity requires nvidia-a100 label + prefers us-east-1a zone.
🎯 Interview Key Takeaway

Taints repel pods from nodes (node-side); Tolerations create exceptions (pod-side). Node Affinity attracts pods to nodes. Use together for dedicated workload isolation (GPU, spot, high-memory nodes).

#Kubernetes#Node Affinity#Taints#Tolerations#Scheduling#Intermediate
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?