Kubernetes Pod Node Selector and Tolerations

Learn how to use Kubernetes Node Selector and Tolerations to control pod scheduling. This guide provides a practical example and explains how to assign pods to specific nodes based on requirements.

Kubernetes Pod Node Selector and Tolerations

This example demonstrates how to use nodeSelector and tolerations to control where a Pod is scheduled in Kubernetes.

Node Selector

The nodeSelector field allows you to specify labels that the node must have to be considered for scheduling.

Tolerations

tolerations allow Pods to be scheduled onto nodes with taints that would normally prevent them from being scheduled.

Example

# https://medium.com/kubernetes-tutorials/learn-how-to-assign-pods-to-nodes-in-kubernetes-using-nodeselector-and-affinity-features-e62c437f3cf8
apiVersion: v1
kind: Pod
metadata:
  name: debug-pod
  namespace: default
spec:
  containers:
  - name: debug
    image: alpine
    imagePullPolicy: IfNotPresent
    command: ["sleep"]
    args: ["100000"]
  nodeSelector:
    node: cpu
  tolerations:
  - key: application
    operator: Equal
    value: myapp
    effect: NoSchedule

This Pod will only be scheduled on nodes with the label node: cpu and tolerates nodes with a taint of application=myapp:NoSchedule.

Further Reading