如何在 kubernetes 中执行参数?

how to execute an argument in kubernetes?

我对如何在 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 ...) 的部分被视为字符串并且不执行。请告诉我如何正确地做到这一点。谢谢!

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

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

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

# 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