• 16 September 2024

The cloud container is a cloud computing model in which the service provider, by creating hardware facilities and some tools, increases the speed of software development and solves the developer’s concern regarding the required infrastructure of the product.

In this article, we examine Configmap’s concept and how to use it, its types and components of a Configmap and its definition.

Prerequisites

The only prerequisite for this system is having an ArvanCloud user account and access to the cloud container in the user panel. After logging in to the user account, go to the profile section, create a new API KEY for yourself in the API KEYS tab, and save it somewhere.

You need to use ArvanCloud CLI to perform the steps in this article. Download this CLI using this link (put it in your PATH if required) and login through the command line:

Then put the API KEY you received from the site in its continuation.

What is Configmap?

Many programs and services require settings that are received through files or environment variables and are configured through them. With the help of Configmap, these settings can be made independent through the container to be used in different environments.

Configmap is very similar to the Secret concept. Typically, Secret stores sensitive passwords and tokens, while Configmap is used for application or service-related configurations.

How to Create Configmap

To create Configmap, you need to enter the required information in yaml format in a file and then submit it to the ArvanCloud cloud container through the command line. In the following, we will examine a simple example of a Configmap with the data required to connect to a MySQL database and its concepts.

Note: indentation is vital in yaml files, and the slightest shift can cause an error or unwanted settings to be returned.

The relevant fields are explained below.

kind: specifies the identity type. For example, this field can have Pod, Configmap, Service StatefulSet, etc. In this example, the target is the Configmap definition, so this value is specified.

metadata.name: Specifies the name of the Configmap.

data: In general, the data inside the Configmap should be presented as key: value. In the example above, two keys named “ui” and “system” are defined. Note that the value related to Keys can be simple (such as ui) or contain several lines. In this case, with |, It turns out that the value has multiple lines.

Enter and save the above lines in a file called configmap.yaml. Then submit your Configmap to the ArvanCloud cloud container through the command line with the following command.

Now, with the following command, you can see your Configmap’s status and execution on the ArvanCloud cloud container.

The output will be similar to the following:

To view your Configmap, type the following command:

The output will be as shown below:

Creating Configmap with Command Line

To create a Configmap, the easiest way is to use the command line and the “arvan paas create configmap” command, an example of which is given below.

The above command creates a Configmap named some-config. In addition, the above command can assign the values to the Configmap using the –from-literal option and as key=value.

You can also use the –from-file instead of the –from-literal option, in which case the file’s values are set as value and the file name as the key.

The above command creates a Secret based on the contents of the game.properties file. You can see the content of Secret below.

Using Configmap

Below is an example of a sample Deployment for using Configmap. The required information is given to Pod by Configmap through env.

In the following, the fields added in Deployment to use the created Configmaps are explained.

spec.template.spec.containers.env.valueFrom: This section specifies that the value of the environment variable is to be read from the configmap.

spec.template.spec.containers.env.valueFrom.configMapKeyRef.key: This section specifies the name of the key in the configmap that the corresponding Value environment variable should use.

spec.template.spec.containers.env.valueFrom.configMapKeyRef.name: This section specifies the name of the Configmap that the environment variable is to use.

In addition to env, Configmap can be used as a volume inside the Pod and “mounted.” This method of use is briefly explained below.

Using Configmap as a Volume

As mentioned, another way to use Configmap is to use it as a volume and mount it inside a pod. Among the advantages of this method is the possibility of using Configmap to provide Configs or files containing keys to Pod.

To use Configmap in this way, just define a Volume for Pod and then provide the Configmap name as its disk. Here is an example of this type:

Suppose you want to mount the Configs content defined in the previous sections in another Deployment and on the same path. This is done as in the example below.

In the following, the fields added in Deployment to use Configmap as a volume are explained.

spec.template.spec.containers.volumeMount: In this section, the name and location where the Configmap is to be mounted are specified. This section is defined similarly to other Volumes used so far.

spec.template.spec.volumes: In this section, the desired Configmap specifications are provided.

spec.template.spec.volumes.name: specifies the Volume on which the desired Configmap is supposed to be mounted.

spec.template.spec.volumes.configmap: Specifies on which Volume the Configmap terms are to be mounted.

spec.template.spec.volumes.configmap.name: Specifies the name of the Configmap to use.

After running the above file, if you enter the Pod and go to the designated path, you will see that each of the fields defined in the Configmap is placed as a separate file in the desired path.

For more information, you can refer to the OKD and Kubernetes documentation.