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. Although Affinity and Anti-Affinity can be used, these are both in beta and thus, we will just mention them. 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 which is still in beta. 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.

Schedule happypanda pod to node01 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 node01:

kubectl label nodes node01 disk=ssd

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

kubectl get nodes --show-labels

Assign happypanda pod to node01, matching disk:ssd label

View 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.

Create happypanda pod:

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

Check that happypanda has been successfully scheduled on node01:

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-

Terminal Host 2