Kubernetes is a popular platform for managing containerized applications. One of the challenges of using Kubernetes is how to configure the applications and pass the configuration data to the containers. This is where Kubernetes ConfigMap comes in handy.
What is Kubernetes ConfigMap?
A ConfigMap is a Kubernetes object that stores key-value pairs of configuration data. It allows you to decouple the configuration from the container image, making it easier to update and maintain. You can create a ConfigMap from various sources, such as files, directories, literals, or environment variables.
In this blog post, we will show you how to create and use a ConfigMap in Kubernetes. We will cover the following topics:
- Creating a ConfigMap
- Consuming a ConfigMap as Environment Variables
- Mounting a ConfigMap as Volume
Creating a ConfigMap
There are several ways to create a ConfigMap in Kubernetes. The simplest way is to use the kubectl create configmap command. For example, you can create a ConfigMap named app-config with two key-value pairs: app.name=hello and app.version=1.0.
kubectl create configmap app-config –from-literal=app.name=hello –from-literal=app.version=1.0
You can also create a ConfigMap from a file or a directory. For example, you can create a ConfigMap named app-config-file from a file named app.properties.
kubectl create configmap app-config-file –from-file=app.properties
Or you can create a ConfigMap named app-config-dir from all the files in a directory named config.
kubectl create configmap app-config-dir –from-file=config/
You can verify that the ConfigMap is created by using the kubectl get configmap command.
kubectl get configmap
You can also view the details of a ConfigMap by using the kubectl describe configmap command.
kubectl describe configmap app-config
Consuming a ConfigMap as Environment Variables
One way to use a ConfigMap in your application is to consume it as environment variables. You can do this by using the envFrom or env field in the pod spec. For example, you can create a pod that runs a container image named hello-app and injects all the key-value pairs from the app-config ConfigMap as environment variables.
apiVersion: v1
kind: Pod
metadata:
name: hello-app
spec:
containers:
- name: hello-app image: hello-app envFrom:
- configMapRef:
name: app-config
- configMapRef:
Alternatively, you can use the env field to inject only specific key-value pairs from the ConfigMap as environment variables.
apiVersion: v1
kind: Pod
metadata:
name: hello-app
spec:
containers:
- name: hello-app image: hello-app env:
- name: APP_NAME
valueFrom:
configMapKeyRef:
name: app-config
key: app.name - name: APP_VERSION
valueFrom:
configMapKeyRef:
name: app-config
key: app.version
- name: APP_NAME
You can verify that the environment variables are set by using the kubectl exec command.
kubectl exec hello-app — env | grep APP_
Mounting a ConfigMap as Volume
Another way to use a ConfigMap in your application is to mount it as a volume. You can do this by using the volumes and volumeMounts fields in the pod spec. For example, you can create a pod that runs a container image named hello-app and mounts the app-config-file ConfigMap as a volume at /etc/config.
apiVersion: v1
kind: Pod
metadata:
name: hello-app
spec:
containers:
- name: hello-app image: hello-app volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
- name: config-volume
configMap:
name: app-config-file
This will create files under /etc/config with the names and contents of the key-value pairs in the ConfigMap. For example, there will be a file named /etc/config/app.properties with the content of the app.properties file.
You can verify that the files are created by using the kubectl exec command.
kubectl exec hello-app — ls /etc/config
Conclusion
In this blog post, we have learned what is Kubernetes ConfigMap and why we need it. We have also learned how to create and use a ConfigMap in Kubernetes. We have seen how to consume a ConfigMap as environment variables or mount it as a volume. We hope this guide has helped you understand how to configure your applications in Kubernetes using ConfigMaps.