How to use node selector to assign virtual machines to a node

In some circumstances, we want to control which node the virtual machine or pod deploys to. The node selector can be used to assign virtual machine or pod to a node.

Add label to a node

The label can be added to a node from either command line or openshift web console.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ oc get nodes
NAME STATUS ROLES AGE VERSION
master1 Ready master 46h v1.20.11+63f841a
master2 Ready master 46h v1.20.11+63f841a
master3 Ready master 46h v1.20.11+63f841a
worker1 Ready worker 45h v1.20.11+63f841a
worker2 Ready worker 45h v1.20.11+63f841a
worker3 Ready,SchedulingDisabled worker 45h v1.20.11+63f841a

$ oc label nodes worker1 worker-node-name=w1

$ oc describe node worker1 | grep worker-node-name
worker-node-name=w1

$ oc get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
worker1 Ready worker 45h v1.20.11+63f841a <omitted..>worker-node-name=w1
<omitted..>

This can also be done through openshift web console by clicking the “edit labels” option on a node.

Image

Add a nodeSelector field to the virtual machine

A node selector can be added through openshift web console.

Image

Image

1
2
3
4
$ oc describe vm vm1
Spec:
Node Selector:
Worker - Node - Name: w1

Reference