Set Environment Variables Inside a Kubernetes Pod

In this post, we are going to show you how to set environment variables inside a Kubernetes pod.

Environment variables can be set at different layers, such as the PodSpec and the Container spec. The environment variables allow a user to configure a container without mounting a file from within the container or from outside of it.

Users and containers within a pod can access these environment variables.

Set Environment Variables

In the code below, I’m deploying a pod and passing one environment variable to pass more than one simply add another variable (–env).

kubectl run -i -t ubuntu --image=ubuntu:latest  --env="Myvar=value1"

Once the pod has been deploying I can access the variable and confirm that it has been set by using the line below inside the pod.

echo $Myvar