Difficulty: beginner
Estimated Time: 3 minutes

You can manually assign pods to a specific node cluster, it is not necessary as the scheduler is doing that automatically for you but it might be some circumstances where you may want more control on this, e.g. to ensure that a pod ends up on a machine with an SSD attached to it.

There're different ways to achive that, nodeSelector, Affinity and Anti-Affinity, and they all use Labels and Selectors.

This scenario provides an overall view of how to assign pods to nodes by using nodeSelector. The Affinity and Anti-Affinity features are still in beta mode thus we will just mention them.

For further info read 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 to assign a pod to a specific node. It is matching a node key/value pair ,also known as Label, telling the scheduler what is the node to fit the pod.

If the label specified as nodeSelector doesn't exist in any node, the pod won't be scheduled. If we still want to schedule our pod even though the label doesn't exist in a node, we need to use Node/Pod affinity and anti-affinity which is in beta. We will dicuss about it later on.

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

Schedule happypanda pod to node01 by using nodeSelector(disk=ssd)

Discover node labels

First of all, we have a look at the current node labels:

kubectl get nodes --show-labels

Add a new node label

Adding a disk=ssd label to node01:

kubectl label nodes node01 disk=ssd

Check the disk=ssd label that we've just added is on node01:

kubectl get nodes --show-labels

Assign happypanda pod to node01, matching disk:ssd label

Look at the file pod-nodeselector.yaml:

cat /manifests/pod-nodeselector.yaml

Note nodeSelector is matching disk:ssd label and then the scheduler will fit this pod on any node with this label.

Create happy panda pod:

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

Check that happy panda has been successfully scheduled on node01:

kubectl get pods -o wide

Deleting happy panda pod and label

Delete happy panda pod:

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


kubectl delete pod happypanda

Remove label from node01:

kubectl label node node01 disk-

Terminal Host 2