<p>Is it possible write write an existing environment variable into a file from a Kubernetes deployment.yaml file?</p>
<p>The background: I've already parsed a json containing secrets. Now, I'd like to store that secret in a local file.</p>
<p>So far, I've tried something like this:</p>
<pre><code>      lifecycle:
        postStart:
          exec:
            command: [&quot;/bin/sh&quot;, &quot;-c&quot;],
            args: [&quot;echo $PRIVATE_KEY &gt; /var/private.key&quot;] 
</code></pre>
<p>( I've setup /var/ as an empty writeVolume. )</p>
<p>Or perhaps there is a completely different way to do this, such as storing the secret in it's own, separate secret?</p>

Charlie Dalsass
<p>Usually when we need to read some secrets from a secret manager, we use an init container, and we create an <code>emptyDir</code> shared between the pods to write the secrets and access them from the other containers. In this case you can use a different docker image with secret manager dependencies and creds, without install those dependencies and provide the creds to the main container:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  initContainers:
  - name: init-container
    image: alpine
    command:
    - /bin/sh
    - -c
    - 'echo &quot;test_value&quot; &gt; /mnt/volume/var.txt'
    volumeMounts:
    - mountPath: /mnt/volume
      name: shared-storage
  containers:
  - image: alpine
    name: test-container
    command:
    - /bin/sh
    - -c
    - 'READ_VAR=$(cat /mnt/volume/var.txt) &amp;&amp; echo &quot;main_container: ${READ_VAR}&quot;'
    volumeMounts:
    - mountPath: /mnt/volume
      name: shared-storage
  volumes:
  - name: shared-storage
    emptyDir: {}
</code></pre>
<p>Here is the log:</p>
<pre class="lang-bash prettyprint-override"><code>$ kubectl logs test-pd
main_container: test_value
</code></pre>
