Injecting Configuration from a Single File
Learn to inject configuration from a single file using the ConfigMap Volume.
Creating a ConfigMap#
In its purest, and probably the most common form, a ConfigMap takes a single file. For example, we can create one from the prometheus-conf.yml
file.
We created a ConfigMap (cm
) called my-config
. The data of the map is the content of the prometheus-conf.yml
file.
Looking into the description#
Let’s describe it, and see what we’ll get.
The output is as follows.
The important part is located below Data
. We can see the key which, in this case, is the name of the file (prometheus-conf.yml
). Further down you can see the content of the file. If you look at the definition of prometheus-conf.yml
in the playground at the end of this lesson, you’ll see that it is the same as what we saw from the ConfigMap’s description.
Mounting the ConfigMap#
ConfigMap is useless by itself. It is yet another Volume that, like all the others, needs a mount.
Pod with mounted ConfigMap#
Let’s take a look at a Pod specification defined in alpine.yml
.
The essential sections are volumeMounts
and volumes
. Since volumeMounts
are the same no matter the type of the Volume, there’s nothing special about it. We defined that it should be based on the volume called config-vol
and that it should mount the path /etc/config
. The volumes
section uses configMap
as the type and, in this case, has a single item name
, that coincides with the name of the ConfigMap we created earlier.
Creating the pod#
Let’s create the Pod and see what happens.
Please confirm that the Pod is indeed running before moving on.
Verification#
Let’s see the content of the /etc/config
directory inside the Pod’s only container.
The output is as follows.
The /etc/config
now has a single file that coincides with the file we stored in the ConfigMap.
Let’s add -l
to the ls
command we executed a moment ago.
The output is as follows.
You’ll see that prometheus-conf.yml
is a link to ..data/prometheus-conf.yml
.
If you dig deeper, you’ll see that ..data
is also a link to the directory named from a timestamp. And so on, and so forth. For now, the exact logic behind all the links and the actual files is not of great importance. From the functional point of view, there is prometheus-conf.yml
, and our application can do whatever it needs to do with it.
Let’s confirm that the content of the file inside the container is indeed the same as the source file we used to create the ConfigMap.
The output should be the same as the contents of the prometheus-conf.yml
file.
We saw one combination of ConfigMap. Let’s see what else we can do with it.
Deleting the objects#
We’ll remove the objects we created thus far and start over.
The command to delete the ConfigMap is as follows.
We are not limited to a single --from-file
argument. We can specify as many as we need.
Try it yourself#
A list of all the commands used in the lesson is given below.
You can practice the commands in the following code playground by pressing the Run button and waiting for the cluster to set up:
/
- prometheus-conf.yml