This blog will demonstrate:
- What is ConfigMap in Kubernetes?
- How to Get ConfigMap Using “kubectl get”?
- How to Get and Save ConfigMap in Yaml Using “kubectl get”?
- How to Describe ConfigMap in Kubernetes?
- Conclusion
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:
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:
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”:
After that, utilize the “kubectl apply -f <filename.yml>” command to create the ConfigMap:
Step 5: Get ConfigMap
To view the Kubernetes ConfigMap, utilize the “kubectl get configmap” command:
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:
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:
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:
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:
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.