how to execute an argument in kubernetes?

爷,独闯天下 提交于 2019-12-11 17:18:25

问题


I have misunderstanding with how to execute $() commands in exec. i'm creating a job in kubernetes with this params:

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

but the part with $(kubectl -n ... --selector ...) is treated as a string and don't execute. Please tell me how to do it properly. Thanks!


回答1:


As far as I know this is not achievable by putting each section as an array element. Instead you can do something like the following:

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



回答2:


From the output of the kubectl exec, I noticed that you can use -- to separate your arguments

# 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


来源:https://stackoverflow.com/questions/49245628/how-to-execute-an-argument-in-kubernetes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!