Supply Only Specific Environment Variables From ConfigMap¶
Consider a case where you want to use only a specific items from ConfigMap as environment variables instead of supplying entire ConfigMap as environment variables.
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 specific items from the ConfigMap as 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
valueFromkeyword to get the value of the keyfoo1in the ConfigMapmy-configmapand then setting it as the value of an environment variable namedfoo1.
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 valueFrom 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
You'll notice the following:
- Environment variables
key1andkey2are set tovalue1andvalue2respectively. - Environment variables
foo1is set tobar1.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created: