How to pass command line argument to kubectl create command

2/28/2020

If I run the kubectl create -f deployment.yaml command with the following deployment.yaml file, everything succeeds.

apiVersion: v1
kind: Pod
metadata:
 name: my_app
 labels:
  app: my_app
spec:
 containers:
  - name: my_app
    image: docker:5000/path_to_my_custom_image
    args: ["my_special_argument"]

However, now I want to have a custom "my_special_argument" as follows

apiVersion: v1
kind: Pod
metadata:
 name: my_app
 labels:
  app: my_app
spec:
 containers:
  - name: my_app
    image: docker:5000/path_to_my_custom_image
    args: ["$(ARG)"]

and I want to somehow set the value of $ARG$ when I execute the kubectl create -f deployment.yaml command. How to do that?

I am looking for something like: kubectl create -f deployment.yaml --ARG=new_arg

Can such command be executed?

-- mercury0114
command-line
command-line-arguments
kubectl
kubernetes

1 Answer

2/28/2020

You can use Environment variables in the deployment.yaml

apiVersion: v1
kind: Pod
metadata:
 name: my_app
 labels:
  app: my_app
spec:
 containers:
  - name: my_app
    image: docker:5000/path_to_my_custom_image
    env:
    - name: SPECIAL_ARG_VAL
      value: "my_special_argument_val_for_my_app"
    args: ["$(SPECIAL_ARG_VAL)"]

Also, you can load the value for environment variables using Secrets or Configmaps.

Here is an example loading value from configmap

apiVersion: v1
kind: Pod
metadata:
 name: my_app
 labels:
  app: my_app
spec:
 containers:
  - name: my_app
    image: docker:5000/path_to_my_custom_image
    env:
    - name: SPECIAL_ARG_VAL
      valueFrom:
        configMapKeyRef:
          name: special-config
          key: SPECIAL_VAL_KEY
    args: ["$(SPECIAL_ARG_VAL)"]

You can create the configmap using kubectl as the following, but recommend to have a separate yaml file.

kubectl create configmap special-config --from-literal=SPECIAL_VAL_KEY=my_special_argument_val_for_my_app

You can even remove the args from the pod yaml above if you had the same environment variable defined in the Dockerfile for the image.

-- Sithroo
Source: StackOverflow