Kubernetes ConfigMaps Explained: What You Need to Know
What is a ConfigMap in Kubernetes?
A ConfigMap is an API object that lets you store configuration data as a key-value pairs. This data is then consumed by other kubernetes resources such as pods. It is mainly used to store non-confidential data, as it does not provide any sort of secrecy or encryption.
A ConfigMap is not designed to hold large amount of data (1 MiB Limit). For larger data storage you can use multiple ConfigMaps or use volume instead of ConfigMaps.
Why would you use a ConfigMap in Kubernetes?
You can use a ConfigMap to keep your application code separate from your configuration. This lets you change easily your configuration depending on your environment such as dev or prod and can dynamically change configuration at runtime.
When you work locally on your laptop, you set the environment variable in you shell, so when it comes to kubernetes deployments, you reference values in a ConfigMap.
How to use Kubernetes ConfigMap using YAML?
apiVersion: v1
kind: ConfigMap
metadata:
name: test-cm
data:
db-port: "3306"
kind: Specifies the kind of resource, in this case its a ConfigMap.
data: Contains the actual configuration data as key-value pairs.
Create the ConfigMap using the below command:
Kubectl apply -f cm.yaml
We will look at two different ways to use a ConfigMap to configure a container inside a Pod:
1. Using environment variables
2. Using volume mounts
Example of ConfigMap as pod environment variable
apiVersion: v1
kind: Pod
metadata:
name: demo-cm
spec:
containers:
- name: demo-app
image: nginx
env:
- name: DB-PORT
valueFrom:
configMapKeyRef:
name: test-cm
key: db-port
DB-PORT - Name of the env variable
valueFrom: Get the value from configMap
configMapKeyRef: ConfigMap reference
name: Name of the ConfigMap
key: value of key inside the ConfigMap
kubectl apply -f pod.yaml
Once the pods are running, exec into the pods and run the below command.
kubectl exec -it (name of the pod) -- /bin/bash
Now we are inside the container, next run the below command to check the env variable.
env | grep DB
This is how you retrieve the environment variable from the ConfigMap.
Example of ConfigMap as volume mounts
apiVersion: v1
kind: pod
metadata:
name: demo
spec:
containers:
- name: demo-python-app
image: python-app
volumeMounts:
- name: db-connections
mountPath: /opt
volumes:
- name: db-connections
configMap:
name: test-cm
volumes: - name: db-connections - Name of the volume.
configMap: - name: test-cm - Read the information from the ConfigMap named test-cm.
volumeMounts: Mounting the volume named db-connections.
mountPath: Path for the volume mount.
kubectl apply pod1.yaml
Once the pods are running, exec into the pods and run the below command.
kubectl exec -it (name of the pod) -- /bin/bash
Now we are inside the container, next run the below command to check the env variable.
cat /opt/db-port | more
The port number is 3308, since we have updated the cm.yml file which you can see below.
Understanding ConfigMap updates
When a ConfigMap is set with the environment variable they won't update the value automatically until the containers are restarted. The same ConfigMap when set using the volumes, it will receive the new ConfigMaps value automatically without restarting the containers.
Conclusion:
In the above guide we have covered both the ways to use the ConfigMaps - either by using them as environment variables or mounting them into a file.
ConfigMaps are stored in plain text so you should not use them for sensitive values. Kubernetes secrets are the alternative to ConfigMaps to store sensitive information with encryption support, which we will cover in our next part.
If you found this post useful, give it a like👍
Repost♻️
Follow Bala for more such posts 💯🚀
Subscribe to my newsletter
Read articles from Bala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by