简体   繁体   English

kubectl执行失败,出现“容器” <command> 不适用于广告连播 <pod_name> ”

[英]kubectl exec failed with “container <command> is not valid for pod <pod_name>”

I am working with Minikube and I have alpine pod with one container. 我正在与Minikube合作,我有一个装有一个容器的高山豆荚。
When I run: 当我跑步时:
kubectl exec -it -v=6 alpinec1-7c65db48b4-v2gpc /bin/sh

I receive a shell and I can run any command ( ifconfig , etc.) inside it. 我收到一个外壳,并且可以在其中运行任何命令( ifconfig等)。

But when I tried to run sh with -c it failed: 但是,当我尝试使用-c运行sh ,它失败了:

root:~# kubectl exec -it -v=6 alpinec1-7c65db48b4-v2gpc /bin/sh -c 'ifconfig'
I0722 05:45:25.091111   80392 loader.go:357] Config loaded from file /home/root/.kube/config
I0722 05:45:25.111876   80392 round_trippers.go:405] GET https://192.168.190.143:8443/api/v1/namespaces/default/pods/alpinec1-7c65db48b4-v2gpc 200 OK in 16 milliseconds
I0722 05:45:25.232564   80392 round_trippers.go:405] POST https://192.168.190.143:8443/api/v1/namespaces/default/pods/alpinec1-7c65db48b4-v2gpc/exec?command=%2Fbin%2Fsh&container=ifconfig&container=ifconfig&stdin=true&stdout=true&tty=true 400 Bad Request in 13 milliseconds
                                                                     I0722 05:45:25.232921   80392 helpers.go:201] server response object: [{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "container ifconfig is not valid for pod alpinec1-7c65db48b4-v2gpc",
  "reason": "BadRequest",
  "code": 400
}]
F0722 05:45:25.233095   80392 helpers.go:119] Error from server (BadRequest): container ifconfig is not valid for pod alpinec1-7c65db48b4-v2gpc

kubectl interprets the -c flag not as a flag for ifconfig , but as a flag for the kubectl exec command itself -- which specifies the exact container of a Pod in which the command should be executed; kubectl不会将-c标志解释为ifconfig的标志,而是将其解释为kubectl exec命令本身的标志-该标志指定应在其中执行命令的Pod的确切容器; this is also the reason that kubectl looks for a container named "ifconfig" in your Pod. 这也是kubectl在Pod中寻找一个名为“ ifconfig”的容器的原因。 See the documentation for more information. 请参阅文档以获取更多信息。

Instead, use -- to denote flags that should not be interpreted by kubectl exec any more but instead be passed to the invoked command ( ifconfig , in this case) as-is: 而是使用--表示不再由kubectl exec解释的标志,而是按kubectl exec传递给调用的命令(在本例中为ifconfig ):

$ kubectl exec -it -v=6 alpinec1-7c65db48b4-v2gpc -- /bin/sh -c 'ifconfig'

Also note that in this case, you do not really need to invoke ifconfig from a shell; 还要注意,在这种情况下,您实际上不需要从shell调用ifconfig。 you could also just directly call ifconfig without using /bin/sh : 您也可以直接调用ifconfig而不使用/bin/sh

$ kubectl exec -it -v=6 alpinec1-7c65db48b4-v2gpc -- ifconfig

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

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