Skip to content

Usage of configmap

configmap-diagram.gif

What are ConfigMaps?

In Kubernetes, a ConfigMap is an API object that provides a way to store configuration data in key-value pairs. ConfigMaps are designed to decouple configuration data from container images, allowing you to manage configuration data separately from the application code. This means that you can update configuration data without having to rebuild and redeploy the application code.

kubectl create configmap testConfigMap1 --from-literal shortname=AAA --from-literal longname=bbbbbbbbbbbbbbbb
kubectl get cm
kubectl get cm testConfigMap1 -o yaml
kubectl get cm testConfigMap1 -o json
kubectl describe cm testConfigMap1

Examples of ConfigMaps in Kubernetes

Example 1: Creating a ConfigMap from a file

Suppose you have a configuration file config.properties that contains key-value pairs for your application’s configuration. You can create a ConfigMap from this file using the following command:

kubectl create configmap my-config --from-file=config.properties

This will create a ConfigMap called my-config that contains the key-value pairs from the config.properties file.

Example 2: Using a ConfigMap in a Pod

Once you have created a ConfigMap, you can use it in a Pod by mounting it as a volume. For example, suppose you have a Pod definition that looks like this:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: my-config
## Mount the ConfigMap as a volume
kubectl apply -f pod.yaml
## Check
kubectl exec -it my-pod -- ls /etc/config

This Pod definition will create a Pod called my-pod with a single container that mounts the my-config ConfigMap as a volume at the path /etc/config. The key-value pairs from the ConfigMap will be available to the container as separate files in the /etc/config directory.

Example 3: Updating a ConfigMap

If you need to update the configuration data stored in a ConfigMap, you can do so using the kubectl edit configmap command. For example, suppose you want to update the my-config ConfigMap with a new key-value pair:

kubectl edit configmap my-config

This will open the my-config ConfigMap in your default editor. You can then add a new key-value pair to the ConfigMap and save the changes. The updated configuration data will be automatically propagated to any Pods that are using the my-config ConfigMap.

Conclusion

In conclusion, ConfigMaps are a powerful feature of Kubernetes that allow you to manage configuration data separately from your application code. By decoupling configuration data from container images, you can update configuration data without having to rebuild and redeploy your application code. We hope that this blog post has helped you understand what ConfigMaps are and how to use them in your Kubernetes deployments.

References

  • The Kubernetes Book, Nigel Poulton, 2021
  • https://github.com/nigelpoulton/TheK8sBook/tree/main/configmaps
  • https://matthewpalmer.net/kubernetes-app-developer/articles/configmap-example-yaml.html
Feedback