Introduction

Many applications require configuration via some combination of config files, command line arguments, and environment variables. ConfigMaps in Kubernetes is the equivalence of externalized configuration in Spring Boot. ConfigMaps is a simple key/value store, which can store simple values to files. In this post, we will see how to use ConfigMaps to externalize our application configuration.

Dependencies

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-configuration-processor</artifactId>

<optional>true</optional>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-kubernetes-config</artifactId>

<version>0.3.0.RELEASE</version>

</dependency>

What is Kubernetes ConfigMap

The ConfigMap API resource provides mechanisms to inject containers with configuration data while keeping containers agnostic of Kubernetes. This allows you to decouple configuration artifacts from image content to keep containerized applications portable. ConfigMap can be used to store fine-grained information like individual properties or coarse-grained information like entire config files or JSON blobs.

application.properties

server.port=9000



spring.application.name=springboot-kubernetes-example



spring.cloud.kubernetes.config.name=springboot-kubernetes-example

spring.cloud.kubernetes.config.namespace=default

spring.cloud.kubernetes.config.sources[0].name=springboot-kubernetes-example

spring.cloud.kubernetes.reload.enabled=true

spring.cloud.kubernetes.reload.mode=polling

spring.cloud.kubernetes.reload.period=2000



management.endpoint.restart.enabled=true

management.endpoint.health.enabled=true

management.endpoint.info.enabled=true

Create ConfigMap

We will now be creating the ConfigMap here. See file content below :

apiVersion: v1

kind: ConfigMap

metadata:

name: springboot-kubernetes-example

data:

application.properties: |-

webservice.endpoint.transferfund=http://www.customer-service.app/api/tf

webservice.endpoint.getbalance=http://www.customer-service.app/api/balance

webservice.endpoint.customerinfo=http://www.customer-service.app/api/customerinfo

kubectl create -f springboot-k8s-configmap.yml

kubectl describe configmap springboot-kubernetes-example

docker build -t saaddico/springboot-k8s-configmap:v3 .

Deploy the Application

Now that we have created a docker image we can create kubernetes deployment with below manifest :

kubectl create -f springboot-k8s-configmap-deployment.yml

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

name: springboot-configmap-k8s

labels:

app: springboot-configmap-k8s

role: backend

stage: production

spec:

replicas: 1

selector:

matchLabels:

app: springboot-configmap-k8s

version: v0.0.1

template:

metadata:

labels:

app: springboot-configmap-k8s

version: v0.0.1

spec:

containers:

- name: springboot-configmap-k8s

image: saaddico/springboot-k8s-configmap:v3

imagePullPolicy: Always

ports:

- containerPort: 9000

Be sure to add this environment variable :

export KUBERNETES_NAMESPACE=default

kubectl get pod

kubectl logs -f springboot-configmap-k8s-6678fb9ffd-qtbsh

As you can see from above the property are being read by our application and also reloaded at a certain time interval. Source code for this project can be found here.