How to override env variables in React JS application with Kuberenetes?

Viewed 542

I've a simle React JS application and it's using a environment variable(REACT_APP_BACKEND_SERVER_URL) defined in .env file. Now I'm trying to deploy this application to minikube using Kubernetes.

This is my deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test-ui
  template:
    metadata:
      name: test-ui-pod
      labels:
        app: test-ui
    spec:
      containers:
        - name: test-ui
          image: test-ui:1.0.2
          ports:
            - containerPort: 80
          env:
            - name: "REACT_APP_BACKEND_SERVER_URL"
              value: "http://127.0.0.1:59058"

When I run the application, it's working but REACT_APP_BACKEND_SERVER_URL is giving the value which I defined in .env file. Not the one I'm overriding. Can someone help me with this please? How to override the env variable using Kubernetes deployment?

1 Answers

After starting the app with your deployment YAML and checking for the environment variables I see the environment variables for that environment variable.

REACT_APP_BACKEND_SERVER_URL=http://127.0.0.1:59058

you can check that by doing an kubectl exec -it <pod-name> -- sh and running env command.

So you can see that REACT_APP_BACKEND_SERVER_URL is there in the environment variables. It's available for your application to use. I suspect that you may need to understand better from the React app side on how to use the .env file.

Related