如何在kubernetes中执行参数?

时间:2018-03-12 22:51:11

标签: kubernetes kubectl

我对如何在exec中执行$()命令有误解。我用这个参数在kubernetes创造了一份工作:

command:
    - ./kubectl
    - -n
    - $MONGODB_NAMESPACE
    - exec
    - -ti
    - $(kubectl
    - -n
    - $MONGODB_NAMESPACE
    - get
    - pods
    - --selector=app=$MONGODB_CONTAINER_NAME
    - -o
    - jsonpath='{.items[*].metadata.name}')
    - --
    - /opt/mongodb-maintenance.sh

但是带有$(kubectl -n ... --selector ...)的部分被视为字符串而不执行。请告诉我如何正确地做到这一点。谢谢!

2 个答案:

答案 0 :(得分:0)

据我所知,将每个部分作为数组元素无法实现。相反,您可以执行以下操作:

command:
    - /bin/sh
    - -c
    - |        
      ./kubectl -n $MONGODB_NAMESPACE exec -ti $(kubectl -n $MONGODB_NAMESPACE get pods --selector=app=$MONGODB_CONTAINER_NAME -o jsonpath='{.items[*].metadata.name}') -- /opt/mongodb-maintenance.sh

答案 1 :(得分:0)

从kubectl执行程序的输出中,我注意到您可以使用--来分隔参数

# List contents of /usr from the first container of pod 123456-7890 and sort by modification time.
# If the command you want to execute in the pod has any flags in common (e.g. -i),
# you must use two dashes (--) to separate your command's flags/arguments.
# Also note, do not surround your command and its flags/arguments with quotes
# unless that is how you would execute it normally (i.e., do ls -t /usr, not "ls -t /usr").

kubectl exec 123456-7890 -i -t -- ls -t /usr