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

11/8/2019

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"}
-- hal
json
kubernetes

2 Answers

11/8/2019

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

kubectl scale statefulset web --replicas=3
-- prometherion
Source: StackOverflow

11/8/2019

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

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