Kubernetes Deployment - Pass arguments

5/22/2020

Could anyone please help me? I want to pass arguments to Kubernetes deployment when I use kubectl command to apply the deployment file?

Ex: In my Deployment. yaml, I have arguments as below and I want to pass the argument values when I run the kubectl apply - f .yaml

So, in the below example, I want to override the args - userid and role when I run the above kubectl command.Thanks in advance!!

spec:
      containers:
        - name: testimage
          image: <my image name>:<tag>
          args:
          - --userid=testuser
          - --role=manager
-- testbg testbg
kubectl
kubernetes
kubernetes-deployment

2 Answers

5/22/2020

The simple answer is. You can't do that.

kubectl is not a template engine. As some people mentioned, you have options like Helm or Kustomize which can solve this. I'd encurage you to look into Helm3 since it nicely solves your problem with a command like helm upgrade --install ... --set userid=xxx --set role=yyy.

If you're stuck with kubectl only though, you might want to use it's ability to ingest yaml from stdin and pass your yaml through any type of templating first. ie. as follows :

...
args:
- --userid=$USER
- --role=$ROLE
...
cat resource.yaml | USER=testuser ROLE=manager envsubst | kubectl apply -f -

obviously any other string replacement method would do (sed, awk, etc.)

-- Radek 'Goblin' Pieczonka
Source: StackOverflow

5/22/2020

This should be added in your deployment.yml

spec: 
  containers: 
    - name: testimage 
      image: <my image name>:<tag> 
      args: ["--userid","=","testuser","--role","=","manager"]
-- JaskiratSra
Source: StackOverflow