Skip to content

KodeCloud CKAD Node Selectors and Node Affinity

Links: 111 KodeCloud Index


Node Selectors

  • Let us assume that we have 2 small nodes and 1 large node.
  • We would like to send the demanding pods to the larger node.
    • We can use node selectors for this.
    • attachments/Pasted image 20220924165323.jpg
  • Prior to using node selectors in pod definition file the nodes must be labelled.
  • Labelling a node: k label nodes <node-name> <label-key>=<label-value>

  • Sample Pod definition file with node selector:

    apiVersion:
    kind: Pod
    metadata:
        name: myapp-pod
    spec:
        containers:
        - name: data-processor
          image: data-processor
        nodeSelector:
          size: Large
    

  • Node selectors server the purpose but they have limitations

    • We cannot provide advanced expression like place the pod on a large or medium node Or place the pods on any nodes that are not small.
    • We cannot achieve this using node selectors.

Node Affinity

  • The primary use of Node affinity is to ensure that pods are hosted on particular nodes.
  • Sample pod definition file with Node Affinity
    apiVersion: v1
    kind: Pod
    metadata:
        name: myapp-pod
    spec:
        containers:
        - name: data-processor
          image: data-processor
        affinity:
          nodeAffinity:
            requiredDuringSchedulingIgnoredDuringExecution:
              nodeSelectorTerms:
              - matchExpressions:
                - key: size
                  operator: In
                  values:
                  - Large
                  - Medium
    
  • In the above example the pods will be placed on nodes with labels including large and medium value.
  • We can use the NotIn operator to not place on a node with a particular value.
  • There are lot of operators. Check the documentation to know more.
A combination of Taints & Tolerations and Node Affinity rules are used to place pods on the desired node.
  • If we just use Taints & Tolerations then we have a guarantee that the no other pods will be placed in the red tainted node but it is possible that red pod might be placed on some other node. attachments/Pasted image 20220924184513.jpg
  • If we just use Node Affinity then we can make sure that the pod gets placed in the required node but there is no stopping other pods from getting placed in that node. attachments/Pasted image 20220924184659.jpg

Different types of Node Affinity

  • The type of node affinity defines the behaviour of scheduler wrt to node affinity and stages in the lifecycle of the pod.
    • attachments/Pasted image 20220924182329.jpg
    • Planned means planned for some future release
  • attachments/Pasted image 20220924182835.jpg
    • requiredDuringSchedulingIgnoredDuringExecution means that if the scheduler cannot find the node for the pod then the node will not be scheduled.
    • requiredDuringSchedulingIgnoredDuringExecution means scheduler tries its best to get the right node for the pod but even if it is not found the pod will be scheduled.
    • DuringExecution - this means the pod has been running but a change has been made in the node affinity. Like an admin removed a label from the node.

Last updated: 2023-05-03