Updated November 30, 2023
Introduction to Kubernetes Environment Variables
Kubernetes environment variables are nothing but the variables that we can define in a Pod’s configuration and it can be used elsewhere in the configuration. We can define Kubernetes environment variables using a ConfigMap in Kubernetes to keep configuration artifacts separately and also make containerized application portable. We can use the ‘env’ or ‘envFrom’ field to set Kubernetes environment variables. It overrides any environment variables specified in the container image. We also have dependent environment variables. We can use $(VAR_NAME) in the value of env in a configuration file to set dependent environment variables.
Explanation: – In the above snapshot, we have two environment variables named ‘APP_VERSION’ and ‘ENVIRONMENT’ and specified under the ‘env’ field.
How environment variables work in Kubernetes?
We have to use the ‘env’ keyword in YAML files in which we want to declare environment variable, it might be pod YAML file or secret YAML file, etc. We have then specified the name of the environment variables and their values under the env keyword as shown in the above example. We have to use the ‘printenv’ to list the pod’s container environment variables. It lists all environment variables including variables specified explicitly in the YAML file. We can also reference environment variables from configMap as well.
Let’s understand with examples:
Create a pod with a few environment variables and print on STDOUT after the container is started.
Step: 1. First of all, we need to create a YAML file as shown below, here file name is demo-pod.yml: –
Step 2. Let’s create the pod using the above YAML file as shown below: –
$kubectl apply -f demo-pod.yml
Step 3. Now, check the status of the pod, it should be in running state, to check the status of the pod, run the below command, you will see the pod named ‘demo-pod’: –
$kubectl get pods
Step 4. Finally, let’s check the environment is available in the container or not. To check the status, we have to connect to the pod and execute ‘printenv’ command as shown below: –
$kubectl exec <POD_NAME> -- printenv $kubectl exec demo-pod -- printenv
Explanation: – In the above snapshot, we can see that the environment variables ‘APP_VERSION’ and ‘ENVIRONMENT’ mentioned in the yaml file are present in the container.
Create ConfigMap and reference the data of ConfigMap in the pod as environment variables.
Step 1. First thing first, let’s create two ConfigMaps using below yaml file named ‘demo-configmap.yml’: –
demo-configmap.yml $kubectl apply -f demo-configmap.yml
Step 2. Now, create a pod that is going to take reference from configMap for the value of the environment variable mentioned in the pod yaml file or container. Here file name is demo2-pod.yml: –
demo2-pod.yml $kubectl apply -f demo2-pod.yml
Explanation: In the above example, we can see that Kubernetes has created 2 configMap ‘env-config-file’ and ‘env-var-config’ and also created a pod named ‘demo2-pod’.
Step 3. Let’s go ahead and check the status of the pod and see if the value of the environment variables have been replaced successfully or not.
$kubectl get pods
Explanation: In the above snapshot, we can see that the status is not running but completed because the container has stopped after running the command mentioned in the ‘demo2-pod.yml’ file.
Step 4. So let’s go ahead and check the logs of the pod and see if environment variables are available or not with correct values: –
$kubectl logs <POD_NAME> (Note – It will only show the pods running in default namespace) $kubectl logs demo2-pod
Explanation: In the above snapshot, we can see that the value of environment variables has been assigned that is mentioned in the ConfigMap.
Pass secrets as container environment variables.
Step 1. In this scenario, first, we have to create a secret using the below yaml file: –
$kubectl apply -f test-secret.yml
Step 2. Next, create a pod using the below YAML file and as discussed earlier use the ‘envForm’ keyword to take the secret reference as shown in the below YAML file: –
$kubectl apply -f demo3-pod.yml
Step 3. Let’s check the status of the pod and environment variables of the container ran under that container: –
$kubectl get pods
$kubectl logs demo3-pod
Explanation: In the above snapshot, we can see that container has environment variables ‘PASSWORD’ and ‘USER_NAME’ and it has a value that is not visible as text as it is coming from Kubernetes secret.
- It is very useful to specify environment variables to containers while pod creation for example if we want to assign a tag to a container, etc.
- It helps to override the image variables in container if required.
- It also allows us to reference the value from ConfigMap or secrets.
Kubernetes environment variables provide all advantages that we get in normal environment variables in Linux or any application development. We have discussed the basic use of Kubernetes environment variables, however, we can make it complex by using dependent environment variables.
We hope that this EDUCBA information on “Kubernetes Environment Variables” was beneficial to you. You can view EDUCBA’s recommended articles for more information.