ConfigMaps can be used to store application configuration artifacts, with the container image and, customize the containerized applications. I thought to write up an article to show, how we can configure Kubernetes PODs to use ConfigMaps data with the containers. Basically, two methods will be discussed in this article.
How To Create A ConfigMaps
First of all, ConfigMap should be created with the parameters. I have created tcKey1= tc-Value-1 and, tcKey2= tc-Value-2, parameters in the ConfigMap, here is the yaml file to create the ConfigMap object .
apiVersion: v1 kind: ConfigMap metadata: name: tc-config-map data: tcKey1: tc-Value-1 tcKey2: tc-Value-2
Create the ConfigMap object with kubectl command pointing the yaml file, ConfigMap object will be created as below
Read More: Kubernetes Error: Unable To Connect To The Server Tcp I/O Timeout
Use ConfigMaps To Store Values As Environment Variables
I have deployed a Kubernetes POD with the name of “tc-configmap-pod“, referencing the created ConfigMap. My “tcKey1” value is passed to the “TC_VAR” environmental variable.
apiVersion: v1 kind: Pod metadata: name: tc-configmap-pod spec: containers: - name: tc-container image: busybox command: ['sh', '-c', "echo $(TC_VAR) && sleep 3600"] env: - name: TC_VAR valueFrom: configMapKeyRef: name: tc-config-map key: tcKey1
If you logged in the container shell and echo the “TC_VAR” environmental variable, you can see the value which I passed through the ConfigMap.
Here is the command to access the Container shell
kubectl exec POD_NAME -it --sh
How To Pass ConfigMap Data With A File, And A Mounted Volueme
We can pass the ConfigMap data to the container as a form of a file and a mounted volume. I have created the below yaml file for my POD to mount the ConfigMap, to the “/etc/config” mount location. Volume name would be “tc-config-volume“.
apiVersion: v1 kind: Pod metadata: name: tc-configmap-volume-pod spec: containers: - name: tc-app-container image: busybox command: ['sh', '-c', "echo $(cat /etc/config/tcKey2) && sleep 3600"] volumeMounts: - name: tc-config-volume mountPath: /etc/config volumes: - name: tc-config-volume configMap: name: tc-config-map
If you access the container shell and check the mount location, two files should be created as below. I have specified two keys in my ConfigMap and two files were created accordingly. Files contained the specified values in the ConfigMap.
You can directly view the value you specified in the key, with below command
kubectl exec POD_NAME -it -- cat /etc/config/tcKey1
I hope this article will help someone to understand ConfigMaps objects, which can be used in Kubernetes easily.
For More Information: