<p>How do I provide a .env file in Kubernetes. I am using a Node.JS package that populates my process.env via my .env file.</p>

Samuel Corsi-House
<p>You can do it in two ways:</p>
<ol>
<li>Providing env variable for the container:
During creation of a pod, you can set environment variables for the containers that run in that Pod. To set environment variables, include the <code>env</code> field in the configuration file.</li>
</ol>
<p>ex:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: &quot;Hello from the environment&quot;
    - name: DEMO_FAREWELL
      value: &quot;Such a sweet sorrow&quot;
</code></pre>
<ol start="2">
<li>Using ConfigMaps:
first you need to create a ConfigMaps, ex is below, here <code>data</code> field refers your values in a key-value pair.</li>
</ol>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
</code></pre>
<p>Now, use <code>envFrom</code> to define all of the ConfigMap's data as container environment variables, ex:</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ &quot;/bin/sh&quot;, &quot;-c&quot;, &quot;env&quot; ]
      envFrom:
      - configMapRef:
          name: special-config
  restartPolicy: Never
</code></pre>
<p>you can even specify individual field by giving <code>env</code> like below:</p>
<pre><code>      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_LEVEL
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_TYPE
</code></pre>
<p>Ref: <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data" rel="noreferrer">configmap</a> and <a href="https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/" rel="noreferrer">env set</a></p>
