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

<p>Normally we put a marker text (e.g. <code>{{IMAGE_NAME}}</code>) in the yaml file and use a bash text manipulation tools to change it with actual value in the deployment file.</p>

<p>Is there a way to use environment variables or other methods (like using arguments when running <code>kubectl create</code>) instead of text-replace tools?</p>

<p>What I've done is use envvars in the deployment configuration, then run <code>apply</code>/<code>create</code> with the output from an <a href="https://linux.die.net/man/1/envsubst" rel="nofollow noreferrer"><code>envsubst</code></a> command:</p>

<p>deployment.yaml file:</p>

<pre><code>[...]
spec:
  replicas: $REPLICA_COUNT
  revisionHistoryLimit: $HISTORY_LIM
[...]
</code></pre>

<p>during deploy:</p>

<pre><code>$ export REPLICA_COUNT=10 HISTORY_LIM=10
$ envsubst &lt; deployment.yaml | kubectl apply -f -
</code></pre>
