nodeSelectors - q-uest/notes-doc-k8s-docker-jenkins-all-else GitHub Wiki

You can constrain a Pod to only be able to run on particular Node(s), or to prefer to run on particular nodes. There are several ways to do this, and the recommended approaches all use label selectors to make the selection. Generally such constraints are unnecessary, as the scheduler will automatically do a reasonable placement (e.g. spread your pods across nodes, not place the pod on a node with insufficient free resources, etc.) but there are some circumstances where you may want more control on a node where a pod lands, for example to ensure that a pod ends up on a machine with an SSD attached to it, or to co-locate pods from two different services that communicate a lot into the same availability zone.

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.

  • Run below command to get list of nodes

kubectl get nodes

  • If you want label a node then use below command

kubectl label nodes <node-name> <label-key>=<label-value>

  • For example assume node name is kube-node-1 and my desired label is 'disktype=ssd', then I can run

kubectl label nodes kube-node-1 disktype=ssd

Now pod templates look like

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd
⚠️ **GitHub.com Fallback** ⚠️