Access Kubernetes ConfigMap in Spring Boot Application

 Hello There,

Welcome back to our new post on Kubernetes tutorials. In this example we will see how can we configure and access ConfigMap in Spring boot application.

ConfigMaps are helpful where you have multiple environment (Dev,Test Prod etc) and you want to promote your container with different environment configuration. There are three ways Pods can access ConfigMap in containers.










For todays Demo , we will use Environment Variable approach.

Pre-requisite : MiniKube Cluster  

For Demo Purpose I have created Spring Boot application which will expose below end point. 

curl localhost:8080/configMap


Docker Image : shashivish123/springboot 

Below is how Controller class looks like and value is being autowired from application.properties.
















Remember CONFIG_KEY value will be coming from Kubernetes ConfigMap Environment Configuration.

Now Let's Begin , We will start by Creating firs ConfigMap in Kubernetes.

Step 1: Create a namespace where we will deploy container and config maps.





Step 2: Now lets create a ConfigMap with name "app-config" and create key value pair.

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  config-key: Hello-cAs-Unified 

Notice in above ConfigMap file we have create a key-value pair config-key: Hello-cAs-Unified

Step 3 : Alright our config Map file is ready , now we will go ahead apply it in Kubernetes Cluster.

kubectl create -f app-config.yaml -n casunified 

Once you apply it , you can confirm changes by below command.

kubectl get configMap -n casunified  






kubectl describe configMaps app-config -n casunified













Step 4 : Cool, Our ConfigMap have be successfully created. Now lets quickly created our Spring Boot Pod which will access ConfigMap

kubectl create deployment configdemo --image=shashivish123/springboot:latest --dry-run=client -o=yaml > deployment.yaml

kubectl create service clusterip configdemo --tcp=8080:8080 --dry-run=client -o=yaml > service.yaml

Step 5 : Edit deployment.yaml file which was created in above steps and add below code under container.

        env:
          - name : CONFIG_KEY
            valueFrom:
               configMapKeyRef:
                    name : app-config
                    key: config-key

This is how your deployment.yaml file will look like now.




















Step 6 : Final step is to apply deployment and Service configuration and enable port forwarding.

kubectl create -f deployment.yaml -n casunified 

kubectl create -f service.yaml -n casunified 

kubectl port-forward service/configdemo 8080:8080 -n casunified






Step 7 : Perfect , try accessing below URL on your browser , you should below response from application.

 localhost:8080/configMap











Hope blog was helpful.

Keep learning , Keep Sharing :)

Comments

Post a Comment

Popular posts from this blog

JDBC Hive Connection fails : Unable to read HiveServer2 uri from ZooKeeper

Developing Custom Processor in Apache Nifi