Supply Environment Variables From Multiple Sources¶
What if you want to use ConfigMap for the environemnt variables but also want to supply additional environment variables directly in the pod definition?
Let's see how we can do that.
Step 1: Create a ConfigMap¶
Apply the manifest to create ConfigMap:
Step 2: Verify ConfigMap¶
Step 3: Create Pods That Uses Environment Variables¶
Let's create pods that uses ConfigMap as well as the conventional approach to set environment variables for the container. We'll use a deployment to create pods.
Observe the following:
- We are using
envkeyword to supply a list of environment variables - We are also using
envFromkeyword to supply a list of environment variables from the ConfigMapmy-configmap
Apply the manifest to create deployment:
Step 4: Verify Deployment and Pods¶
Step 5: 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 (using env and envFrom keyword) 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
# Print value of the environment variable foo1
echo $foo1
# Print value of the environment variable foo2
echo $foo2
You'll notice the following:
- Environment variables
key1andkey2are set tovalue1andvalue2respectively. - Environment variables
foo1andfoo2are set tobar1andbar2respectively.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created: