DevOps

How to Create and Use ConfigMap with Kubernetes

How to Create and Use ConfigMap with Kubernetes.

Introduction

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

Using small layered images is one of the practices for building efficient Kubernetes clusters. Utilizing ConfigMaps can help you achieve that.

In this tutorial, you will learn how to create and use ConfigMaps.

How to Create and Use ConfigMap with Kubernetes.

Prerequisites

  • Access to a user account with sudo or root privileges
  • Access to command-line/terminal window (CtrlAltT)
  • Kubernetes platform installed

What is ConfigMap and What is it Used For?

ConfigMaps are APIs that store configuration data in key-value pairs. Their primary function is to keep the configuration separate from the container image. It can represent the entire configuration file or individual properties.

If you are working with Kubernetes, you want to keep your image light and portable. To do this, you should keep the configuration settings separate from the application code. Using ConfigMaps you can add different configuration data on pods to suit the environment they are running in.

For example, you may use the same code with different configuration while in the development, testing or production phase.

Note: Make 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 ConfigMaps from files, directories, and literal values.

The basic syntax for creating a ConfigMap is:

kubectl create configmap [configmap_name] [attribute] 

Depending on the source, the attribute will be:

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

Option 1: Create ConfigMap Using a YAML File

Use a .yaml file that contains the wanted configuration in the format of key-value pairs to create a ConfigMap:

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

For example, to create a ConfigMap under the name 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 Files

Kubernetes allows creating a ConfigMap from one or multiple files in any plaintext format (as long as the files contain key-value pairs).

To create a ConfigMap from a file, use the 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 Directories

You can also create ConfigMaps from directories, that is from all the files within the directory. To do so, use the command:

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

Kubectl packages each file from the directory into the new ConfigMap. Only files with basenames that are valid keys are included. Subdirectories and non-regular files are not included in the ConfigMap.

Option 4: Create ConfigMap From Literal Values

You can also create ConfigMaps from literal values, using the --from-literal option.

To do so, follow the basic syntax:

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

See Key-Value Pairs in ConfigMap

To see details from a Kubernetes ConfigMap and the values for keys, use the command:

kubectl get configmaps [configmap_name] -o yaml

The output should display information in the 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

There are two ways you can configure a pod to use a specific ConfigMap:

  1. Mounting the ConfigMap as a volume
  2. Using environment variables

Note: You must create the ConfigMap before referencing it to the wanted pod.

Mounting ConfigMap as a Volume

Once you have downloaded or created a ConfigMap, you can mount the configuration to the pod by using volumes.

Add a volume section to the to the yaml file of your pod:

volumes:
  - name: config
  configMap
    name: [configmap_name]
    items:
    - key: [key/file_name]
    path: [inside_the_pod]
Use ConfigMap as volume.

Once you have added the required content, use the kubectl create command to create the pod with the ConfigMap as the volume.

Use ConfigMap with EnvFrom

ConfigMaps allows you to introduce multiple values through environment variables.

Add the env section to the yaml file of the pod to pull the specified environment variable(s) from a ConfigMap:

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

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

envFrom:
  - configMapKeyRef
      name: env-config
Use Kubernetes configMap with envFrom.

Then, use the kubectl create command to create the pod with the specified configuration settings.

Conclusion

This article showed you four different ways how to create ConfigMaps. Additionally, it includes two ways of using Kubernetes ConfigMaps with pods.

To continue learning about Kuberbetes and how to secure your workloads, refer to 6 Kubernetes Security Best Practices.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !