How to access environment variables from Kubernetes pod in React/Node.js
P粉790819727
P粉790819727 2023-09-05 09:03:09
0
1
571
<p>We are defining some environment variables in kubernetes pod and when I try to use them in node or React FE code using process.env.TEST (because TEST exists in env as secret), I always get undefined, But when I see there are variables on the pod. </p> <p>Is there any other way to access these variables or do we need to do something explicitly on Node.js or React.js. </p>
P粉790819727
P粉790819727

reply all(1)
P粉459440991

Environment variables in a Kubernetes Pod can be accessed in Node.js using ‍process.env., similar to how they are accessed in any Node.js application. You're doing it the right way, so if the value is undefined, something might not be set correctly.

apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password

React Environment Variables: If you are trying to use environment variables in your React application, you need to prefix them with REACT_APP_. Only environment variables starting with this prefix will be embedded in the build. Therefore, you will use process.env.REACT_APP_ to access them in your code.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template