Conventional Approach of Supplying Environment Variables¶
Let's look at the conventional approach of supplying environment variables to containers running inside a pod.
Step 1: Create Pods That Uses Environment Variables¶
Let's create pods that uses environment variables. We'll use a deployment to create pods:
Observe the following:
- We are using the keyword
env
to supply a list of environment variables to thenginx
container - In this example, we have supplied two environment variables;
key1
, andkey2
, with valuesvalue1
andvalue2
, respectively.
Apply the manifest to create deployment:
Step 2: Verify Deployment and Pods¶
Step 3: Verify Environment Variables¶
Start a shell session inside the container:
List environment variables available to the container:
You'll see a list of environment variables available to the container. This includes both system-provided
as well as user-provided
environment variables.
Print values of the environment variables we set:
# Print value of the environment variable key1
echo $key1
# Print value of the environment variable key2
echo $key2
You'll notice the environment variables key1
and key2
are set to value1
and value2
respectively.
This method of supplying environment variables is acceptable if you have only a few of them. But, what if you have a significant number of environment variables? Adding all of them in the manifest file will needlessly bloat the manifest file.
In that case, you can use ConfigMap
to supply a list of environment variables to containers in a pod. We'll explore this in the next section.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created: