Use ConfigMap to Supply Environment Variables¶
Let's see how we can use ConfigMap to supply environment variables to containers running inside in a pod.
Step 1: Create a ConfigMap¶
Let's create a ConfigMap
with data that contains the required environment variables.
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
to set environment variables for the container. We'll use a deployment to create pods:
Observe that we are using the keyword envFrom
to supply a list of environment variables from the ConfigMap my-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
environment variables.
Print values of the environment variables we set:
# Print value of the environment variable foo1
echo $foo1
# Print value of the environment variable foo2
echo $foo2
You'll notice the environment variables foo1
and foo2
are set to bar1
and bar2
respectively.
Now, what if you want to use ConfigMap
for the environemnt variables but also want to supply additional environment variables directly in the pod definition?
In that case, you can use both the env
and envFrom
field. 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: