An Informative Guide to Kubernetes Environment Variables

Photo of author

By admin

The application of Kubernetes as one of the seamless orchestration platforms is trending. If you’re seeking a decent orchestration platform to complete your Linux projects, you must try Kubernetes. The best thing about this orchestration platform is its utility and customizable features. With Kubernetes, you get the opportunity to create environment variables that allow you to deploy and run your pods more precisely. Can you find another platform that offers equivalent customizable user experiences? Probably, no.

Anyway, if you’re new to Kubernetes, the concept of environment variables may go above your head. Every new Kubernetes user needs some time to adapt to the features and facilities associated with Kubernetes. Initially, you won’t need to create environment variables in your Kubernetes clusters to complete your tasks. To be specific, the main purpose behind creating environment variables is to make the platform more convenient and make your Kubernetes pods work exactly as you want. As you gain expertise in using Kubernetes, creating environment variables in your clusters will be the best way to make the most out of your pods.

In this article, you will find a detailed guide to Kubernetes environment variables and their functions. So, let’s get started with an introduction to Kubernetes environment variables.

Kubernetes Environment Variables

As a Kubernetes enthusiast, you must have been familiar with the usage of pods in Kubernetes. In short, pods are the units that help you accomplish your tasks in Kubernetes. Now, when you create pods in containers, you’re supposed to create a friendly running environment for the pods to help them run seamlessly. Such environments are called environment variables as you get the chance to create an environment variable according to your preference while creating pods in containers.

How to Manage Environment Variables?

Being a beginner, the task of managing environment variables may seem complicated to you. Ideally, an environment variable is utilized in terms of injecting specific configuration data to run an application with Kubernetes. Therefore, if you want to run specific applications and programs with your Kubernetes cluster, you are bound to inject certain configuration data to run them. Now, how do you inject environment variables into your container runtimes? There are three usual methods that you can utilize to set environment variables during creating pods. Here are the options:

  1. Utilizing the env stanza is possibly the most effortless way to set an environment variable while creating a pod inside a container.
  2. If you want the process to be convenient, utilize configMap as a reliable option.
  3. It’s best to use secrets while injecting environment variables to secure passwords and other sensitive information pieces.

Now, you may be keen to speculate the application of these methods in terms of injecting environment variables in the runtime environment of a specific container. The following example executes the injection of different environment variables using the env stanza and the configMaps.

$ kubectl exec -it app — bash

bash-5.0# env

— output truncated —

LOG_LEVEL=info

channel=mychannel

slackConfig={“webhook”:”https://hooks.slack.com/services/T00/B000/XXX”}

— output truncated –

However, we suggest you use the env stanza if you’re about to inject some simple variables into your container runtime. There’s no point in going for complicated methods like using configMap or secrets in such cases.

How to Define Environment Variables for a Specific Container?

Inclusion of the env or envForm in the configuration file is the initial step to set environment variables for a container. The following example describes the process of defining an environment variable for a single-pod container. The name of the environment variable in the pod’s configuration file is DEMO_GREETING, and the value of the environment variable is “Hello from the environment”. Now, let’s have a glance at the pod’s configuration manifest-

apiVersion: v1

kind: Pod

metadata:

name: envar-demo

labels:

purpose: demonstrate-envars

spec:

containers:

name: envar-demo-container

image: gcr.io/google-samples/node-hello:1.0

env:

name: DEMO_GREETING

value: “Hello from the environment”

name: DEMO_FAREWELL

value: “Such a sweet sorrow”

When you’ve created the configuration manifest, execute the following steps to complete the process.

  • You’re supposed to create the pod according to the configuration manifest you’ve created. Check out the following example snippet. Here we have created the pod based on the configuration manifest mentioned above.

kubectl apply -f https://k8s.io/examples/pods/inject/envars.yaml

  • As you’re done with creating pods, access the list of running pods using the following code.

kubectl get pods -l purpose=demonstrate-envars

You’re supposed to get the following output on your screen:

NAME READY STATUS RESTARTS AGE

envar-demo 1/1 Running 0 9s

  • Now, it’s time to list out the pod’s container environment variables. Use the following code to make that possible conveniently.

kubectl exec envar-demo — printenv

The following output is supposed to appear on your screen:

NODE_VERSION=4.4.2

EXAMPLE_SERVICE_PORT_8080_TCP_ADDR=10.3.245.237

HOSTNAME=envar-demo

DEMO_GREETING=Hello from the environment

DEMO_FAREWELL=Such a sweet sorrow

That’s all! You’ve successfully set the container variables for your pods. Now, you’re all set to inject specific configurations to run all types of applications or programs on your Kubernetes cluster.

When to Refrain from Using Environment Variables?

Though environment variables are completely safe and friendly ones of Kubernetes pods, sometimes it’s wise to refrain from using them. Why? Well, environment variables are applicable for different interfaces and operating systems. Environment variables can be set in containers, pods, operating systems, and programming languages. Therefore, you track down the origin of an environment variable when that has been defined at multiple surfaces. In such a condition, you may remain confused about where to set the environment variable to get the best outcomes.

Another question that may appear in your mind is whether the environment variable will work on the runtime environment of your pod or not. If you’re confused about the origin of an environment variable, avoid using that in your container.

Moreover, there’s no way to change an environment variable if the container of the variable has started running. You’re supposed to set the environment variable before deploying the container to run an application. If you feel like changing the environment variable in the middle of the process, you need to start over the process again. If you’re short of time and you need to run an application badly, consider overruling the usage of the environment variable.

Conclusion

This article has explained everything about environment variables to you. You must have understood the potential utilities of environment variables in terms of running containers and pods in smoother ways. However, the potential drawbacks of environment variables can’t be overlooked at all. Therefore, we have discussed the drawbacks of environment variables elaborately too. Now, it’s your turn to put your knowledge to work. Use environment variables to enhance the performances of your pods and containers on your Kubernetes cluster.

Leave a Comment