how to execute an argument in kubernetes?

2019-08-18 03:49发布

问题:

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