Externalise and Configure Spring Boot Environment Variables on Kubernetes

Build Once, Run Anywhere

Zing Zai
2 min readSep 15, 2019

Pre-requisites

This only applies for Spring Boot 2.0 and above. It is time to upgrade if you are still not on this version!

Overview

Spring Boot lets us externalise our configurations so that we can use the same application code in different environments. Coupled with Docker and Kubernetes, it makes deployments extremely easy and fast.

There are many ways of externalising your Spring Boot configs with Kubernetes. In this article, I’m going to share with you how you can read in Spring Boot configs from your OS environment variables by just changing your Kubernetes yaml file. This is the easiest, fastest and cleanest way I’ve came across.

You DO NOT have to change application.properties file.

Example illustration

application.properties (dev env)
sample.yaml (prod env)

Spring Boot Relaxed Binding 2.0 allows Spring Boot projects to read environment variables when they adhere to a specific syntax. (See section Environment Variables).

Specific format to follow

The above table shows some common formatting rules you have to follow when converting application.properties configs to yaml configs.

Conclusion

Converting application.properties configs to yaml configs made our deployments much easier, faster and cleaner. You should do it too!

What’s Next

  1. Encrypted configs: We are evaluating ways of managing our k8 secrets! (i.e. Vault, Cyberark)
  2. Externalise frontend config: Experienced developer friends reached out to me and I’ve wrote a Medium article on this.

Our Experience

My team and I tried the following:

  1. ConfigMaps: This works pretty well. However, there is a learning curve to Kubernetes and ConfigMaps and not everyone has the time to pick it up.
  2. Repackaging base Docker image with production application.properties:
    This is extremely time consuming and not recommended. We had to spend hours repackaging our base Docker images… If we had known about Spring Boot Relaxed Binding method, we would have saved HOURS.
  3. Changing our application.properties with weird markups:
    This works pretty well too… but there is a need to change our application.properties file. And there might be more env variables to externalise if you break your config into multiple parts.

--

--