Is there a one line kubectl command to add the nodeSelector in the pod yaml?

2/25/2021

I was wondering if there was a one line kubectl command to add the nodeSelector in the pod yaml? (I have already attached a label to the node) I am trying to automate this and hence I want to avoid manually downloading the yaml file and adding the nodeSelector. Any ideas using sed or kubectl replace would be appreciated.

-- Contraboy
kubernetes
nodeselector

2 Answers

2/26/2021

FYI I ended up using this command kubectl patch deployments DEPLOYMENTNAME -p '{"spec": {"template": {"spec": {"nodeSelector": {"YOURLABEL": "YOURVALUE"}}}}}

-- Contraboy
Source: StackOverflow

2/25/2021

You can add nodeSelector in pod spec.

As the k8s doc : 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.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    env: test
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd

update

I found a way to do this:

kubectl run --generator=run-pod/v1 -ti --rm test --image=ubuntu:20.04 --overrides='{"spec": { "nodeSelector": {"nodename": "test-node"}}}'
-- Sahadat Hossain
Source: StackOverflow