testbg testbg
testbg testbg

Reputation: 333

Kubernetes Deployment - Pass arguments

I want to pass arguments to Kubernetes deployment when I use kubectl command to apply the deployment file.

Example: In my deployment .yaml, I have arguments as below and I want to pass the argument values when I run with the kubectl apply - f <my-deployment>.yaml

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

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

Upvotes: 4

Views: 12755

Answers (2)

JaskiratSra
JaskiratSra

Reputation: 112

This should be added in your deployment.yml

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

Upvotes: 0

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.)

Upvotes: 6

Related Questions