Kubernetes

How to Get ConfigMap Using “kubectl get”?

Kubernetes is a container orchestration (container composition) system that is used to deploy containerized applications inside the Kubernetes components. This containerized application also sets the environment variable using a .env file or through any command line option. But if the multi-container application or multi-pod service requires the same set of environment variables, users are required to configure these variables separately with each Pod or container which is quite a complex and hectic task. To overcome this problem, Kubernetes provides the API object ConfigMap.

This blog will demonstrate:

What is ConfigMap in Kubernetes?

The ConfigMap is a Kubernetes API object that is used to store and manage the data of Kubernetes resources. It provides the same functionality as the environment variables of containers. However, the ConfigMap is a configuration settings dictionary that stores the configurations, environment variables, and nonconfidential information of Pods or containerized applications in the form of a key-value pair.

One Key difference between environment configuration and ConfigMap is that the environment variables are usually stored in containers or pods however the ConfigMap is portable and can be decoupled from pods. These ConfigMap settings can be used by different pods which saves the developer from configuring environment variables to each pod separately.

How to Get ConfigMap Using “kubectl get”?

To access the ConfigMap using the “kubectl get configmap” command, first, create a new ConfigMap either by applying the yaml manifest or by using the “kubectl create” command and then access the ConfigMap. Follow the below instructions for demonstration.

Step 1: Launch PowerShell

First, launch the Windows PowerShell with administrator rights via the “Startup” menu:

Step 2: Start Kubernetes Cluster

Start the Kubernetes Cluster by utilizing the below command:

minikube start

Note: At that point, the user may encounter an error. To fix the error, make sure the Docker engine is running on the system. Another solution is to completely remove the Kubernetes cluster and node using the “minikube delete” command and restart the Cluster using the below command.

Step 3: Create ConfigMap Yaml File

To create a ConfigMap in Kubernetes, first, create a file named “configmap.yml”. After that, add the below instructions to the file:

apiVersion: v1

kind: ConfigMap

metadata:

name: test-configmap

data:

User: "Linuxhint"

Color: "Red"

dp_port: "3306"

In the above description, the “kind” key is used to specify the Kubernetes resource type, “name” sets the name of ConfigMap, and the “data” key stores the environment variable, nonconfidential data, or any configuration settings.

Step 4: Create ConfigMap

Navigate to the directory where the configmap.yml file exists using “cd”:

cd C:\Users\Dell\Documents\Kubernetes\HTML

After that, utilize the “kubectl apply -f <filename.yml>” command to create the ConfigMap:

kubectl apply -f configmap.yml

Step 5: Get ConfigMap

To view the Kubernetes ConfigMap, utilize the “kubectl get configmap” command:

kubectl get configmap

Currently, we have two ConfigMap, first one is default generated when the user runs the Kubernetes, and the second is the “test-configmap” that we have created in the above step:

How to Get and Save ConfigMap in Yaml Format Using “kubectl get”?

Users can view the ConfigMap configurations in Yaml format. To do so, use the “kubectl get configmap <configmap-name> -o yaml” command. In the below command, the “-o” option is utilized to specify the output format to view the ConfigMap data:

kubectl get configmap test-configmap -o yaml

In order to save the ConfigMap data in a new file, use the below command. After the “>” operator, specify the name of the file in which you want to store the ConfigMap data:

kubectl get configmap test-configmap -o yaml > new-configmap.yml

For verification, use the “ls” command. Here, you can see we have successfully accessed and saved the ConfigMap data into the “new-configmap.yml” file:

ls

How to Describe ConfigMap in Kubernetes?

Here, users may be confused about Kubernetes ConfigMap and think that ConfigMap can save confidential or secret information. Let me clarify that, ConfigMap is only used to set environment variables, configuration settings, or some nonconfidential data. The stored value of ConfigMap can be easily accessible by inspecting it from the command line. To inspect the ConfigMap, use the “kubectl describe configmap <configmap-name>” command:

kubectl describe configmap test-configmap

That is all about accessing ConfigMap using the “kubectl get configmap” command.

Conclusion

To get the ConfigMap, first, create a ConfigMap through the “kubectl create configmap” or “kubectl apply” command. After that, access the Kubernetes ConfigMap by utilizing the “kubectl get configmap” command. In order to access the specific ConfigMap in Yaml format, use the “kubectl get configmap <configmap-name> -o yaml” command. To access or inspect the ConfigMap, simply use the “kubectl describe configmap <configmap-name>” command. This blog has illustrated how to get the ConfigMap through the “kubectl get configmap” command.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.