Configure Environment Variables

Configure Environment Variables

In this tutorial, we will discuss about how to configure environment variables in Kubernetes.

Environment variables define the external state of deployed services. These are particularly important because production grade applications do not run in single environments.

Applications deployed in these environments do not tend to work with the exact same configurations. Environment variables provide a way to specify parameters for deployed services that vary from environment to environment.

An example environment setup could have development, staging and production environments:

Development

The development environment would be the first line of defense against bugs. Here, developers deploy their code and test any newly implemented features.

Any bugs found are dealt with before re-deploying for further testing. The process is iterated until the code is ready for the next stage of testing.

Staging

Once developers are satisfied with their code and consider it fairly stable, it is then deployed to the staging environment for further testing. This is where Quality Assurance (QA) is performed.

Testers access the staging servers and ensure that the application works as it should. They run test cases to detect bugs and run performance tests to find areas that could be improved.

Any bugs or enhancements are reported back to the developers and the process is repeated until the code passes the staging phase.

Production

Once the code has been thoroughly tested, it is then pushed to production where it is made available to end-users.

Kubernetes allows users to provide environment variables when defining Pods. A Pod definition generally looks like as following

apiVersion: v1
kind: Pod
metadata:
  name: my-web-app
spec:
  containers:
  - name: my-web-app-container
    image: my-web-app
    ports:
      - containerPort: 8080
    env:
      - name: APP_COLOR
        value: pink

Here env is an array. So every item under the env property start with a dash, indicating an item in the array. Each item has a name and a value property.

The name is the name of the environment variable made available within the container and the value is its value.

What we just discussed was a direct way of specifying the environment variables using a plain key value pair format.

However there are other ways of setting the environment variables such as using config maps and secrets.

Configure Environment Variables

The main different in this case is that instead of specifying value, we say valueFrom. And then a specification of configMap or secret.

We will discuss about configMaps and secrets in the next tutorials.

Configure Environment Variables
Scroll to top