简体   繁体   English

如何在执行 kubectl patch 命令时修复 json 解组错误?

[英]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.在学习以下教程时: https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/我遇到了错误。 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这是 pod 上的容器镜像:k8s.gcr.io/nginx-slim:0.8

I am using minikube on Windows 7 Pro and standard cmd shell.我在 Windows 7 Pro 和标准 cmd shell 上使用 minikube。

kubectl version kubectl 版本

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:尝试用双引号括起来,然后 escaping 里面的双引号:

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

Since you're going to perform a scale , you should use the command provided by kubectl :由于您要执行scale ,因此您应该使用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.请参阅此处了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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