'Kubectl run set nodeSelector
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" } }'
Solution 1:[1]
nodeSelector must be wrapped with a spec. Like so
kubectl run -ti --rm test --image=ubuntu:18.04 --overrides='{"spec": { "nodeSelector": {"kubernetes.io/hostname": "eks-prod-4"}}}'
Solution 2:[2]
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:
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.
Add the override command to the kubectl run command.
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": { "template": { "spec": { "nodeSelector": { "kubernetes.io/hostname": "master-0" } } } } }'Make sure to use proper
apiVersionbased on your kubernetes version. You might need to useapps/v1beta1instead ofv1. You can run this to get proper version on your installation:kubectl api-versionscheck this thread if you faced any issues with the
apiVersion: https://github.com/kubernetes/kubernetes/issues/55894
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
Solution 3:[3]
Try this:
kubectl run myservice --image myserviceimage:latest --overrides='{"apiVersion": "v1", "spec": {"nodeSelector": { "beta.kubernetes.io/os": "windows" }}}'
Solution 4:[4]
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.
Solution 5:[5]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aitorhh |
| Solution 2 | ahmadali shafiee |
| Solution 3 | Vit |
| Solution 4 | Tony Apuzzo |
| Solution 5 | VonC |
