4

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
2
  • You might be looking for a templating tool like Helm, or something like Kustomize that can make more targeted modifications to the Kubernetes manifests. If you control the application, passing settings as environment variables is a little easier to manage than command-line arguments. Commented May 22, 2020 at 2:25
  • Thanks ! I don't use Kustomize currently. Is there a way to pass the argument values when I run the kubectl cmd to deploy the deployment yaml? Is there any other option other than Helm or Kustomize? Thanks! Commented May 22, 2020 at 5:28

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

1

This should be added in your deployment.yml

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

4 Comments

Thanks! Could you please let me know where I can pass this args: above? Can I pass as parameter to kubectl cmd? Can you give with an example? Thanks!
Please add in your deployment yml itself. spec: containers: - name: testimage image: <my image name>:<tag> args: ["--userid","=","testuser","--role","=","manager"]
Thanks! The below would not work as I want to pass the value for he arguments using kubectl command when I apply the deployment yaml. The below sets the value I believe . How can I pass different values wihout changing the deploymen yaml? spec: containers: - name: testimage image: <my image name>:<tag> args: ["--userid","=","testuser","--role","=","manager"]
@JaskiratSra what you are adding in the comment session is actually a valuable input to your answer. Don't forget that you can edit your posts (I did it this time).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.