100

My question is simple.

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

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
JDev
  • 2,157
  • 3
  • 31
  • 57

4 Answers4

181

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

Dojo
  • 5,374
  • 4
  • 49
  • 79
Nicola Ben
  • 10,615
  • 8
  • 41
  • 65
  • >"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 '18 at 07: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 '18 at 08: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 '18 at 08:51
  • Take a look here https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/ – Nicola Ben Nov 23 '18 at 08:53
  • Does it mean that the only way to create a kubernetes volume from a local file is to upload the file to a web server, then create a pod which would download the file to the volume? I'm just trying to understand whether I'm just stupid or is the design really that ...inconvenient. – Ark-kun Nov 26 '18 at 20:32
  • 1
    That's what I want! Thank you! – Lellansin Oct 08 '19 at 17:49
  • I might be a bit late to the party here, but I've just come across this answer - you could create the script in a ConfigMap and mount the config map as a volume in the container? – Steve Folly Nov 08 '22 at 14:54
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

Anthony Piñero
  • 616
  • 5
  • 10
  • It's not executing any command on the pod shell. It is just logging on to the pod shell. – Vishal786btc Apr 12 '19 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 '20 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. – Anthony Piñero Mar 18 '20 at 18:22
1

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.

Ghasem
  • 14,455
  • 21
  • 138
  • 171
0

Kindly run below one liner with your custom commands for Execute bash command in pod with kubectl purpose

kubectl exec -it  nginx-pod  -- bash -c "ls;pwd;echo hello;uname -r;cat /etc/*se"
linux.cnf
  • 519
  • 6
  • 7