<p>I have a create-react-app with default configurations.
I have some PORT and APIs inside <strong>.env file</strong> configured with</p>
<pre><code>REACT_APP_PORT=3000
</code></pre>
<p>and using inside app with process.env.REACT_APP_PORT.</p>
<p>I have my server deployed on Kubernetes.
Can someone explain how to configure my create-react-app, to use environment variables provided by pods/containers?</p>
<p>I want to access cluster IP via Name given by kubectl svc</p>
<p><strong>Update 1 :</strong></p>
<p>I have the opposite scenario, I don't want my frontend env variables to be configured in kubernetes pod container, but want to use the pod's env variable</p>
<p>e.x CLUSTER_IP and CLUSTER_PORT with their name defined by pod's env variable inside my react app.</p>
<p>For eg.-</p>
<pre><code>NAME TYPE CLUSTER-IP 

XYZ ClusterIP x.y.z.a
</code></pre>
<p>and want to access XYZ in react app to point to the Cluster IP (x.y.z.a)</p>

Sunny
<p>Use Pod fields as values for environment variables</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: dapi-envars-fieldref
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ &quot;sh&quot;, &quot;-c&quot;]
      args:
      - while true; do
          echo -en '\n';
          printenv MY_NODE_NAME MY_POD_NAME MY_POD_NAMESPACE;
          printenv MY_POD_IP MY_POD_SERVICE_ACCOUNT;
          sleep 10;
        done;
      env:
        - name: MY_NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: MY_POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: MY_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: MY_POD_SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
  restartPolicy: Never
</code></pre>
<p><a href="https://kubernetes.io/docs/tasks/inject-data-application/_print/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/inject-data-application/_print/</a>
Maybe above example will help you.</p>
