All ConfigMaps we created so far were done through kubectl create cm commands. Everything in Kubernetes can be defined as YAML, and that includes ConfigMaps as well.

Looking into the YAML#

Even though we have not yet specified ConfigMaps as YAML, we have seen the format quite a few times throughout this chapter. Since we cannot be sure whether you can create a ConfigMap YAML file from memory, let’s make things easy on ourselves and use kubectl to output our existing my-config ConfigMap in YAML format.

Get ConfigMap created earlier

The output is as follows.

Output of the above command

Just as with any other Kubernetes object, ConfigMap has apiVersion, kind, and metadata. The data is where the maps are defined. Each must have a key and a value. In this example, there’s the key weather with the value sunny.

Deploying Prometheus#

Let’s try to translate that knowledge into the objects we’d need to deploy Prometheus.

Looking into the definition#

The definition, of prometheus.yml limited to the relevant parts, is as follows.

Output of of `prometheus.yml`

Line 12-14: The Deployment object defines the volumeMounts that references the prom-conf Volume, which is a configMap. We saw quite a few similar examples before.

Line 25: The ConfigMap object’s data section has only one key (prometheus.yml). Once this ConfigMap is mounted as a volume, the name of the file will be the same as the key (prometheus.yml).

The value has a bit of “special” syntax. Unlike the previous example where the value was a single word written directly after the colon, the structure of the value is now a bit more complex. To be more precise, it contains multiple lines.

When working with a large value, we can start with the pipe sign (|). Kubernetes will interpret the value as “everything that follows, as long as it is indented.” You’ll notice that all the lines of the value are at least two spaces to the right of the beginning of the key (prometheus.yml). If you’d like to insert an additional key, all you’d need to do is to add it on the same level (indentation), as the other prometheus.yml.

Creating the application#

Let’s create the application and confirm that everything works as expected.

Create application

Destroying Everything#

For now, we’ll destroy the cluster we used in this chapter.

Delete cluster

Try it yourself#

A list of all the commands used in the lesson is given below.

Commands used in this Lesson

You can practice the commands in the following code playground by pressing the Run button and waiting for the cluster to set up.

/
prometheus.yml
Defining configMaps as YAML

Troubleshooting Tips for minikube#

If you were to create the deployment in minikube locally, you will need the following commands to set the appropriate IP:

Alternative commands for minikube

We created the objects (with the help of sed transformations), we waited until the Deployment rolled out, and, finally, we opened the Prometheus targets screen in a browser. The result should be a green target towards Prometheus’ internal metrics.

Converting ConfigMap Output into Environment Variables
A Plea NOT to Use ConfigMaps!
Mark as Completed
Report an Issue