Skip to content
Advertisement

kubectl exec behaving strangely, stating command not found even though the binary is there in the pod

So have an OpenShift cluster and running a pod in the mongodb-test namespace. The pod is running fine

$ kubectl get pods -n mongodb-test                                             
NAME               READY   STATUS      RESTARTS   AGE
mongodb-1-7ww9k    1/1     Running     0          14m

When I exec into the pod and run the mongo command, I dont get any issue and the command works as expected.

$ kubectl exec -it -n mongodb-test  mongodb-1-7ww9k   -c mongodb  sh
sh-4.2$ mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
> 

Now the problem is when I try to run the same command using below syntax I get mongo not found

$ kubectl exec -it -n mongodb-test mongodb-1-7ww9k -c mongodb -- sh -c mongo
sh: mongo: command not found
E0227 13:02:01.728579   24237 v3.go:79] EOF
                                           command terminated with exit code 127

Below are the output of echo $PATH and which mongo from inside the pod.

$ kubectl exec -ti -n mongodb-test mongodb-1-7ww9k -c mongodb -- sh 
sh-4.2$ echo $PATH
/opt/rh/rh-mongodb36/root/usr/bin:/opt/rh/rh-mongodb36/root/usr/sbin:/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
sh-4.2$ which mongo
/opt/rh/rh-mongodb36/root/usr/bin/mongo
sh-4.2$ 

Advertisement

Answer

So, here is what the problem was. When I tried to execute mongo actually after getting inside the pod using the command

$ kubectl exec -it -n mongodb-test  mongodb-1-7ww9k   -c mongodb  sh

somehow the path to where the mongo executable is was being set into PATH (through .bash_profile :confused), but when I tried to call mongo, using the below command, the same was not happening.

kubectl exec -it -n mongodb-test mongodb-1-7ww9k -c mongodb -- sh -c mongo

Since we suspected the PATH was being set in .bash_profile I tried to execute mongo in bash and below command worked.

kubectl exec -it -n mongodb-test mongodb-1-7ww9k -c mongodb -- bash -c mongo
Advertisement