<p>I'm currently working on a Kubernetes deployment file and I need to set the environment variables that the container will have.</p>
<p>Is there a way to set Kubernetes secrets as environment variables in the deployment file?</p>
<p>Here is a dummy file to help explain:</p>
<p><a href="https://i.stack.imgur.com/mv4Rr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mv4Rr.png" alt="dummy deployment file" /></a></p>

<p>The syntax that worked for me was:</p>
<pre><code>spec:
  ...
  template:
    ...
    spec
      ...
      containers:
          env:
            - name: PORT
              valueFrom:
                configMapKeyRef:
                  name: env-configmap
                  key: PORT
            - name: ELASTIC_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: env-secret
                  key: ELASTIC_PASSWORD

</code></pre>
<p>The other syntax may also work and be acceptable, I'm just offering this answer as another alternative syntax for <em>mapping</em> Kubernetes configuration and secret values into environment variables.</p>
<p>When I checked the <a href="https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables" rel="nofollow noreferrer">URL referenced in the answer from @Valentin BOUSSON</a>.
The page now (2023-05-16) says:</p>
<blockquote>
<p>To use a Secret in an environment variable in a Pod:</p>
<p>For each container in your Pod specification, add an environment variable for each Secret key that you want to use to the env[].valueFrom.secretKeyRef field.
Modify your image and/or command line so that the program looks for values in the specified environment variables.</p>
</blockquote>
