How to set dynamic values with Kubernetes yaml file How to set dynamic values with Kubernetes yaml file kubernetes kubernetes

How to set dynamic values with Kubernetes yaml file


You can't do it automatically, you need to use an external script to "compile" your template, or use helm as suggested by @Jakub.

You may want to use a custom bash script, maybe integrated with your CI pipeline.

Given a template yml file called deploy.yml.template very similar to the one you provided, you can use something like this:

#!/bin/bash# sample value for your variablesMYVARVALUE="nginx:latest"# read the yml template from a file and substitute the string # {{MYVARNAME}} with the value of the MYVARVALUE variabletemplate=`cat "deploy.yml.template" | sed "s/{{MYVARNAME}}/$MYVARVALUE/g"`# apply the yml with the substituted valueecho "$template" | kubectl apply -f -


You can also use envsubst when deploying.

e.g.

cat $app/deployment.yaml | envsubst | kubectl apply ...

It will replace all variables in the file with their values.We are successfully using this approach on our CI when deploying to multiple environments, also to inject the CI_TAG etc into the deployments.


I don't think it is possible to set image through variable or Config Map in Kubernetes. But you can use for example Helm to make your deployments much more flexible and configurable.