Reputation: 680
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$
Upvotes: 4
Views: 13049
Reputation: 680
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
Upvotes: 3
Reputation: 17689
Can you try as below
# kubectl run mongo --image=mongo --port=27017
# kubectl exec -it mongo-857dc9fb9d-scknx -- mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
if it doesnt work then try below
# kubectl exec -it mongo-857dc9fb9d-scknx -- /opt/rh/rh-mongodb36/root/usr/bin/mongo
Upvotes: 0