简体   繁体   English

kubectl 运行中的格式错误的命令

[英]Malformed command in kubectl run

I have a Kubernetes cluster on which is deployed a replica of this image (but it's not important for the sake of the question).我有一个 Kubernetes 集群,在该集群上部署了映像的副本(但就问题而言,这并不重要)。 Now, in Powershell I need to perform one REST request against the pod.现在,在 Powershell 中,我需要对 pod 执行一个 REST 请求。 In particular I have to send a PUT request to the endpoint http://<POD_IP>:18681/PCCIS/V1/Service/Properties/Servers passing this JSON payload:特别是我必须发送一个PUT请求到端点http://<POD_IP>:18681/PCCIS/V1/Service/Properties/Servers传递这个 JSON 有效载荷:

{"servers":[{"port":"18682","address":"10.244.1.29"}]}

What I was trying to do is perform this action from within a curlimages/curl pod, just to be able to reach the pod's network.我试图做的是从curlimages/curl pod 中执行此操作,只是为了能够访问 pod 的网络。 This is my attempt:这是我的尝试:

$PrizmDocPodIP="10.244.1.29"
$JsonPostBody='{"servers":[{"port":"18682","address":"10.244.1.29"}]}'
$cmd = "curl -i --header 'Content-Type: application/json' --request PUT --data '$JsonPostBody' http://$($PrizmDocPodIP):18681/PCCIS/V1/Service/Properties/Servers"
kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never "$cmd"

Unexpectedly, the command output this error:没想到,命令output这个错误:

curl: (3) nested brace in URL position 82:
curl -i --header 'Content-Type: application/json' --request PUT --data '{servers:[{port:18682,address:10.244.1.29}]}' http://10.244.1.29:18681/PCCIS/V1/Service/Properties/Servers

As you can see, the problem I have is that the string command I pass is not interpreted the right way and moreover the payload loses all the doublequotes of the JSON, that are important for the curl command to propertly work.如您所见,我遇到的问题是我传递的字符串命令没有以正确的方式解释,而且有效负载丢失了 JSON 的所有双引号,这对于curl命令正常工作很重要。

One another test I made is with this command (the echo is used to show what the sh command really would execute)我进行的另一项测试是使用此命令( echo显用于显示sh命令实际执行的内容)

kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never -- sh -c "echo $cmd"

And the output is output 是

curl -i --header Content-Type: application/json --request PUT --data {servers:[{port:18682,address:10.244.1.29}]} http://10.244.1.29:18681/PCCIS/V1/Service/Properties/Servers

Showing that the cmd Powershell variable loses its doublequotes when passed to kubectl run显示cmd Powershell 变量在传递给kubectl run时会丢失其双引号

What should be the right syntax to pass the $cmd command as is (preserving all its quotes and chars)?按原样传递$cmd命令的正确语法应该是什么(保留其所有引号和字符)?

I found the solution after some searching and trials.经过一番搜索和试验,我找到了解决方案。 First, I have to escape the double quotes in the $JsonPostBody variable.首先,我必须转义$JsonPostBody变量中的双引号。

$PrizmDocPodIP="10.244.1.29"
$JsonPostBody='{\"servers\":[{\"port\":\"18682\",\"address\":\"10.244.1.29\"}]}'

The other error is that the curlimages/curl Docker image executes the kubectl run arguments as the curl 's arguments, so I don't have to use the curl command: The other error is that the curlimages/curl Docker image executes the kubectl run arguments as the curl 's arguments, so I don't have to use the curl command:

kubectl run -i --tty --rm curl --image=curlimages/curl --restart=Never -- -i --header 'Content-Type: application/json' --request PUT --data $JsonPostBody http://$($PrizmDocPodIP):18681/PCCIS/V1/Service/Properties/Servers

This outputs这输出

HTTP/1.1 200 OK
[1mDate[0m: Wed, 08 Jul 2020 13:23:30 GMT
[1mConnection[0m: keep-alive
1mContent-Length[0m: 0

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

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