How to Make the Most of Kubernetes Environment Variables – InApps is an article under the topic Software Development Many of you are most interested in today !! Today, let’s learn How to Make the Most of Kubernetes Environment Variables – InApps in today’s post !

Read more about How to Make the Most of Kubernetes Environment Variables – InApps at Wikipedia

You can find content about How to Make the Most of Kubernetes Environment Variables – InApps from the Wikipedia website

In traditional systems, environment variables play an important role, but not always a crucial one. Some applications make more use of environment variables than others. Some prefer configuration files over environment variables. However, when it comes to Kubernetes, environment variables are more important than you might think. It’s partially due to the way containers work in general and partially due to the specifics of Kubernetes. In this post, you’ll learn all about environment variables in Kubernetes.

Traditionally, environment variables are dynamic key-value variables that are accessible to any process running on the system.

The Basics

Let’s start with the basics. What are environment variables and why do they exist? Traditionally, environment variables are dynamic key-value variables that are accessible to any process running on the system. The operating system itself will set many environment variables that help running processes understand the specifics of the system. Thanks to this, software developers can include logic in their software that makes the programs adjustable to a specific operating system.

Environment variables also hold a lot of important information about the user, things like username, preferred language, user home directory path and many other useful bits of information.

User-Defined Environment Variables

As a user, you can easily create and access your own environment variables. On Unix-based systems, you can do that by executing the export command followed by the name of your variable and its value.

So, for example, to create an environment variable called myvar with a value of 10, you need to execute the following:

export myvar=10

You can then access the value of your variable using a dollar sign followed by your variable name.

In our case, to print (using Linux command echo) the value of our variable, we can execute the following:

echo $myvar

And if you want to print all the environment variables, you can execute either printenv or env commands.

All of this applies to applications running in your pods too.

Environment Variables in Kubernetes

The basic principle of environment variables in Kubernetes is the same. However, Kubernetes uses environment variables quite extensively and for a few different things. Therefore, it’s good to understand the role environment variables play in Kubernetes. That’s especially true if you want to migrate an existing application that doesn’t use environment variables that much. You can still create your pods without any environment variables. But if you ignore environment variables in Kubernetes completely, you may lose some of the value of the more powerful Kubernetes features.

Read More:   Open-Sourcing Swift is Not Genius, It’s Just Apple Once Again Winning Over Developers – InApps 2022

Before we move any further, you also need to know that it’s generally good practice when developing microservices to provide configuration to your Docker containers as environment variables whenever possible. This way you can make your Docker image more generic and possibly reuse the same image for different purposes. With that being said, let’s see how you can inject some environment variables into your Kubernetes pods.

The main use case for environment variables in Kubernetes is similar to the one from traditional software development.

Configuration for Your Pods

The main use case for environment variables in Kubernetes is similar to the one from traditional software development. That is to provide information about the environment to your software. This information is usually used to alter or adapt the way software works to the specifics of the environment. The definition may seem vague, so let me give you an example. Instead of having separate Docker images for your development and production environments, you can use the same image but run your application in a development or production mode based on an environment variable.

In Kubernetes, environment variables are scoped to a container, and there are three main ways of adding them. Let’s break them down.


The first option is the most straightforward. You can simply specify environment variables directly in your deployment definition with an env keyword:

As soon as your application is instructed to read the value of an environment variable called ENVIRONMENT, you can use it directly to run your application in the desired mode.

To run the same application in a production mode, you can simply reuse the same deployment definition. You’ll only need to change the environment variable value, and optionally a name of the pod:

Here’s another example: Imagine that you have a web application that needs to download a product catalog to be served to users. This catalog may differ in a few ways, for example, by a country, month or supplier. This is a perfect use case for an environment variable. Instead of creating many different versions of your application to accommodate different download options, your application can remain generic. Which catalog it has to download will be determined by the value of some specific environment variable.

Read More:   Cooking up Some Workflow – InApps 2022


Another way of providing environment variables to your application is by passing them from Kubernetes secrets. You may guess that this is a good option when you need to pass some sensitive information like passwords or tokens. This way you don’t specify the value of the environment variable directly in the deployment as we did before. Instead, you instruct Kubernetes to take the value of a specified secret object and use it as a value of an environment variable for your pod.

For example, if you have a Kubernetes secret like this:

As you can see, in our example we have username and password defined in Kubernetes secret, but we are only passing the password value to the pod. If you want to pass all the secrets from a Kubernetes secret without specifying each key, you can use secretRef instead of secretKeyRef. This way, you only need to specify the Kubernetes secret object name, and all the values from it will be automatically loaded as environment variables:


Another way of injecting environment variables into your pods is by using values from ConfigMaps. For example, if you have ConfigMap like this:

and you want to load both environment and timezone as environment variables into your pod, you can add the following valueFrom definition to your deployment:

In your pod, you’ll then be able to see both environment variables as defined in your ConfigMap:

As with secrets, if you don’t want to load all values from a ConfigMap, you can define specific keys instead by changing configMapRef to configMapKeyRef.

The main difference between passing environment variables from ConfigMaps and specifying them directly as in the first example is the fact that here the environment variable life cycle is separated from the pod life cycle. This means you can update the value of your variable independently from the running pod. Or, to put it differently, you’ll need to restart the pod yourself in order to load the new value of the environment variables into the pod. On the other hand, when you specify environment variables directly in the deployment, every change to the variables will automatically trigger pod restart.


Environment variables play an important role in Kubernetes. You can use them not only to provide basic information about the operating system to your application, but also as the main configuration mechanism for your pods or for passing sensitive information. It’s not uncommon in Kubernetes to extract as much configuration as possible as info ConfigMaps and environment variables to keep your Docker images as generic as possible. As you can see, even something simple like environment variables have a few options in Kubernetes. If you want to learn more, Release founding engineer Regis Wilson wrote about why Kubernetes is hard and what you can do about it.

InApps is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.

Feature image via Pixabay


Rate this post
As a Senior Tech Enthusiast, I bring a decade of experience to the realm of tech writing, blending deep industry knowledge with a passion for storytelling. With expertise in software development to emerging tech trends like AI and IoT—my articles not only inform but also inspire. My journey in tech writing has been marked by a commitment to accuracy, clarity, and engaging storytelling, making me a trusted voice in the tech community.

Let’s create the next big thing together!

Coming together is a beginning. Keeping together is progress. Working together is success.

Let’s talk

Get a custom Proposal

Please fill in your information and your need to get a suitable solution.

    You need to enter your email to download


      Success. Downloading...