Forwarding Environment Variables in the Cloud

Latiif alSharif
3 min readDec 5, 2019
Containers working in concert are the building blocks of cloud computing

Developing modern software often requires making sure it works in different environments during the different stages of development. Each with its own set of external and internal components; for instance running it on your local machine vs staging server vs production server. One tool to configure such parameters is environment variables.

In this article, we will discuss step-by-step how you can avoid hard coding constant values in your code when developing for the cloud.

Reading in the environment variable in the code

The implementation of this step depends on your programming language/framework. For this article Go is used, as it provides a compact native way to create a server.

Simple HTTP server that uses an environment variable to greet its users

Start the program by running

go run server.go

This will start a server listening on port 8080 that we can test with

curl localhost:8080/John

Which will print out “Hello, John!” because the environment variable GREETING is not defined, but if you run the server with

GREETING=Hola go run server.go

the response you get will be “Hola, John!” because the program found GREETING in your environment.

The program reads the value of the environment variable from the operating system

The next step is to containerize our program into a Docker image.

docker build -t goserver .
Basic two-step dockerfile with minimal image size

Once the image goserver is built, you can run the container and pass it the environment variable

docker run -p 8080:8080 -e GREETING=Salut goserver

By port mapping, we can test the container as in the previous step to get a French greeting “Salut, John!”

Docker makes environment variables available in the OS of the container

The next step is to create a Kubernetes pod resource to run the container.

(Note that in order to use the image in a remote Kubernetes cluster, you have to push it to a registry e.g. Docker Hub . We will be using Minikube with the local docker registry on our machine)

Pod resource definition file for goserver

In lines 14 and 15, we define the name and value of the environment variable that Kubernetes will create and make it available in the Docker container.

Kubernetes passes the value of environment variables down to Docker image

We create the pod in Kubernetes by running the following command:

kubectl -f goserver-pod.yaml apply

Once the pod is running, we can test it by port forwarding

kubectl port-forward goserver-pod 8080:8080

And by running the same curl command again we get

Server responded with the greeting specified in the pod definition file

We can see how the environment variable GREETING was passed from Kubernetes Pod → Docker Container → Operating System → Program

Summary

The true benefit of environment variables lies in the fact that they are used to decouple components from each other allowing for effortless and on-demand scaling and adjustment of configuration parameters such as the IP addresses and ports to your development database versus your production one.

By passing different IP addresses as environment variables, the coupling is easily changeable

--

--