<p>I am trying to deploy a Spring Boot application using configuration data from Kubernetes cluster. I have a simple RestController that prints a message by reading from a Kubernetes cluster. </p>

<pre><code>    private String message = "Message not coming from Kubernetes config map";

@RequestMapping(value="/echo", method=GET)
public String printKubeConfig() {
    return message;
}
</code></pre>

<p>Specified the name of the config map in my application.yml</p>

<pre><code>spring:
  application:
    name: echo-configmap
</code></pre>

<p>echo-configmap</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: echo-configmap
data:
  application.properties: |-
    message=Hello from dev Kubernetes Configmap
  application_qa.properties: |-
    message=Hello from qa Kubernetes Configmap
</code></pre>

<p>I have several environments like qa, int, test etc  </p>

<ol>
<li>What's the best way to specify environment specific properties in the config map? And how to access them in Spring boot application?<br>
Ex: if the application is deployed in qa, my service should return the message "Hello from qa Kubernetes Configmap"  </li>
<li>We also have plans to read these configuration files from GIT in future. How to handle that usecase?</li>
</ol>

<p>Let me try and provide an answer which I think gives you what you need, without using any tools beyond what you'll have installed on most boxes. Maybe try this first, and if you find the approach becomes difficult to manage and scale, move onto something more sophisticated.</p>

<h2>Step 1: Version control configmaps per environment</h2>

<p>Create a folder like <code>k8s/configmaps</code> or something, and create one configmap per environment:</p>

<pre><code>k8s/configmaps/properties.dev.yaml
k8s/configmaps/properties.qa.yaml
k8s/configmaps/properties.sit.yaml
k8s/configmaps/properties.uat.yaml
</code></pre>

<p>Each configmap should contain your environment specific settings.</p>

<h2>Step 2: Have a namespace per environment</h2>

<p>Create a k8s namespace per environment, such as:</p>

<pre><code> application-dev
 application-qa
 application-sit
 application-uat
</code></pre>

<h2>Step 3: Create the configmap per environment</h2>

<p>A little bash will help here:</p>

<pre><code>#!/usr/bin/env bash
# apply-configmaps.sh
namespace="application-${ENVIRONMENT}"
for configmap in ./k8s/configmaps/*.${ENVIRONMENT}.yml; do
    echo "Processing ConfigMap $configmap"
    kubectl apply -n ${namespace} -f $configmap
done
</code></pre>

<p>Now all you need to do to create <em>or update</em> configmaps for any environment is:</p>

<pre><code>ENVIRONMENT=dev ./update-configmaps.sh
</code></pre>

<h2>Step 4: Finish the job with CI/CD</h2>

<p>Now you can create a CI/CD pipeline - if your configmap source changes just run the command shown above.</p>

<h2>Summary</h2>

<p>Based on primitive commands and no special tools you can:</p>

<ul>
<li>Version control config</li>
<li>Manage config per environment</li>
<li>Update or create config when the config code changes</li>
<li>Easily apply the same approach in a CI/CD pipeline if needed</li>
</ul>

<p>I would strongly recommend you follow this basic 'first principles' approach before jumping into more sophisticated tools to solve the same problems, in many cases you can do it yourself without much effort, learn the key concepts and save the more sophisticated tooling till later if you really need it.</p>

<p>Hope that helps!</p>
