Config-map kubernetes multiple environments

I am trying to deploy a Spring boot application using configuration data from a Kubernetes cluster. I have a simple RestController that prints a message while 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;
}

Defined map configuration name in my application.yml application

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.

  • What is the best way to specify the environment properties set on the configuration map? And how to access them in a Spring boot application?
    Example: if the application is deployed to qa, my service should return the message "Hello from qa Kubernetes Configmap"
  • We also have plans to read these configuration files with GIT in the future. How to handle this usecase?
+5
5

Helm. Helm Chart, Kubernetes (, ConfigMaps, Deployments , ) .

Helm, Helm. helm create templates/, YAML ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-%s" .Release.Name .Chart.Name }}
  labels:
    app: {{ .Chart.Name | trunc 63 | trimSuffix "-" }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  application.properties: |-
    message={{ .Values.properties.message }}

YAML (, helm create ). ConfigMap , :

containers:
  - name: {{ .Chart.Name }}
    # [...]
    volumes:
      - name: property-volume
        mountPath: /etc/your-app/properties
volumes:
  - name: property-volume
    configMap:
      name: {{ printf "%s-%s" .Release.Name .Chart.Name }}

Helm values.yaml, , . (, ConfigMap {{ .Values.properties.message }}):

replicaCount: 1
image:
  repository: your-docker-image
  tag: your-docker-tag
properties:
  message: Hello!

Helm helm install , . YAML, values.yaml --set:

$ helm install --name dev --set image.tag=latest --set replicaCount=1 path/to/chart
$ helm install --name prod --set image.tag=stable --set replicaCount=3 --set properties.message="Hello from prod" path/to/chart

: , . helm upgrade, .

+2

, , , , , - , , . , , , , - .

1:

k8s/configmaps - , :

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

, .

2:

k8s , :

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

3:

bash :

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

, , :

ENVIRONMENT=dev ./update-configmaps.sh

4: CI/CD

CI/CD - configmap , , .

:

  • CI/CD

" ", , , , .

, !

+2

, spring-cloud-config - https://cloud.spring.io/spring-cloud-config/

, , git.

config-client config-server , .

- . , SPRING_PROFILES_ACTIVE.

0

, , qa, , production. Kubernetes, ​​ . , prod qa. , k8s .

, , Deployment . Deployment ConfigMap, Spring. ConfigMap -, .

, - ConfigMap. , .

Deployment ConfigMap, . , echo-properties ConfigMap, . Kubernetes . ConfigMap Spring Cloud Config, .

, ( ), fabric8 ConfigMap Controller . - , , , ConfigMap, .

, , , Spring Cloud Config , , ConfigMaps , , .

0

git- . https://github.com/kubernetes-sigs/kustomize

apiVersion: apps/v1 type: : : app: mycomponent env: dev: : mycomponent: myapplication

kubectl get pods -n myapplication -l env = dev, tier = backend, app = mycomponent

0

Source: https://habr.com/ru/post/1690478/


All Articles