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

1.5k views Asked by At

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.

2

There are 2 answers

5
Sahadat Hossain On BEST ANSWER

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"}}}'
0
Contraboy On

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