Sometimes there are variables in the deployment yaml file which are not pre-specified and will be known only during deployment (For example name and tag for the image of a container).
Normally we put a marker text (e.g. {{IMAGE_NAME}}
) in the yaml file and use a bash text manipulation tools to change it with actual value in the deployment file.
Is there a way to use environment variables or other methods (like using arguments when running kubectl create
) instead of text-replace tools?
What I've done is use envvars in the deployment configuration, then run apply
/create
with the output from an envsubst
command:
deployment.yaml file:
[...]
spec:
replicas: $REPLICA_COUNT
revisionHistoryLimit: $HISTORY_LIM
[...]
during deploy:
$ export REPLICA_COUNT=10 HISTORY_LIM=10
$ envsubst < deployment.yaml | kubectl apply -f -
Unfortunately there is no way to use environment variables directly with kubectl. The common solution is to use some kind of a templating language + processing as you suggested.