Kubectl run set nodeSelector

7/3/2018

Is there a way to specify the nodeSelector when using the Kubernetes run command?

I don't have a yaml file and I only want to override the nodeSelector.

I tried the following but didn't work:

kubectl run myservice --image myserviceimage:latest --overrides='{ "nodeSelector": { "beta.kubernetes.io/os": "windows" } }'
-- nbilal
kubernetes

4 Answers

1/28/2019

In Kubernetes 1.12 and newer, the matching rules have changed to use nodeAffinity.

kubectl run hello-world --replicas=1 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=8080 --overrides='{"apiVersion":"v1","spec":{"affinity":{"nodeAffinity":{"requiredDuringSchedulingIgnoredDuringExecution":{"nodeSelectorTerms":[{"matchFields":[{"key":"metadata.name","operator":"In","values":["my-chosen-node-01"]}]}]}}}}}'

Note that this approach works fine in Unix like shells but there are quoting issues with running this under PowerShell on Windows.

-- Tony Apuzzo
Source: StackOverflow

7/7/2018

I have an actual answer now... Here is my final answer:

In order to specify the node selector via the run command (and make it work so that it runs on a certain node), we can do the following:

0) Make sure that the node that you want to target can schedule pods on it. My master node (master-0) was unprepared for this, so I had to remove its taint, via the command:

kubectl taint node master-0 node-role.kubernetes.io/master:NoSchedule-

(The trailing - is important), and where master-0 is replaced by the name of yours, if this case is needed.

1) Add the override command to the kubectl run command.

run hello-world --replicas=1 --labels="run=load-balancer-example" --image=gcr.io/google-samples/node-hello:1.0  --port=8080 --overrides='{ "apiVersion": "apps/v1beta1", "spec": { "template": { "spec": { "nodeSelector": { "kubernetes.io/hostname": "master-0" } } } } }'

*Make sure that your apiVersion is supported... I got the following error when I tried to use the v1:

error: no kind "Deployment" is registered for version "v1"

To get past this i ran the command:

kubectl api-versions

and at the urging of this thread, https://github.com/kubernetes/kubernetes/issues/55894 ,picked the "apps/v1beta1"

I am not sure how the above answers work, as they do not have enough encapsulation in the json... nor how: https://github.com/kubernetes/kubernetes/issues/45153 works, as to me the problem was that:

The key I was missing was that: it's not .spec.nodeSelector, it has to be .spec.template.spec.nodeSelector

-- Ruwd
Source: StackOverflow

7/4/2018

Try this:

kubectl run myservice --image myserviceimage:latest --overrides='{"apiVersion": "v1", "spec": {"nodeSelector": { "beta.kubernetes.io/os": "windows" }}}'

-- VKR
Source: StackOverflow

7/3/2018

The kubectl run documentation mentions:

--overrides="": 

An inline JSON override for the generated object.
If this is non-empty, it is used to override the generated object.
Requires that the object supply a valid apiVersion field.

So at least try:

--overrides='{ "apiVersion": "v1", "nodeSelector"... }'

If that does not work, check the yaml actually generated (from issue 24873);

for n in $(kubectl get -o=name pvc,configmap,serviceaccount,secret,ingress,service,deployment,statefulset,hpa,job,cronjob)
do
    mkdir -p $(dirname $n)
    kubectl get -o=yaml --export $n > $n.yaml
done
-- VonC
Source: StackOverflow