Reputation: 2531
How to execute a Bash command in the pod? I want to do everything with a single 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:/#
Or so.
[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
If it's just a Bash, it certainly works. But I want to jump into the mongo shell immediately.
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
Upvotes: 114
Views: 312366
Reputation: 174
Tested with Kubernetes 1.30.1
kubectl exec <pod> -n <namespace> -- /bin/bash -c "here is your command or the script with all your commands"
Upvotes: 0
Reputation: 1
I think the main issue here is the lack of space between -- and bash
.
I ran into a similar problem and simply added --, then a space before bash
.
It works pretty well this way.
kubectl exec -it --namespace=my-namespace my-pod -c my-container -- bash -c "pwd"
The first -c flag means container
. The second means command
Note: The container flag is optional. Only use it if you've got multiple containers in the pod and you want to execute commands within a specific container.
Upvotes: 0
Reputation: 48356
Case 1: For one container in the pod, you could directly use
kubectl exec -it -n NAMESPACE pod-name -- /bin/bash
or
kubectl exec -it -n NAMESPACE pod-name -- /bin/sh
it depended on the type of shell command used in your pod.
Case 2: There is more than one container in the Pod, the additional -c
could be used to figure out this container.
kubectl exec -it -n NAMESPACE pod-name -c container-name -- /bin/bash
Case 3: There is NO shell in your container image, like cluster autoscaler
. One option is to run a shell in this container through ephemeral containers
and kubectl debug
. Note: Ephemeral container support went into beta in Kubernetes 1.23.
kubectl debug -n NAMESPACE -it pod-name --image=ubuntu
Upvotes: 1
Reputation: 15563
For me this was the right command (and order of parameters) to open a pod shell:
kubectl -n <namepace> exec -it <pod> -- /bin/sh
Note: Using /bin/bash
caused this error:
error: Internal error occurred: error executing command in container: failed to exec in container: failed to start exec "": OCI runtime exec failed: exec failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown
So make sure you're using /bin/sh
to avoid unwanted errors.
Upvotes: 2
Reputation: 666
I use something like this to get into the pod's shell:
kubectl exec -it --namespace develop pod-name bash
then you can execute the command you want within the pod (e.g. ping)
ping www.google.com
then you can see your ping log and voila ... enjoy it :D
Upvotes: 32
Reputation: 11317
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"
You forgot a space between "--" and "bash".
To execute multiple commands you may want:
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
Upvotes: 200