search

Home  >  Q&A  >  body text

How to access environment variables from Kubernetes pod in React/Node.js

<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粉790819727P粉790819727435 days ago563

reply all(1)I'll reply

  • P粉459440991

    P粉4594409912023-09-06 00:51:54

    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.

    reply
    0
  • Cancelreply