Using Kubernetes ConfigMaps and Docker Env Variables in FE projects

Ahmad Mhaish
The Startup

--

After moving to Microservices architect, and by using a framework like Kubernetes, the need to store all configuration and variables in an easy to access place appears. And that is the main benefit of Kubernetes ConfigMaps and Secrets.

These two objects let you store and manage in order your configurations and sensitive data. Like integration endpoints, hidden/shown features, passwords, tokens, ssh keys, …etc.

The real magic comes when you can use these values in your Kubernetes service deployment file where you are defining the Docker instance and environment parameters, which means that these parameters can be passed as environment parameters to any service in the mesh.

One of the challenges is to find a way to pass these parameters to a front-end project either if it's a pure JS project or written in one of the famous platforms now (React, Angular and Vue). We will talk next about configuring Kubernetes ConfigMaps, using them in services, and the trick to using these variables in FE project.

Defining ConfigMap/Secret in K8

K8s secrets are all about that Base64 encoding. Kubernetes does this to avoid accidentally exposing secrets.

As you can find in K8 official website about the secrets:

To use a secret, a pod needs to reference the secret. A secret can be used with a pod in two ways: as files in a volume mounted on one or more of its containers, or used by kubelet when pulling images for the pod.

Secrets can be created using kubectl command directly like the example in the previous website, or the best way always by using yaml file, so we store them in a repository as the first step, and maybe setup a CI/CD pipeline for them.

Next is an example using yaml file to create the secrets:

apiVersion: v1
kind: Secret
metadata:
name: my-secrets # The name of the secret which can contain a group of variables
type: Opaque
data:
integrations.url: aHR0cDovL2ludGVncmF0aW9uLnVybA== # The name fo the variable: the value of that variable in base64 format

Thankfully though, encoding in base64 is pretty easy. Just run the following command with your…

--

--

Ahmad Mhaish
The Startup

I am a Software and AI engineer that have a great passion for integrating technology with businesses and human life.