DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Spring Boot With Kubernetes
  • 7 Microservices Best Practices for Developers
  • Popular Tools Supporting YAML Data Format
  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes

Trending

  • DGS GraphQL and Spring Boot
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  • Ethical AI in Agile
  • Agentic AI for Automated Application Security and Vulnerability Management
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. K8s KnowHow - Using a ConfigMap

K8s KnowHow - Using a ConfigMap

Use ConfigMap to create an externalized configuration decoupled from deployment artifacts.

By 
Nikhil Bhide user avatar
Nikhil Bhide
·
Dec. 10, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
56.5K Views

Join the DZone community and get the full member experience.

Join For Free

This is the fifth article of Kubernetes KnowHow series. In first four articles we have learned how to use pod, services and replicaset and deployment in K8s.

Properties files are very important in any application. Not only they make application flexible but they also provide different behavior depending upon the value you set in the file. In fact, many times we use a properties file to drive different behavior in staging, development, QA, UAT or production environments.

Generally, properties files are packed along with the code and the entire package is deployed in the execution environment. In this approach, if you want to change any properties then its absolutely essential to ship the code even if there is a change in the properties file. Hence, up to a certain extent, this approach works fine. However, in today’s era of digital transformation in which five nines has become the need of the hour, an externalized configuration is desired. Therefore, a similar functionality is a need in K8s runtime environment.

This is the fifth article of K8s knowhow series. In this article, I am going to demonstrate how K8s provides externalized configuration, flexible configuration and portability for containers. ConfigMaps allow you to decouple configuration artifacts from deployment artifacts, a capability that provides portability to containerized applications. For a production environment, the recommended approach is to externalize this configuration and ConfigMap file approach comes handy in this.

If you are familiar with the Spring Cloud ecosystem, then ConfigMap is similar to Spring Config server. There are two ways of using ConfigMaps.

  • Using ConfigMap as an environment variable
  • Mounting ConfigMap as file service

Let’s get into action. We will be using our Hello World application. The demonstration is based on Spring Boot, Docker, and K8s.

Using ConfigMap As an Environment Variable

In this case, we will create a new environment variable in K8s and it will be used in the code. Now in Java world, an environment variable can be used in the code using System.getenv(String) API. In a regular Java application, the environment variable can be set up in J2EE application containers such as Oracle WLS or IBM WAS or it can be set up in the OS. However, in K8s things are not the same. In order to use the environment variable we have to create config map from a literal.

Image title

Using the  kubectl create configmap  command, we have created two environment variables, app.name and app.desc. Environment variables are created using a literal.

Let's understand what happens behind the scenes.

Image title

Now pay attention to the data section. Under the data section, you will find key and value pairs. Technically, ConfigMap is mere a key-value store. The name of the property is the key and value of the property is the value. The application code requires you to just lookup for these key-value pairs.

In order to use this environment variable in a Java code, we just write the following code.

Image title

The below snippet defines two K8s environment variables named "SPRING_BOOT_HELLO_WORLD_APP_NAME" and "SPRING_BOOT_HELLO_WORLD_DESC." These variables will get the values from the ConfigMap app-env-config. The important point to note is the key.

Image title


Properties files are used to keep “N” number of properties in a single file to run the application in different environments. In a Spring Boot application, properties are kept in the application.properties file under the classpath. Let us take a look at application.properties file packed inside the application jar. This is the default file.

Image title

We are using the command  kubectl create configmap  to create a ConfigMap from an individual file, or from multiple files.

Now let us see the entire code.

Image title

Image title

Mounting ConfigMap as a File 

In this section, I will be illustrating how to use ConfigMap for mounting files to externalize the configuration. In this example, I will be externalizing the application.properties file using ConfigMap. Even though the default file is wrapped inside the jar, under src/main/resources. In simple terms, we are going to override the default file by supplying the file through ConfigMap.

The first step is to create ConfigMap from the application.properties. Let us understand how this ConfigMap is stored in K8s.

Image title

Through ConfigMap, we will mount the application.properties file into K8s cluster and it can be used in the application. Notice that the data section contains the contents of application.properties, and the key is the file name.

Now, in order to override the default configuration file, we need to mount the application.properties (through ConfigMap) in the classpath of application. Spring Boot provides this capability by providing different options. SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

  1. A /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

You can visit the Spring official page for more details.

The most simple and best way is to mount application.properties in the “/config ” directory.

Image title

Carefully, take a look at the mount path. Notice that the name of the ConfigMap should be exactly the same that we created above that is app-file-configmap. The key should be the name of the file. Also, ensure that name of volume mount configuration to that of volume configuration.

Image title

This snippet explains how any property defined in the application.properties file can be used in the application. It is a no-brainer as we are using the standard way recommended by Spring that is using @Value annotation to inject property value into a variable.

Now we are good to go ahead with our ConfigMap sample application. Let us take a look at the entire code snippet.

Image title

Let's create a Docker image and upload it to Dockerhub. In my case, the name of the image is k8s-springboot-helloworld-configmap--app.

The following is K8s pod configuration file.

Image title

Let us also create the service with a NodePort service type so that the welcome service can be used from outside the K8s cluster.

Image title

Now let's apply these changes into K8s.

Navigate to browser and access http://<minikube_ip>:<service_node_port>/welcome. In my case its, http:// 192.168.99.100:30880/welcome.

Scrupulously observe the output. The returned string is:

Image title

At the same time, check the default values of environment variables hardcoded in the code and the default value of property in the application.properties wrapped inside jar. You can figure out that values of environment variables and application.properties are getting fetched from ConfigMap.

This project is available on my github.

That’s it for now in this article. In the next article, we will see how to use ConfigMap’s “Secret” object. Stay tuned for more.

Kubernetes Spring Framework application Property (programming) Docker (software)

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot With Kubernetes
  • 7 Microservices Best Practices for Developers
  • Popular Tools Supporting YAML Data Format
  • Containerization and Helm Templatization Best Practices for Microservices in Kubernetes

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!