Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

config-map kubernetes multiple environments

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.

    private String message = "Message not coming from Kubernetes config map";

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

Specified the name of the config map in my application.yml

spring:
  application:
    name: echo-configmap

echo-configmap

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

I have several environments like qa, int, test etc

  1. What's the best way to specify environment specific properties in the config map? And how to access them in Spring boot application?
    Ex: if the application is deployed in qa, my service should return the message "Hello from qa Kubernetes Configmap"
  2. We also have plans to read these configuration files from GIT in future. How to handle that usecase?
like image 309
A V Avatar asked Dec 09 '17 12:12

A V


People also ask

How do I create a ConfigMap from multiple files?

You can use kubectl create configmap to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose basename is a valid key in the directory and packages each of those files into the new ConfigMap.

What is config map in Kubernetes?

A ConfigMap is an API object that lets you store configuration for other objects to use. Unlike most Kubernetes objects that have a spec , a ConfigMap has data and binaryData fields. These fields accept key-value pairs as their values. Both the data field and the binaryData are optional.

How do you use helm for multiple environments?

Usually a Helm upgrade or install requires the release_name, chart_folder and other necessary flags. You'll need to run your Helm commands with explicit flag -f or --values, with the value as the path to your environment specific values file. And make sure you add your application specific other Helm flags if required.


1 Answers

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.

Step 1: Version control configmaps per environment

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

k8s/configmaps/properties.dev.yaml
k8s/configmaps/properties.qa.yaml
k8s/configmaps/properties.sit.yaml
k8s/configmaps/properties.uat.yaml

Each configmap should contain your environment specific settings.

Step 2: Have a namespace per environment

Create a k8s namespace per environment, such as:

 application-dev
 application-qa
 application-sit
 application-uat

Step 3: Create the configmap per environment

A little bash will help here:

#!/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

Now all you need to do to create or update configmaps for any environment is:

ENVIRONMENT=dev ./update-configmaps.sh

Step 4: Finish the job with CI/CD

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

Summary

Based on primitive commands and no special tools you can:

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

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.

Hope that helps!

like image 61
Dave Kerr Avatar answered Nov 08 '22 13:11

Dave Kerr