使用kubectl在pod中执行bash命令? [英] Execute bash command in pod with kubectl?
问题描述
我的问题很简单.
如何在Pod中执行bash命令?我想用一个bash命令来做所有事情?
How to execute a bash command in the pod? I want to do everything with one bash command?
[root@master ~]# kubectl exec -it --namespace="tools" mongo-pod --bash -c "mongo"
Error: unknown flag: --bash
因此,该命令将被忽略.
So, the command is simply ignored.
[root@master ~]# kubectl exec -it --namespace="tools" mongo-pod bash -c "mongo"
root@mongo-deployment-78c87cb84-jkgxx:/#
大概.
[root@master ~]# kubectl exec -it --namespace="tools" mongo-pod bash mongo
Defaulting container name to mongo.
Use 'kubectl describe pod/mongo-deployment-78c87cb84-jkgxx -n tools' to see all of the containers in this pod.
/usr/bin/mongo: /usr/bin/mongo: cannot execute binary file
command terminated with exit code 126
如果只是扑朔迷离,那肯定行得通.但是我想立即跳入mongo shell.
If it's just a bash, it certainly works. But I want to jump into the mongo shell immediatelly.
我找到了一个解决方案,但是它不起作用.告诉我现在是否有可能? 执行多个命令(或从shell执行)脚本)在kubernetes容器中
I found a solution, but it does not work. Tell me if this is possible now? Executing multiple commands( or from a shell script) in a kubernetes pod
谢谢.
推荐答案
双破折号-"用于将要传递给命令的参数与kubectl参数分开. 所以正确的方法是:
The double dash symbol "--" is used to separate the arguments you want to pass to the command from the kubectl arguments. So the correct way is:
kubectl exec -it --namespace=tools mongo-pod -- bash -c "mongo"
您忘记了-"和"bash"之间的空格.
You forgot a space between "--" and "bash".
要执行多个命令,您可能需要:
To execute multiple commands you may want:
-
创建脚本并将其作为卷安装在pod中并执行
to create a script and mount it as a volume in your pod and execute it
使用脚本启动辅助容器并运行它
to launch a side container with the script and run it
这篇关于使用kubectl在pod中执行bash命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!