Is it possible to configure React application to use container's environment variables in Kubernetes?

1/15/2020

To begin with - let us suppose we have a React application. We want to build it and deploy to 3 environments - dev, test and production. As every front-end app it needs to call some APIs. API addresses will vary between the environments. So they should be stored as environment variables.

As every modern, progressive developer we want to use containers. In particular Kubernetes.

We want to build our web application and deploy it on K8S cluster. The container image should be built and kind of sealed for changes, then before deployment to each particular environment the variables should be injected.

But it seems there's one great impossibility here. When it comes to .NET apps for example, when we have .dll compiled, it reads a config file in the runtime. It's not a case with React. After we generate build we have just static files. The variables are changed to static values in the process of building React app. It seems there's no way to update it after that point - or is it?

-- Landeeyo
containers
docker
kubernetes
reactjs

2 Answers

1/15/2020

You can use config map to store the API endpoint address and refer it as environment variable in the pod.

If you want to change some values while the pod is running you can mount the config map and any change to it will be synced in the pod

-- Arghya Sadhu
Source: StackOverflow

1/15/2020

The way you are trying to solve your problem is not correct.

You don't need to know anything about the addresses of the backend services in your react app. Only the frontend server/gateway that is serving your react app needs to know about the backend services. Any request from the react app should be proxied via the gateway.

See API gateway pattern - https://microservices.io/patterns/apigateway.html

-- Shashank V
Source: StackOverflow