Reputation: 1533
While using vanilla docker, compose or swarm, I’m able to pass “—add-env FOO=bar” as a Docker cli parameter in order to define a env var value for the container at runtime. However, In kubernetes, I’m only seeing how to hardcore the value in the .yml file at the container spec section.
Can anyone point me how to pass it at runtime? (I.e. pass a env var when using “kubectl apply ...”)
Thanks! Really appreciate any help!
Best regards,
Upvotes: 0
Views: 2819
Reputation: 1254
It's pretty easy to use the templating language of your choice. For example with erb you can just do erb deployment.erb.yaml | kubectl apply -f -
.
For most simple use cases you can just use envsubst: cat deployment.yaml | envsubst | kubectl apply -f -
Upvotes: 2
Reputation: 22922
I assume that by "runtime" you actually mean when kubectl create/apply is invoked. No that is not possible.
What you want to do instead is probably run your manifest via some templating engine. The simplest you could do is to pipe your template via sed like cat manifest | sed 's/PLACEHOLDER/VALUE/'
into kubectl apply so you can have the ENV defined properly in manifest with a placeholder string that is replaced before it is pushed to kubectl. A bit more advanced solution, and the one I would generally advise, is to get familiar with helm charts where you can for example run helm upgrade my-release --reuse-vars --set my.var=value
Upvotes: 3