Difficulty: beginner
Estimated Time: 5-10 minutes

The Kubernetes scheduler automatically assigns pods to nodes, however there are times where you need more control over where the scheduler assigns pods. For example, you need to schedule a pod to a certain machine that has an SSD attached to it.

There are different ways to achive this, using: nodeSelector, Affinity and Anti-Affinity, which all use Labels and Selectors.

In this scenario, we'll be covering how to assign pods to nodes using nodeSelector. For further reading, check out Assigning Pods to Nodes.

Congratulations! You can start the next exercise in this series.

Assign pod to nodes

Step 1 of 3

Node Selector

nodeSelector is a selector which allows you to assign a pod to a specific node. It matches a node key/value pair, also known as a Label, which tells the Kubernetes scheduler which node to schedule the pod to.

If the label specified as nodeSelector doesn't exist on any node, then the pod will fail to be scheduled. If we still want to schedule our pod (even though the label doesn't exist on a node), we need to use Node/Pod affinity and Anti-affinity. We will cover and discuss this later in this scenario.

Note: By default, Kubernetes adds labels to nodes such as kubernetes.io/hostname, beta.kubernetes.io/arch and so on. For further information, read Built-in node labels in the Kubernetes documenation.

In the following section we will schedule the happypanda pod to the node01 node by using nodeSelector(disk=ssd).

Discover node labels

First of all, let's look at the current node labels:

kubectl get nodes --show-labels

Add a new node label

Now, add the label disk=ssd to the node01 node:

kubectl label nodes node01 disk=ssd

Ensure the label we've just created has been added to node01:

kubectl get nodes --show-labels

Assign the happypanda pod to node01, matching disk:ssd label

Check the file pod-nodeselector.yaml:

cat /manifests/pod-nodeselector.yaml

Notice that nodeSelector is matching the label we just added to the node - disk:ssd. The Kubernetes scheduler will use this label to try and schedule pods onto a node with the label.

Next, we will create the happypanda pod by running the following command:

kubectl apply -f /manifests/pod-nodeselector.yaml

Let's verify

Check that happypanda has been successfully scheduled on the node01 node:

kubectl get pods -o wide

Deleting happypanda pod and label

Delete happypanda pod:

kubectl delete -f /manifests/pod-nodeselector.yaml


kubectl delete pod happypanda

Remove label from node01:

kubectl label node node01 disk-