使用 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
如果它只是一个 bash,它当然可以工作.但我想立即跳入 mongo shell.
If it's just a bash, it certainly works. But I want to jump into the mongo shell immediatelly.
我找到了一个解决方案,但它不起作用.告诉我这现在是否可能?执行多个命令(或从 shell 执行)脚本)在 kubernetes pod 中
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 command you want to run inside the container 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屋!