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
env
keyword to supply a list of environment variables. - We are also using
valueFrom
keyword to get the value of the keyfoo1
in the ConfigMapmy-configmap
and 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
key1
andkey2
are set tovalue1
andvalue2
respectively. - Environment variables
foo1
is set tobar1
.
Clean Up¶
Assuming your folder structure looks like the one below:
Let's delete all the resources we created: