Configuring Spring Boot on Kubernetes with ConfigMap

ConfigMaps is the Kubernetes counterpart of the Spring Boot externalized configuration. ConfigMaps is a simple key/value store, which can store simple values to files. In this post “Configuring Spring Boot on Kubernetes with ConfigMap”, we will see how to use ConfigMaps to externalize the application configuration.

One of the ways configuring the spring boot application on kubernetes is to use ConfigMaps. ConfigMaps is a way to decouple the application specific artifacts from the container image, thereby enabling better portability and externalization.

The sources of this blog post are available in my github repo. In this blog post, we will build simple GreeterApplication, which exposes a REST API to greet the user. The GreeterApplication will use ConfigMaps to externalize the application properties.

Setup

You might need access to Kubernetes Cluster to play with this application. The easiest way to get local Kubernetes cluster up and running is using minikube. The rest of the blog assumes you have minikube up and running.

Create ConfigMap

You can see the contents of the ConfigMap using the command kubectl get configmap spring-boot-configmaps-demo-oyaml

Create Fragment deployment.yaml

Once we have the Kubernetes ConfigMaps created, we then need to inject the GREETER_PREFIX as an environment variable into the Kubernetes deployment. The following code snippet shows how to define an environment variable in a kubernetes deployment.yaml.

The above snippet defines an environment variable called GREETING_PREFIX which will have its value set from ConfigMap spring-boot-configmaps-demo key greeter.prefix.

NOTE:

As the application is configured to use fabric8-maven-plugin, we can create Kubernetes deployment and service as fragments in ‘$PROJECT_HOME/src/main/fabric8’. The fabric8-maven-plugin takes care of building the complete Kubernetes manifests by merging the contents of the fragment(s) from ‘$PROJECT_HOME/src/main/fabric8’ during deployment.

Deploy Application

To deploy the application, execute the following command from the $PROJECT_HOME ./mvnw clean fabric8:deploy.

Access Application

The application status can be checked with command, kubectl get pods -w once the application is deployed, let’s do a simple curl like curl $(minikube service spring-boot-configmaps-demo --url)/greet/jerry; echo ""; command will return a message Hello jerry! Welcome to Configuring Spring Boot on Kubernetes! The return message has a prefix called “Hello”, which we had injected via the environment variable GREETING_PREFIX with the value from the ConfigMap property “greeter.prefix”.

Let’s deploy and access the application like how we did earlier, but this time the response will be using the application.properties from ConfigMaps.

In this Part-I of the blog series, we saw Configuring Spring Boot on Kubernetes with ConfigMaps, in the Part-II of the series we will see on how to use kubernetes Secrets in configuring Spring Boot Application.