I am trying to run a kubectl exec command on a pod, but it fails saying 'No such file or directory'
I can run the command if I login to the terminal of the pod through bash Also this problem is only for a few commands. I found that there is a PATH variable difference
When i do kubectl exec $POD -- printenv , then PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
When i run -- printenv from the terminal of POD , then PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/abc/scripts:/opt/abc/bin:/opt/admin/bin:/opt/abc/bin:/root/bin
I am guessing this is causing the commands to fails when run through kubectl exec.
Any ideas to overcome this are welcome; can we pass the env variable of PATH in someway to the POD which using kubectl exec ?
CodePudding user response:
You can try executing bash -c "<command>"
$ kubectl exec <pod> -- bash -c "<cmd>"
It is likely PATH is being modified by some shell initialization files
CodePudding user response:
The issue was required env variable (PATH) and some others were missing. I passed the required env variables along with the command.
Example
kubectl exec $pod_name -- bash -c "cd ../b/; env ENV_VARIABLE_1=ENV_VALUE_2 && env ENV_VARIABLE_2=ENV_VALUE_2 && "
Details: - Kubectl set environment variables and run command