Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Configuring Spring Boot on Kubernetes with ConfigMap

October 3, 2017
Kamesh Sampath
Related topics:
JavaKubernetesMicroservicesSpring Boot
Related products:
Red Hat OpenShift Container Platform

Share:

    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.

    There are two ways to use ConfigMaps,

    1. ConfigMaps as Environment variables
    2. Mounting ConfigMaps as files

    ConfigMaps as Environment variables

    Assuming you have cloned my github repo, let's refer to the cloned location of the source code as $PROJECT_HOME throughout this document.

    You will notice that com.redhat.developers.GreeterController has a code to look up an environment variable GREETER_PREFIX.

    package com.redhat.developers;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @Slf4j
    public class GreeterController {
    
        @Value("${greeter.message}")
        private String greeterMessageFormat; 
    
        @GetMapping("/greet/{user}")
        public String greet(@PathVariable("user") String user) {
            String prefix = System.getenv().getOrDefault("GREETING_PREFIX", "Hi");
            log.info("Prefix :{} and User:{}", prefix, user);
            if (prefix == null) {
                prefix = "Hello!";
            }
    
            return String.format(greeterMessageFormat, prefix, user);
        }
    }

    By convention Spring Boot applications rather any Java applications passes these kind values via system properties. Let us now see how we can do the same with Kubernetes deployment.

    • Let's create a Kubernetes ConfigMaps to hold the property called greeter.prefix, which will then be injected into the Kubernetes deployment via an environment variable called GREETER_PREFIX.        

    Create ConfigMap          

    kubectl create configmap spring-boot-configmaps-demo --from-literal=greeter.prefix="Hello"
    •  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.

    spec:
      template:
        spec:
          containers:
            - env:
              - name: GREETING_PREFIX
                valueFrom:
                 configMapKeyRef:
                    name: spring-boot-configmaps-demo
                    key: greeter.prefix
    • 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".

    Mounting  ConfigMaps as files

    Kubernetes ConfigMaps also allows us to load a file as a ConfigMap property. That gives us an interesting option of loading the Spring Boot application.properties via Kubernetes ConfigMaps.

    To be able to load application.properties via ConfigMaps, we need to mount the ConfigMaps as the volume inside the Spring Boot application container.

    Update application.properties

    greeter.message=%s %s! Spring Boot application.properties has been mounted as volume on Kubernetes!

    Create ConfigMap from file

    kubectl create configmap spring-app-config --from-file=src/main/resources/application.properties

    The command above will create a configmap called spring-app-config with the application.properties file stored as one of the properties.

    The sample output ofkubectl get configmap spring-app-config -o yaml is shown below.

    apiVersion: v1
    data:
      application.properties: greeter.message=%s %s! Spring Boot application.properties has been mounted as volume on Kubernetes!
        on Kubernetes!
    kind: ConfigMap
    metadata:
      creationTimestamp: 2017-09-19T04:45:27Z
      name: spring-app-config
      namespace: default
      resourceVersion: "53471"
      selfLink: /api/v1/namespaces/default/configmaps/spring-app-config
      uid: 5bac774a-9cf5-11e7-9b8d-080027da6995

    Modifying GreeterController

    package com.redhat.developers;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @Slf4j
    public class GreeterController {
    
        @Value("${greeter.message}")
        private String greeterMessageFormat; 
    
        @GetMapping("/greet/{user}")
        public String greet(@PathVariable("user") String user) {
            String prefix = System.getenv().getOrDefault("GREETING_PREFIX", "Hi");
            log.info("Prefix :{} and User:{}", prefix, user);
            if (prefix == null) {
                prefix = "Hello!";
            }
    
            return String.format(greeterMessageFormat, prefix, user);
        }
    }

    Update Fragment deployment.yaml

    Update the deployment.yaml to add the volume mounts that will allow us to mount the application.properties under /deployments/config.

    spec:
      template:
        spec:
          containers:
            - env:
              - name: GREETING_PREFIX
                valueFrom:
                 configMapKeyRef:
                    name: spring-boot-configmaps-demo
                    key: greeter.prefix
              volumeMounts:
              - name: application-config 
                mountPath: "/deployments/config" 
                readOnly: true
          volumes:
          - name: application-config
            configMap:
              name: spring-app-config 
              items:
              - key: application.properties 
                path: application.properties

    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.

    The Spring Boot and Kubernetes Series

    How to Configure the Spring Boot Application on Kubernetes

    Part I: Configuring Spring Boot on Kubernetes with ConfigMap

    Part II: Configuring Spring Boot Kubernetes Secrets

    Last updated: May 30, 2023

    Recent Posts

    • AI meets containers: My first step into Podman AI Lab

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    • Upgrade from OpenShift Service Mesh 2.6 to 3.0 with Kiali

    • EE Builder with Ansible Automation Platform on OpenShift

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue