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.
Run kubectl get nodes to get the names of your cluster’s nodes.
kubectl get nodes
Output will be like
We will add a new label disktype=ssd
to the first node on this list.
but first, let’s confirm the label hasn’t be assign to any nodes by filtering the previous using the selector option.
kubectl get nodes --selector disktype=ssd
Output
To add a label to the fist node, we can run these command
# export the first node name as a variable
export FIRST_NODE_NAME=$(kubectl get nodes -o json | jq -r '.items[0].metadata.name')
# add the label to the node
kubectl label nodes ${FIRST_NODE_NAME} disktype=ssd
Output example
We can verify that it worked by re-running the kubectl get nodes --selector
command
kubectl get nodes --selector disktype=ssd
Output example
We will now create a simple pod creating file with the nodeSelector
in the pod spec
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
Sample Output
The NODE
name should match the output of the command below
kubectl get nodes --selector disktype=ssd
Sample Output