This page is under regular updates. Please check back later for more content.
Node Selectors

Node Selector based label

Overview

  • One can use for selecting labels is to constrain the set of nodes onto whihc a pod can be schedule to only be able to run on paticular nodes.
  • Generally such commonstrates are unnecessary as the scheduler will automatically do a reasonable placement, but on certain circumstances we might need it
  • We can use labels to tag nodes
  • The nodes are tagged, you can use the label selectors to specify the pods run only on specific nodes.
  • First we give label to the node.
  • When use node selector to the port configuration

Example of Node Selector

k8s_example_6.yml
kind: Pod
apiVersion: v1
metadata:
  name: example-6
  labels:
    env: development
spec:
    containers:
      - name: c00
        image: ubuntu
        command: ["/bin/bash", "-c", "while true; do echo Hello-world; sleep 5 ; done"]
    nodeSelector:                                         
       hardware: t2-medium

Pod will be created on the node that contain the specified label/node selector

Create a pod

kubectl apply -f k8s_example_6.yml
Output
controlplane $ kubectl apply -f k8s_example_6.yml 
pod/example-6 created

Verify the nodes and pods

kubectl get nodes
kubectl get pods
Output
controlplane $ kubectl get nodes
NAME           STATUS   ROLES           AGE   VERSION
controlplane   Ready    control-plane   8d    v1.31.0
node01         Ready    <none>          8d    v1.31.0
controlplane $ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
example-6   0/1     Pending   0          18s

If you notice the pod is still in the pending state that is because it is meant to be run on the node that contain the node selector that is hardware=t2-medium

For more details

kubectl describe pods example-6
Output
controlplane $ kubectl describe pods nodelabels
.
.
.
Node-Selectors:              hardware=t2-medium
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason            Age    From               Message
  ----     ------            ----   ----               -------
  Warning  FailedScheduling  9m18s  default-scheduler  0/2 nodes are available: 2 node(s) didn't match Pod's node affinity/selector. preemption: 0/2 nodes are available: 2 Preemption is not helpful for scheduling.

Assign a node selector to a worker node

kubectl label nodes node01 hardware=t2-medium
Output
controlplane $ kubectl label nodes node01 hardware=t2-medium
node/node01 labeled

Verify if the pod has started

kubectl get pods
Output
controlplane $ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
nodelabels   1/1     Running   0          12m
Output
controlplane $ kubectl describe pods nodelabels
.
.
.
Node-Selectors:              hardware=t2-medium
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason            Age                    From               Message
  ----     ------            ----                   ----               -------
  Warning  FailedScheduling  13m                    default-scheduler  0/2 nodes are available: 2 node(s) didn't match Pod's node affinity/selector. preemption: 0/2 nodes are available: 2 Preemption is not helpful for scheduling.
  Warning  FailedScheduling  2m38s (x2 over 7m38s)  default-scheduler  0/2 nodes are available: 2 node(s) didn't match Pod's node affinity/selector. preemption: 0/2 nodes are available: 2 Preemption is not helpful for scheduling.
  Normal   Scheduled         15s                    default-scheduler  Successfully assigned default/nodelabels to node01
  Normal   Pulling           15s                    kubelet            Pulling image "ubuntu"
  Normal   Pulled            11s                    kubelet            Successfully pulled image "ubuntu" in 3.78s (3.781s including waiting). Image size: 29754514 bytes.
  Normal   Created           11s                    kubelet            Created container c00
  Normal   Started           11s                    kubelet            Started container c00