In Kubernetes, Node Selector, Node Affinity, Taints, and Tolerations are mechanisms that help you control which nodes your pods can be scheduled on. Here’s a detailed overview of each:

1. Node Selector

Node Selector is the simplest way to constrain pods to nodes with specific labels.

Example

To use a node selector, you can add the following to your pod specification:

CopyapiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
  nodeSelector:
    disktype: ssd

In this example, the pod will only be scheduled on nodes that have the label disktype=ssd.

2. Node Affinity

Node Affinity is a more flexible way to specify rules about which nodes your pods can be scheduled on. It allows you to define rules based on node labels using expressions.

Example

Here’s how to use node affinity in a pod specification:

CopyapiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
          - matchExpressions:
              - key: disktype
                operator: In
                values:
                  - ssd
                  - hdd

In this example, the pod will be scheduled on nodes that have the label disktype with values ssd or hdd.

Types of Node Affinity

  • requiredDuringSchedulingIgnoredDuringExecution: The rules must be met for scheduling.
  • preferredDuringSchedulingIgnoredDuringExecution: The rules are preferred but not mandatory.

3. Taints

Taints are applied to nodes and allow a node to repel certain pods. Taints consist of a key, value, and effect.

Example

To apply a taint to a node, use the following command:

Copykubectl taint nodes <node-name> key=value:NoSchedule

This command will prevent any pods that do not tolerate the taint from being scheduled on the specified node.

Taint Effects

  • NoSchedule: Pods that do not tolerate the taint will not be scheduled on the node.
  • PreferNoSchedule: Kubernetes will try to avoid scheduling pods that do not tolerate the taint, but it’s not guaranteed.
  • NoExecute: Existing pods that do not tolerate the taint will be evicted from the node.

4. Tolerations

Tolerations are applied to pods and allow them to be scheduled on nodes with matching taints.

Example

To add a toleration to a pod, include the following in your pod specification:

CopyapiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
  tolerations:
    - key: key
      operator: Equal
      value: value
      effect: NoSchedule

In this example, the pod can be scheduled on nodes with the taint key=value:NoSchedule.

Summary

  • Node Selector: Simple way to select nodes based on labels.
  • Node Affinity: More flexible and expressive than node selectors, allowing for complex scheduling rules.
  • Taints: Prevent pods from being scheduled on nodes unless they tolerate the taint.
  • Tolerations: Allow pods to be scheduled on nodes with specific taints.