ConfigMaps (k8s)

Introduction

Kubernetes ConfigMaps are objects that allow you to decouple configuration artifacts from image content, making it possible to manage application configuration separately from the application's lifecycle​.

Reference

Optimizing Spring Boot Config Management with ConfigMaps: Environment Variables or Volume Mounts

https://www.infoq.com/articles/config-maps-with-spring-boot/


Spring Boot access to ConfigMap data as environments variables or Volume Mounts

There are two main approaches to configure Spring Boot applications running on Kubernetes with ConfigMaps:


First Approach: Loading ConfigMaps into Spring Boot Applications as Environment Variables

Loading ConfigMaps into Spring Boot applications as environment variables is a best practice for managing configuration data in Kubernetes environments.

A ConfigMap YAML contains the JSON configuration we want to inject into Spring Boot application. We can create a ConfigMap using a YAML file. For an example YAML definition for a ConfigMap, see reference "Optimizing Spring Boot Config Management with ConfigMaps: Environment Variables or Volume Mounts".

Create a ConfigMap Object into Kubernetes Cluster by executing the command, kubectl apply -f config.yml, and the ConfigMap will be created into Kubernetes cluster. We can deploy them separately or as part of the same deployment manifest YML.

Configure the Kubernetes deployment YAML file to mount the ConfigMaps (config.yml) as environment variables in the Spring Boot application. This can be done by referencing the ConfigMap keys in the env section of the container specification. For an example deployment manifest, see reference "Optimizing Spring Boot Config Management with ConfigMaps: Environment Variables or Volume Mounts".

In a Spring Boot application, access the configuration data provided by the ConfigMaps through environment variables. Spring Boot automatically loads environment variables into its Environment object, allowing you to programmatically access them.

The SPRING_APPLICATION_JSON environment variable in Spring Boot allows you to provide inline JSON to configure Spring Boot applications. When Spring Boot starts up, it looks for this environment variable and, if present, parses the JSON content and merges it with the Spring Boot application's existing configuration properties.

When Spring Boot application starts up and detects the SPRING_APPLICATION_JSON environment variable with this JSON content, it will override the corresponding properties for the datasource URL, username and password.

Once the deployment is created, Kubernetes will inject the environment variables from the ConfigMap into Spring Boot application pods. The Spring Boot application will automatically pick up these environment variables and use them to configure itself.


Second Approach: Loading ConfigMaps into Spring Boot Applications as Volumes

Loading ConfigMaps into Spring Boot applications as volumes in Kubernetes is another practice for managing configuration data in Kubernetes environments.

Create a ConfigMap in Kubernetes containing the configuration data needed by Spring Boot application. We can create a ConfigMap using YAML configuration, see reference "Optimizing Spring Boot Config Management with ConfigMaps: Environment Variables or Volume Mounts".

Modify the Kubernetes deployment configuration YAML file to mount the ConfigMap as a volume into the pods running Spring Boot application. Specify the mount path where the application expects to find its configuration files, see reference "Optimizing Spring Boot Config Management with ConfigMaps: Environment Variables or Volume Mounts".

Configure the Spring Boot application to read configuration files from the mounted volume path. We need to adjust the application's property YML file locations accordingly.

Deploy the Spring Boot application to Kubernetes using the updated deployment configuration. Make sure to create the ConfigMap in the same namespace as the application running. For example: namespace: dev

Verify that Spring Boot application can access the configuration data from the mounted volume. You can get into a running pod and inspect the mounted volume to ensure that the configuration files are present.

Make changes to the ConfigMap data using kubectl commands or by updating the ConfigMap YAML configuration. Verify that the changes are reflected in the running Spring Boot application without requiring a redeployment or code changes.