How to refer to variables in a Kubernetes deployment file?

9/20/2017

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?

-- mahdix
kubernetes

2 Answers

9/20/2017

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 -
-- Keyan P
Source: StackOverflow

9/20/2017

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.

-- Janos Lenart
Source: StackOverflow