93

my question is simple.

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:/# 

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 immediatelly.

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

Thanks.

1
  • 1
    bash -c does not add anything useful here.
    – tripleee
    Mar 26, 2021 at 7:17

3 Answers 3

170

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

7
  • >"to create a script and mount it as a volume in your pod and execute it" - Given a script, how can I mount it to a Pod as a volume using kebectl? It's easy with docker, but I do not know how to do that with Kubernetes.
    – Ark-kun
    Nov 23, 2018 at 7:59
  • Mount the volume containing the script and then call it from inside your container. For example in your container you call /volume/script.sh and in kubernetes you mount your volume and give /volume as mount folder.
    – Nicola Ben
    Nov 23, 2018 at 8:15
  • 1
    I guess my main problem is creating the volume containing the script. With Docker it's trivial to mount some part of host filesystem to the container. With multi-node Kubernetes cluster, I'm at loss.
    – Ark-kun
    Nov 23, 2018 at 8:51
  • Take a look here kubernetes.io/docs/tasks/configure-pod-container/…
    – Nicola Ben
    Nov 23, 2018 at 8:53
  • 1
    That's what I want! Thank you!
    – Lellansin
    Oct 8, 2019 at 17:49
28

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

3
  • It's not executing any command on the pod shell. It is just logging on to the pod shell. Apr 12, 2019 at 16:29
  • @Anthony , Nicola, it seems kubectl exec requires container name (irrespective of we specified or not) . Isnt there a way to just connect the pod ( and not the container) ?
    – Nag
    Mar 18, 2020 at 11:50
  • @Vishal786btc your perception is very subjective taking into account that the post does not indicate executing a command without having to enter the pod, so some will want to enter or not inside the pod to execute a command. Mar 18, 2020 at 18:22
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.