Introduction to Kubernetes Configmap
In Kubernetes, ConfigMap is the dictionary of key-value pairs of strings representing your Pods’ configuration. These values can be assigned to new containers in a containerized application cluster, then make those usable for the same application or can be changed to create a new environment based on the configuration of the previous one.
You use ConfigMap to separate your application code from your cluster configuration. This setup allows you to dynamically and easily change your configuration based on your development, testing, and production environments. Ideally, ConfigMap can store configuration for your code, connection strings, username, passwords, hostnames, URIs and URLs.
Configmap of Kubernetes
Kubernetes have two types of such API objects which can be referred to as configuration data in a Pod when creating or stating it, viz. Secret and ConfigMap.
Both Secret and ConfigMap behave similarly in Kubernetes, like how they are created, referred or exposed.
This article focuses on ConfigMaps, mainly used for non-sensitive dates like environment variables, config files, etc.
ConfigMap is basically a Kubernetes API resource that stores configuration data as Key-Value pairs. This key-value pair data can be provided by a file, files, a directory having multiple files or Literals values on the command line and many other ways. This data is used by Pods for configuration.
ConfigMap provides you a way to work with strings that you can pass to your pods at the time of creation. In addition, ConfigMap segregates the configuration data from container image contents to make containerized applications as well as workload portable and reusable to make another environment with different configuration data as it’s easier to change and manage. It also prevents you from hardcoding anything in configuration data for pod specifications.
But you must note that ConfigMaps is not a substitute for Kubernetes Secrets, where you store sensitive information of your cluster; rather, ConfigMaps is useful to share and store only non-secret or non-sensitive data.
How Configmap Works in Kubernetes?
Before we go further to examples, we should have the below notes: –
- You must have a ConfigMap before referring to it while creating a Pod.
- ConfigMaps resides in a specific NameSpace, and only Pods residing in the same NameSpace can refer to it.
- You can’t use ConfigMaps for statis pods; it just doesn’t support it.
- For an existing ConfigMap, you can edit it on the fly and redeploy it using a command like below.
This will open the ConfigMap in default editor like “vi editors” and in YAML format.
kubectl edit configmap test-configmap
As we discussed before, ConfigMap is a dictionary or collection of Key-Value Pairs. These pairs are providing either by files or directly by Literal Values on the Kubectl command line. Then Kubeclt API packages these pairs into a configuration.
When you have a properties file like below: –
You use this file to create a ConfigMap like below: –
kubectl create configmap user-configmap --form-file=test-user.properties
Now check if this is created like below: –
kubectl get configmaps user-configmap
Now when you view this ConfiMap as YAML format like below. You can see the whole Package which was created using your file, which had only key-pair values.
kubectl get configmaps user-configmaps -o yaml
Also, to create ConfiMap, you can use a YAML file like below For example: –
Prepare a file with the mandatory fields like data, kind, apiversion, etc.
Then use Kubectl like below to create an object from this file.
kubectl create -f user2.yaml
Now you can see the contents and other related details using kubectl like below: –
kubectl describe configmap user-configmap
Now we can see how we can use a ConfigMap and refer to it while creating a pod.
In the below example: –
we created a file which is having contents for creating a ConfigMap as well as the pod. Here we are using NGINX image to create a container in a pod.
Now use kubectl like below to create ConfigMap and a Test pod-based of NGINX. Once these are created, you will get output like below: –
kubectl create -f testconfig.yaml
Now use kubectl like below to run a command in a pod from outside of it. Here we are using a simple env command inside, which shows all the environment variables. When filtering the output, you can find those environment variables you set in your ConfigMap contents as Key-Value pair.
kubectl exec “test-pod” -it -- hostname
kubectl exec “test-pod” -it -- env | grep _ENV
Example of Kubernetes Configmap
In Kubernetes, you have two main ways to create a ConfigMap. In each of these two ways, you have several other available options that you can use based on your requirements and needs. These options may change in different Kubernetes versions, so better check you Kubernetes version’s Official Documentations: –
1. Use the below command with many options. Here <NAME> is your ConfigMap name, and <DATA> is the source of your key-value pair.
kubectl create configmap <NAME> <DATA>
You can use a different source like the below examples: –
- For a file: –
kubectl create configmap configmap-test --form-file=configmap-test-file.properties
- From Directories: –
kubectl create configmap configmap-test --form-file=./configmap-test-directory/
- From more than one files: –
kubectl create configmap game-config-2 --form-file=test-file_1.properties --from-file=test-file_2.properties
- From environment files which have environment variable set: –
kubectl create configmap game-config-env-file --form-env-file=configmap-test-env-file.properties
- From a key in a file: –
kubectl create configmap configmap-test --form-file=<my-key-name>=<path-to-file>
- From Literal Values: –
kubectl create configmap special-config --form-literal=test.key1=sample1 --form-literal=test.key2=sample2
2. Use kustomization.yaml via kubectl since version 1.14. You can also specify generators inside kustomization.yaml inside a folder/directory to create a ConfigMap and then apply when creating a pod.
You have many options for this as well, like mentioned in the below examples:
- From file or files in a directory, you first have to create kustomization.yaml like below:
cat << EOF >./kustomization.yaml
Then you move this file to a specified directory as per your requirements, then use this file to create ConfigMap Object like below:
You can check whether ConfigMap is created of not, like below: –
In the above, you must note that the newly created ConfigMap’s name has a suffix attached to it by hashing its contents which ensures that a newly created ConfigMap is generated and different from others each time when its contents are modified.
- From Literal Values: –
You can use like below to give Key-Value pairs in kustomization.yaml and place kustomization.yaml in a directory:-
cat << EOF >./kustomization.yaml
Then use the same to create configmap like below: –
Using ConfigMap efficiently allows you to not to create custom images every time when you have some change in requirements for containers, but you can segregate your generic images and have multiple ConfigMaps or Secrets. Then use these while starting of creating new pods. Thus, saving efforts to manage and improve the modularity of your infrastructure environment.
This is a guide to Kubernetes Configmap. Here we discuss the Examples of Kubernetes Configmap and How does it work, and how to use it. You may also have a look at the following articles to learn more –