skip to Main Content

We’re defining some environment variables in kubernetes pods and when i try to use them in either node or React FE code using process.env.TEST(as TEST is present in env as secrets), i always got undefined but when i see variables on pods it was there.

Is there any other way to access those variables or we need to do something explicitly either on Node.js or React.js.

2

Answers


  1. Environment variables in a Kubernetes pod can be accessed in Node.js using ‍process.env.<VARIABLE_NAME>‍, similar to how you’d access them in any Node.js application. You are doing it the right way, so if the values are undefined, then something might not be set up 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’re trying to use the environment variables in a React app, they need to be prefixed with REACT_APP_. Only environment variables that start with this prefix will be embedded into the build. So you’ll access them in your code using process.env.REACT_APP_<VARIABLE_NAME>.

    Login or Signup to reply.
  2. The secrets for frontend service like react, secrets must to be passed during the build time i.e while building the docker image

    we build react apps and also call secrets from the vault during build time and it works.

    pipeline steps for frontend apps should be :

    1. download secrets from the vault(secret manager)
    2. build docker image
    3. Deployment

    hope this helps you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search