Skip to main content

Workloads & Scheduling

How to Schedule a Kubernetes Pod Using nodeSelector

The DevOpscube team runs a Kubernetes cluster with different types of worker nodes. A machine-learning workload must be scheduled only on nodes designated for ML applications.

The node node01 is designated as an ML node and needs to be identified using a custom Kubernetes label.

Your task is to label node01 so that it is identified as an ML application node.

Create a Pod named ml-app using the nginx image and configure it so that it can be scheduled only on nodes labeled for ML workloads.

After deploying the Pod, verify that:

  • node01 has the required label.
  • The ml-app Pod is in the Running state.
  • The Pod is scheduled on a node with the required label.
  • The Pod is not scheduled onto nodes that do not have the required label.

Label the Node

Add a custom label to node01 to identify it as a node intended for ML workloads:

$ kubectl label node node01 type=ml-app

node/node01 labeled

You can verify that the label was added successfully:

$ kubectl get node node01 --show-labels

NAME     STATUS   ROLES    AGE   VERSION   LABELS
node01   Ready    worker   9d    v1.37.0   beta.kubernetes.io/arch=arm64,beta.kubernetes.io/os=linux,kubernetes.io/arch=arm64,kubernetes.io/hostname=node01,kubernetes.io/os=linux,node-role.kubernetes.io/worker=worker,type=ml-app

You should see type=ml-app in the list of labels.

Create the Pod Manifest

Generate the Pod manifest without creating the Pod:

kubectl run ml-app \
  --image=nginx \
  --dry-run=client -o yaml > ml-app.yaml

Edit ml-app.yaml and add the Kubernetes nodeSelector under spec:

ml-app.yaml
apiVersion: v1 kind: Pod metadata: name: ml-app labels: run: ml-app spec: containers: - name: ml-app image: nginx nodeSelector: type: ml-app

The Kubernetes nodeSelector ensures that the Pod is scheduled only on a node with the type=ml-app label.

Apply the Pod

Apply the manifest to create the Pod:

$ kubectl apply -f ml-app.yaml

pod/ml-app created

Verify the Pod

Verify the Pod status and confirm that it is scheduled on node01:

$ kubectl get pod ml-app -o wide

NAME     READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
ml-app   1/1     Running   0          36s   10.244.196.180   node01   <none>           <none>

You can also verify the configured Kubernetes nodeSelector:

kubectl get pod ml-app -o jsonpath='{.spec.nodeSelector}{"\n"}'

Expected:

{"type":"ml-app"}

This confirms that the Pod uses the type=ml-app selector and is scheduled on the matching node.

CKA Exam Tips

1. Remember Kubernetes nodeSelector Syntax

The nodeSelector key and value must match the label assigned to the node.

nodeSelector:
  type: ml-app

2. Label a Node Quickly

You can add the required label with:

kubectl label node node01 type=ml-app

3. Kubernetes nodeSelector Is a Scheduling Requirement

Unlike a toleration, nodeSelector restricts the Pod to nodes with the specified label. If no matching node is available, the Pod remains in the Pending state.