Kubernetes 如何使用kubectl exec在容器中运行命令,该命令使用容器中的环境变量?

Kubernetes 如何使用kubectl exec在容器中运行命令,该命令使用容器中的环境变量?,kubernetes,kubectl,Kubernetes,Kubectl,我正在尝试编写一个脚本,使用kubectl exec在容器中运行一些命令。我想使用容器中存在的环境变量,但我很难找出如何防止我的本地shell对var求值,并且仍然在容器中对其求值 这是我的第一次尝试,但是$MONGODB_ROOT_PASSWORD由我的本地shell而不是容器中进行计算: kubectl -n enterprise exec mycontainer -- mongodump --username root --password $MONGODB_ROOT_PASSWORD -

我正在尝试编写一个脚本,使用kubectl exec在容器中运行一些命令。我想使用容器中存在的环境变量,但我很难找出如何防止我的本地shell对var求值,并且仍然在容器中对其求值

这是我的第一次尝试,但是$MONGODB_ROOT_PASSWORD由我的本地shell而不是容器中进行计算:

kubectl -n enterprise exec mycontainer -- mongodump --username root --password $MONGODB_ROOT_PASSWORD --out /dump
kubectl -n enterprise exec mycontainer -- echo 'mongodump --username root --password $MONGODB_ROOT_PASSWORD --out /dump' | sh
我尝试了这一点,但管道也有同样的问题,它是在本地而不是容器中进行评估的:

kubectl -n enterprise exec mycontainer -- mongodump --username root --password $MONGODB_ROOT_PASSWORD --out /dump
kubectl -n enterprise exec mycontainer -- echo 'mongodump --username root --password $MONGODB_ROOT_PASSWORD --out /dump' | sh

有没有办法用kubectl exec做到这一点?

你需要一个
sh-c
在里面,比如
exec--sh-c“whatever$PASSWORD”
你需要一个
sh-c
在里面,比如
exec--sh-c“whatever$PASSWORD”