Taints, Tolerations and Affinity - Sandeep-K-Khandelwal/CKAD GitHub Wiki

Taints and Tolerations

  • Taints are applied on Node and Tolerations is applied on the PODs
  • Taints and tolerations work together to ensure that nodes accept only specific pods. However, this does not guarantee that PODs will always be placed on the tainted node
  • A toleration "matches" a taint if the keys are the same and the effects are the same.
  • Taint a node - kubectl taint nodes node1 key1=value1:effect. Effect could be NoScheule, PreferNoSchedule and NoExecute
  • Remove taint from a node - kubectl taint nodes node1 key1=value1:effect-
  • Tolerations for a Pod
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
  tolerations:
  - key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoSchedule"

Node Selector and Affinity

Node Selector and Affinity are used to make sure pods are places only on the desired nodes

Node Selector

  • Node Selector works based on the labels assigned on the POD and Node. Nodes are placed on the nodes with matching labels only
  • Add label to a node - kubectl label node <node_name> key=value
  • Example : kubectl label node node01 size=large
  • Add the node selector in the POD definition file
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
spec:
  containers:
    - name: nginx-container
      image: nginx
  nodeSelector:
    size: large
  • Node Selector doesn't work well with complex logic

Affinity

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
    - name: nginx-container
      image: nginx
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: Size
            operator: In
            values:
            - Large

Valid values as of now are -

  1. requiredDuringSchedulingIgnoredDuringExecution
  2. preferredDuringSchedulingIgnoredDuringExecution

Future values are - requiredDuringSchedulingRequiredDuringExecution

⚠️ **GitHub.com Fallback** ⚠️