Centralized Configuration Spring Application on Kubernetes

A couple months ago, Spring Cloud Kubernetes project has been graduated from the incubation process. The project provides Kubernetes integration with Spring Cloud. For those who are not familiar with Kubernetes, you can look at this article, and this, for introduction, and you can start interactive tutorial here.

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems. [source]

One of the functionalities is integration with the Kubernetes ConfigMap.

In general, spring application configuration is created using files (e.g. properties, yaml). There is no problem with this way, until we have many service instances. Only one value changes, then the configuration should be distributed to all service nodes and… restart the services. Another problem can occur if you deploy the service using a container (e.g. Docker) and you save the configuration file in it, then you have to re-build the container image (I don’t think anyone will do this 😅).

If you don’t deploy the service to Kubernetes, then you can use Spring Cloud Config or using Consul for Distributed Configuration. But… when your service is deployed to a container and with container orchestration (in this case Kubernetes) for its management, it would be nice to try Kubernetes ConfigMap for centralized configuration.

This time, I want to share my experience trying this project, at least as far as I have learned until today.

First of all you have to install minikube and kubectl so you can run Kubernetes on your local development machine. Next…

Start Minikube

Start minikube

Create ConfigMap

ConfigMap.yaml

Above is an example of a yaml file to create a ConfigMap. Fill data property with the contents of the application.properties that we usually use, and then execute kubectl create -f ConfigMap.yaml command.

get and describe configmap

You can check the list and content of ConfigMap with the command as shown above.

Create Role

ClusterRoleBinding.yaml

Project Setup

  • spring-boot-starter-web
  • spring-boot-starter-actuator
  • spring-cloud-starter-kubernetes-config
  • lombok

if you use maven, see code below…

dependencies

application.properties

application.properties

spring.cloud.kubernetes.reload.* configs are for configuring the ConfigMap auto reload method, and management.endpoint.* are to configure the actuator.

Object Mapping for Configuration in ConfigMap

Config.java

and add @Configuration annotation and @ConfigurationProperties(prefix="myconfig") annotation, the prefix here is adjusted to the content in the ConfigMap.

Test the Configuration

TestHTTPService.java

Next, you can try it directly without deploying it to Kubernetes, because Spring Cloud Kubernetes has the Transparency feature.

All of the features described above will work equally well regardless of whether your application is running inside Kubernetes or not.

But, before you run your application, you must have an environment variable named KUBERNETES_NAMESPACE containing the namespace name that you use.

export KUBERNETES_NAMESPACE=default

Test Result

test config

The picture above shows the results of the http API that was created previously to display the config.

Deploy to Kubernetes

build docker image

After your image is ready, then it’s ready to deploy. Next…

Create Controller, i choose Replicaset

ReplicaSet.yaml
create replicaset controller

Create Service

Service.yaml
create and describe service

Next, test using Service port…

test result

Full source code is available on my Github page.

Thanks for reading (Sorry For My Bad English 😅) and feel free to comment.

Are you looking for information about remote work?
or have a cool resource about remote work?
remotework.FYI is all you need to know about remote work, find and share cool resources right now.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store