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

or

kubectl delete pod happypanda

Remove label from node01:

kubectl label node node01 disk-

Creating Katacoda Scenarios

Thanks for creating Katacoda scenarios. This tab is designed to help you as an author have quick access the information you need when creating scenarios.

This tab will not be visible to users.

Here are some useful links to get you started.

Running Katacoda Workshops

If you are planning to use Katacoda for workshops, please contact [email protected] to arrange capacity.

Debugging Scenarios

Below is the response from any background scripts run or files uploaded. This stream can aid debugging scenarios.

If you still need assistance, please contact [email protected]