0

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
  • 99
  • 1
  • 1
  • 10

2 Answers2

3

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
  • 3,583
  • 2
  • 12
  • 19
  • This is exactly what I am trying to do, normally I download the file and add the nodeSelector manually to the pod spec. I am trying to automate this via a 1 liner if there is a way? – Contraboy Feb 25 '21 at 20:47
  • Do you have an example of adding and applying the nodeSelector using kubectl edit to the pod spec? Appreciate your help! – Contraboy Feb 25 '21 at 20:54
  • 2
    I got a command for doing this, try this one – Sahadat Hossain Feb 25 '21 at 21:08
  • Thanks I will give it a shot! – Contraboy Feb 25 '21 at 21:08
  • 1
    @Contraboy I'd reckon that your comment about `$ kubectl patch` could be posted as answer as it's also a valid solution. Please post your comment as an answer with some explanation to give it more visibility for future readers of this question. – Dawid Kruk Feb 26 '21 at 11:55
0

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

Contraboy
  • 99
  • 1
  • 1
  • 10