How do I create and use ConfigMap in Kubernetes? Step-by-step guide

Introduce

ConfigMaps are a useful Kubernetes feature that allows you to maintain lightweight, portable images by separating configuration settings.

Using small, layered images is one of the best practices for building efficient Kubernetes clusters. Using ConfigMaps can help you achieve this goal.

In this tutorial, you will learn how to create and use a ConfigMap, including Kubernetes examples of creating and using a ConfigMap.

Prerequisites

  •  Access user accounts using sudo or root privileges
  • Access the command line/terminal window ( Ctrl  –  Alt  –  T )
  • Kubernetes platform installed

What is ConfigMap, and what is it used for?

ConfigMap is an API that stores configuration data in key-value pairs. Their main function is to separate configuration from container images. It can represent an entire profile or a single property.

If you are using Kubernetes, you want to keep your images lightweight and portable. To do this, you should separate configuration settings from the application code. Using ConfigMaps, you can add different configuration data to pods to suit the environment in which they run.

For example, you can use the same code with different configurations during the development, testing, or production phases.

Note: Please note that ConfigMap is not encrypted and should not be used for sensitive information. To store and manage sensitive information, use Kubernetes Secrets.

How to create a ConfigMap?

You can create ConfigMap from files, directories, and literal values.

How do I create and use ConfigMap in Kubernetes? The basic syntax for creating a ConfigMap is:

kubectl create configmap [configmap_name] [attribute] [source]

Depending on the source, the properties will be:

  • –from file (if the source is a file or directory)
  • --from-literal (if the source is a key-value pair)

Option 1: Create a ConfigMap using a YAML file

Create a ConfigMap using a .yaml file containing the required configuration in key-value format:

kubectl create configmap [configmap_name] --from-file [path/to/yaml/file]

For example, to create a ConfigMap named example-configmap from the example-configmap.yaml file , you would run:

kubectl create example-configmap --from-file /api/v1/namespace/default/configmaps/example-configmap.yaml

Option 2: Create ConfigMap from file

Kubernetes example of creating and using ConfigMap: Kubernetes allows the creation of ConfigMap from one or more files in any plain text format (as long as the file contains key-value pairs).

To create a configuration map from a file, use the following command:

kubectl create configmap [configmap_name] --from-file [path/to/file]

To create a ConfigMap from multiple files, run:

kubectl create configmap [configmap_name] --from-file [path/to/file1] --from-file [path/to/file2] --from-file [path/to/file3]

Option 3: Create ConfigMap from Directory

You can also create a ConfigMap from a directory (i.e. all files in the directory). To do this, use the following command:

kubectl create configmap [configmap_name] --from-file [path/to/directory]

Kubectl packs each file in the directory into a new ConfigMap. Only include files with the basename of a valid key. Subdirectories and non-regular files are not included in ConfigMap.

Option 4: Create ConfigMap from literal value

You can also use this --from-literaloption to create a ConfigMap from literal values.

To do this, follow the basic syntax:

kubectl create configmap [configmap_name] --from-literal [key1]=[value1] --from-literal [key2]=[value]2

View key-value pairs in ConfigMap

To view the details of a Kubernetes ConfigMap and the values ​​of keys, use the following command:

kubectl get configmaps [configmap_name] -o yaml

The output should display the information in yaml format:

apiVersion: v1
data: 
  key1: value1
  key2: value2
  ...
kind: ConfigMap
metadata:
  creationTimeStamp: ...
  name: example-configmap
  namespace: default
  resourceVersion: ...
  selfLink: /api/v1/namespace/default/configmaps/example-configmap
  uid: ...

Configure Pod to use ConfigMap

How to create and use ConfigMap in Kubernetes? There are two ways to configure a pod to use a specific ConfigMap:

  1. Mount the ConfigMap as a volume
  2. Use environment variables

Note: You must create the ConfigMap before you can reference it to the desired pod.

Kubernetes example of creating and using ConfigMap: Mounting ConfigMap as a volume

After downloading or creating a ConfigMap, you can use volumes to mount the configuration to pods.

Add a volumes section to the pod’s yaml file:

volumes:
  - name: config
  configMap
    name: [configmap_name]
    items:
    - key: [key/file_name]
    path: [inside_the_pod]

After adding the required content, use this kubectl createcommand to create a pod with the ConfigMap as the volume.

Using ConfigMap with EnvFrom

How do I create and use ConfigMap in Kubernetes? ConfigMaps allows you to introduce multiple values ​​via environment variables.

Add the env section to the pod’s yaml file to extract the specified environment variables from the ConfigMap:

env: 
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: [configmap_name] 
              key: [key/file_name]

To extract all environment variables from the ConfigMap, add the envFrom section to the yaml file:

envFrom:
  - configMapKeyRef
      name: env-config

Then, use this kubectl createcommand to create a pod with the specified configuration settings.

In conclusion

This article shows you examples of creating and using ConfigMap in Kubernetes, including four different ways of creating ConfigMap. Additionally, it includes two ways to use Kubernetes ConfigMaps with Pods.