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:
the first approach loads ConfigMaps into Spring Boot applications as environment variables
the second approach loads ConfigMaps into Spring Boot applications as mounting them as volumes. Volume mounting enables hot reloading of configurations when changes are made to the ConfigMap
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.
Define a ConfigMap YAML File
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
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.
Load ConfigMaps as Environment Variables
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".
Access ConfigMap Data in Spring Boot
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.
Start Spring Boot Application
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
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".
Mount ConfigMap as a Volume
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".
Update the Spring Boot Application
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 Application
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 the Configuration
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.
Test Configmap Updates
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.