Open In App

Kubernetes – ConfigMap

Last Updated : 17 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An open-source container orchestration system called Kubernetes is primarily used for automated software deployment, scaling, and management. Another name for Kubernetes is K8s. Originally developed by Google, Kubernetes is now managed by the Cloud Native Computing Foundation. Although it now supports both containers and CRI-O alongside the Docker runtime, it was originally intended to interact. Automating container-managed operational tasks is the primary goal of Kubernetes. There are built-in commands for deploying the application and rolling out the desired changes to the application. It is currently used by companies such as Google, Spotify, and Capital One.

kubernetes ConfigMap

In Kubernetes, Configmap is an API object which is mainly used to store non-confidential data. The data that is stored in ConfigMap is stored as key-value pairs. ConfigMaps are configuration files that may be used by pods as command-line arguments, environment variables, or even as configuration files on a disc. This feature allows us to decouple environment-specific configuration from our container images, after this is done our applications are easily portable. The thing to be noted here is that ConfigMap does not provide any sort of secrecy or encryption, so it is advised to store non-confidential data only we can use secrets to store confidential data. 

A ConfigMap may be used to set configuration data independently of the application code. Imagine that we are developing an application that you can run on your own computer and in the cloud. We create the code to check the DATABASE HOST environment variable. We can set the variable to localhost locally but in the cloud, we set it to a Kubernetes Service. A configMap is not designed to store large data in it. The data that is stored in ConfigMap can not be more than 1MiB. If we need to store more data than that then we can use volume instead of ConfigMaps.

What is a Kubernetes ConfigMaps?

We know that ConfigMap is an API object which is mainly used to store non-confidential data or configuration for other objects to use. Most of the Kubernetes objects have spec but ConfigMap has data and binaryData fields. Key value pair is accepted by these fields as values. The Data field is used to store UTF-8 strings while the binary data field is used to store binary data as base64-encoded strings. A valid DNS subdomain name should be given to ConfigMap. The key value that is recorded in the data field and the key value in the binaryData field cannot both be the same.

configmap object

Without being available to Pods this way directly, it may be used by other components of the system. Data that is utilized for configuration by other system components may be stored in COnfigMap. ConfigMaps are most commonly used to config settings for containers running in a Pod present in the same namespace. We can even use ConfigMap separately. 

Creating and Viewing Kubernetes ConfigMaps

The kubectl create configmap command can be used to construct a Kubernetes ConfigMap. The data that you wish to place in the ConfigMap together with the name of the ConfigMap are both inputs for this command.

For instance, the command below generates a ConfigMap named my-configmap with the information below:

kubectl create configmap my-configmap –from-literal=key1=value1 –from-literal=key2=value2

Additionally, a file or directory of files can be used to generate a ConfigMap. The –from-file or –from-dir options are used, respectively, to accomplish this.

For instance, the command below generates a ConfigMap with the name my-configmap from the text file my-file.txt:

kubectl create configmap my-configmap –from-file=key1=my-file.txt

Once the configmaps has been created you can view thw config maps by using the following command:

kubectl describe configmap my-configmap

Using Kubernetes ConfigMaps

A ConfigMap must first be created before it can be used to configure a Pod. The kubectl create configmap command can be used to achieve this. The ConfigMap must be mounted into the Pod once it has been built. Utilising the kubernetes volumes part of the Pod specification will allow you to do this.

When the ConfigMap is mounted into the Pod, the container(s) in the Pod can access the configuration information stored there. The containers can then start up and operate using these configuration data.

A Pod specification that mounts a ConfigMap is shown in the following example:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: configmap-volume
      mountPath: /etc/configmap
  volumes:
  - name: configmap-volume
    configMap:
      name: my-configmap

This Pod specification mounts the ConfigMap my-configmap into the container at the path /etc/configmap. The container can then access the configuration data in the ConfigMap at this path.

Kubernetes ConfigMaps and Pods 

The data which is non-confidential can be stored ion kubernetes by using config maps. Kubernetes pods uses the ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume. This makes the application more portable by decoupling the environment-specific configuration from your container images.

A pod is the smallest unit that exists in Kubernetes. It is similar to that of tokens in C or C++ language. A specific pod can have one or more applications. The nature of Pods is ephemeral this means that in any case if a pod fails then Kubernetes can and will automatically create a new replica/ duplicate of the said pod and continue the operation. The pods have the capacity to include one or more containers based on the requirement. The containers can even be Docker containers. The Pods in Kubernetes provide environmental dependencies which include persistent storage volumes which means it is permanent and is available to all pods in the said cluster and even configuration data that is required to run the container within the pod.

With the help of config maps you can use the different environment variable for the different environment which can be configure with the application.

Using Kubernetes ConfigMaps as Files From a Pod

You must first construct a ConfigMap in order to use it as a file in a pod. The command kubectl create configmap can be used to accomplish this. The ConfigMap must be mounted as a volume in the Pod once it has been generated. The volumes part of the Pod specification can be used to accomplish this.

For instance, the following Pod specification mounts a volume named my-configmap at the path /etc/configmap for a ConfigMap with the name my-configmap:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: configmap-volume
      mountPath: /etc/configmap
  volumes:
  - name: configmap-volume
    configMap:
      name: my-configmap

The containers in the Pod can access the files in the ConfigMap once the ConfigMap has been mounted into the Pod. These files can then be used by the containers to launch and operate.

Here is an illustration of how to use a container to access the files in a ConfigMap:

# Get a list of all the files in the ConfigMap

ls /etc/configmap

# Get the contents of a file in the ConfigMap

cat /etc/configmap/my-file.txt

Updating Kubernetes ConfigMaps

The configmaps can befig edited by using two ways one is by changing the config maps in the config file and by using the command kubectl edit configmap command. It will open the kubernetes configmaps file and there you can make the changes required in that file.

You can also use kubectl update configmap command. to update the config maps for example as shown below.

kubectl update configmap my-configmap –from-literal=key1=value1 –from-literal=key2=value2

FAQs On Kubernetes ConfigMaps

1. Difference Between kubernetes Configmaps vs Secrets

  1. Configmaps: ConfigMaps are used to store configuration files, certificates, and log files, among other non-sensitive data types.
  2. Secrets: Passwords, API keys, and database connection strings are examples of sensitive data that is kept in kubernetes secrets.

2. How To List kubernetes List ConfigMaps

To list the ConfigMaps in the kubernetes particular namespace you can use the following command.

kubectl get configmap

3. kubernetes Configmaps Is Forbidden

If you are facing the CnfigMaps forbidden error then it is indication to that you didn’t have enough permissions to acceses the ConfigMaps.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads