Home

node selector

//to label a node
kubectl lable nodes <node-name> <lable-key>=<label-value>
kubectl lable nodes node-1 size=Large

Setting node selector to the pod so that it gets deployed to node1

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

Now myapp-pod will be placed at node1.

Note: NodeSelector does not work when boolean logic is required. Ex. Large OR Medium, Not Small For this Node Affinity and Anti-affinity are used.

apiVersion: v1
kind: Pod
metadata:
 name: myapp-pod
spec:
 containers:
 - name: data-processor
   image: data-processor
 affinity:
  nodeAffinity:
   requiredDuringSchedulingIgnoredDuringExecution:
    nodeSelectorTerms:
    - matchExpressions:
      - key: soze
        operator: In //NotIn//Exists(need not require values)
        values:
        - Large

Types of nodeAffinty:

Available: requiredDuringSchedlingIgnoredDuringExecution preferredDuringSchedlingIgnoredDuringExecution

DuringScheduling: Pod does not exist and is created for the first time

DuringExecution: Pod already is running na da change is made in the environment that affects node affinity

Type DuringScheduling DuringExecution Type1 Required Ignored Type2 Preferred Ignored

Type1: During Scheduling If the Required Type is selected, scheduler will mandate the pod be placed on node with given affinity rule. If it cannot find one, the pod will not be scheduled. When placement of pod is crucial.

During Execution When pod has been running and a change is made that affects the node affinity. Changes will be ignored and pods will not be impacted

Type2: Preferred when placement is not crucial and workload is more important, the pod is placed on any node if node with the desired affinity is not found.

During Execution When pod has been running and a change is made that affects the node affinity. Changes will be ignored and pods will not be impacted

Planned: requiredDuringSchedlingRequiredDuringExecution preferredDuringSchedlingRequiredDuringExecution

Type DuringScheduling DuringExecution Type3 Required Required Type4 Preferred Required

Type3,4: Evict any pod that does not meet affinity rules during the execution