简体   繁体   中英

kubectl run override nodeselctor: error: Invalid JSON Patch

I'm trying to override the node selector for a kubectl run .

kubectl run -it powershell --image=mcr.microsoft.com/powershell:lts-nanoserver-1809-20211215 --restart=Never --overrides='{ "apiVersion": "v1", "spec": { "template": { "spec": { "nodeSelector": { "kubernetes.io/os": "windows" } } } } }' -- pwsh

But I get "Invalid Json Path".

This is my yaml if I do a deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
 ...
spec:
  ...
  template:
    ...
    spec:
      ...
      nodeSelector:
        kubernetes.io/os: windows

and if I do get pods -o json I get:

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        ...
    },
    "spec": {
        ...
        "nodeSelector": {
            "kubernetes.io/os": "windows"
        }

kubectl run is a command to start a Pod . You can read more about it here

kubectl run -it powershell --image=mcr.microsoft.com/powershell:lts-nanoserver-1809-20211215 --restart=Never --overrides='{ "apiVersion": "v1", "spec": { "template": { "spec": { "nodeSelector": { "kubernetes.io/os": "windows" } } } } }' -- pwsh

Using a command above you are trying run a Pod with specification "template": { "spec": { which is used only for Deployment and that is why you get an error Invalid Json Path .

nodeSelector as you can see in documentation could be specify under spec in Pod config file as below:

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

When you add --dry-run=client -o yaml to your command to see how the object would be processed, you will see below output which doesn't have nodeSelector :

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: powershell
  name: powershell
spec:
  containers:
  - image: mcr.microsoft.com/powershell:lts-nanoserver-1809-20211215
    name: powershell
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

To solve your issue, you can delete template and spec from you command which should look as below:

kubectl run -it powershell --image=mcr.microsoft.com/powershell:lts-nanoserver-1809-20211215 --restart=Never --overrides='{ "apiVersion": "v1", "spec": { "nodeSelector": { "kubernetes.io/os": "windows" } } }' -- pwsh

Adding --dry-run=client -o yaml to see what will be changed, you will see that nodeSelector exist:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: powershell
  name: powershell
spec:
  containers:
  - image: mcr.microsoft.com/powershell:lts-nanoserver-1809-20211215
    name: powershell
    resources: {}
  dnsPolicy: ClusterFirst
  nodeSelector:
    kubernetes.io/os: windows
  restartPolicy: Never
status: {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM