nodeSelector
nodeSelector is the simplest recommended form of node selection constraint. nodeSelector is a field of PodSpec. It specifies a map of key-value pairs. For the pod to be eligible to run on a node, the node must have each of the indicated key-value pairs as labels (it can have additional labels as well). The most common usage is one key-value pair.
Attach a label to the node
Run kubectl get nodes to get the names of your cluster’s nodes.
kubectl get nodes
Output will be like
Pick out the one that you want to add a label to, and then run
kubectl label nodes <node-name> <label-key>=<label-value>
to add a label to the node you’ve chosen.
For example, if my node name is ‘ip-192-168-15-64.us-west-2.compute.internal’ and my desired label is ‘disktype=ssd’, then I can run
kubectl label nodes ip-192-168-15-64.us-west-2.compute.internal disktype=ssd
You can verify that it worked by re-running kubectl get nodes –show-labels and checking that the node now has a label. You can also use kubectl describe node “nodename” to see the full list of labels of the given node.
kubectl get nodes --show-labels
Output will be like
Add a nodeSelector field to your pod configuration
Take whatever pod config file you want to run, and add a nodeSelector section to it, like this. For example, if this is my pod config:
Then add a nodeSelector like so:
cat <<EoF > ~/environment/pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd
EoF
Then you run
kubectl apply -f ~/environment/pod-nginx.yaml
And the Pod will get scheduled on the node that you attached the label to. You can verify that it worked by running
kubectl get pods -o wide
And looking at the “NODE” that the Pod was assigned to