0
0
Kubernetesdevops~5 mins

Node selectors for simple scheduling in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your app to run only on certain computers in your cluster. Node selectors help you pick those computers by matching labels you set on them.
When you want a pod to run only on nodes with special hardware like GPUs.
When you want to keep certain apps on nodes in a specific data center or zone.
When you want to separate workloads by node type, like fast SSD nodes versus regular nodes.
When you want to avoid running some pods on nodes reserved for system tasks.
When you want to test an app on a specific node before wider rollout.
Config File - pod.yaml
pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: nginx
  nodeSelector:
    disktype: ssd

This file creates a pod named example-pod running an nginx container.

The nodeSelector section tells Kubernetes to schedule this pod only on nodes labeled with disktype=ssd.

Commands
This command adds the label disktype=ssd to the node named node1. This label is used by the pod's node selector to find a matching node.
Terminal
kubectl label nodes node1 disktype=ssd
Expected OutputExpected
node/node1 labeled
This command creates the pod defined in pod.yaml. Kubernetes will try to schedule it on a node with the label disktype=ssd.
Terminal
kubectl apply -f pod.yaml
Expected OutputExpected
pod/example-pod created
This command shows the pods with extra details including the node they are running on. You can verify the pod is on the correct node.
Terminal
kubectl get pods -o wide
Expected OutputExpected
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES example-pod 1/1 Running 0 10s 10.244.1.5 node1 <none> <none>
-o wide - Shows detailed information including the node name
This command shows detailed information about the pod, including the node selector and the node where it is scheduled.
Terminal
kubectl describe pod example-pod
Expected OutputExpected
Name: example-pod Namespace: default Node: node1/192.168.1.10 Start Time: Thu, 01 Jan 2024 12:00:00 +0000 Labels: <none> Annotations: <none> Status: Running IP: 10.244.1.5 Containers: example-container: Image: nginx State: Running Ready: True Restart Count: 0 Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Node-Selectors: disktype=ssd
Key Concept

If you remember nothing else from this pattern, remember: node selectors let you tell Kubernetes to run pods only on nodes with matching labels.

Common Mistakes
Not labeling any node with the key-value used in nodeSelector.
Kubernetes cannot find a matching node, so the pod stays pending and never runs.
Always label at least one node with the exact key and value used in the pod's nodeSelector before creating the pod.
Using a different label key or value in the pod than what is on the node.
The pod will not match any node and remain unscheduled.
Double-check that the label key and value in the pod's nodeSelector exactly match the node's label.
Forgetting to apply the pod manifest after labeling nodes.
The pod won't be created or scheduled, so no workload runs.
Run 'kubectl apply -f pod.yaml' after labeling nodes to create and schedule the pod.
Summary
Label nodes with key-value pairs to identify their characteristics.
Use nodeSelector in pod specs to require pods run only on nodes with matching labels.
Apply the pod manifest and verify the pod runs on the correct node using kubectl commands.