Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute bash command in pod with kubectl?

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.

like image 757
JDev Avatar asked Jul 09 '18 14:07

JDev


People also ask

Which command is used to run a shell inside the pod?

The kubectl exec command lets you start a shell session inside containers running in your Kubernetes cluster.

How do you SSH into a pod in Kubernetes?

In order to SSH into the Pod, the Pod should have SSH server installed. This can be provisioned by installing OpenSSH Server as part of the Docker image tied to the Pod. The following commands should be included in the Dockerfile associated with the container tied to the Pod.


2 Answers

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

like image 38
Anthony Piñero Avatar answered Sep 22 '22 22:09

Anthony Piñero


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

like image 153
Nicola Ben Avatar answered Sep 21 '22 22:09

Nicola Ben