简体   繁体   中英

How to fix json unmarshal error while executing kubectl patch command?

When following tutorial: https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/ i run into error. The following command fails:

kubectl patch sts web -p '{"spec":{"replicas":3}}'

Error from server (BadRequest): json: cannot unmarshal string into Go value of type map[string]interface {}

How do I fix this?

This is the container image on pods: k8s.gcr.io/nginx-slim:0.8

I am using minikube on Windows 7 Pro and standard cmd shell.

kubectl version

Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.0", 
GitCommit:"2bd9643cee5b3b3a5ecbd3af
9d09018f0773c77", GitTreeState:"clean", BuildDate:"2019-09-18T14:36:53Z", 
GoVersion:"go1.12.9", Compiler:"gc"
Platform:"windows/amd64"}

Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.0", 
GitCommit:"2bd9643cee5b3b3a5ecbd3af
9d09018f0773c77", GitTreeState:"clean", BuildDate:"2019-09-18T14:27:17Z", 
GoVersion:"go1.12.9", Compiler:"gc"
Platform:"linux/amd64"}

Try surrounding it with double quotes and then escaping the double quotes inside:

kubectl patch sts web -p "{\"spec\":{\"replicas\":3}}"

Since you're going to perform a scale , you should use the command provided by kubectl :

kubectl scale statefulset web --replicas=3

In addition to the accepted answer, you may also get this error if you try to apply the patch from file. In such case you can just cat the file as follows:

kubectl patch deployment/<deployment-name> -p "$(cat <patch-file-name>.yaml)" --namespace <namespace name>

Refer here for more.

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