How to fetch configmap from kubernetes pod

7/3/2019

I have one spring boot microservice running on docker container, below is the Dockerfile

FROM java:8-jre
MAINTAINER <>
WORKDIR deploy/
#COPY config/* /deploy/config/
COPY ./ms.console.jar /deploy/
CMD chmod +R 777 ./ms.console.jar
CMD ["java","-jar","/deploy/ms.console.jar","console"]
EXPOSE 8384

here my configuration stores in external folder, i.e /config/console-server.yml and when I started the application, internally it will load the config (spring boot functionality).

Now I want to separate this configuration using configmap, for that I simply created one configmap and storing all the configuration details.

kubectl create configmap console-configmap --from-file=./config/console-server.yml

kubectl describe configmap console-configmap

below are the description details:

Name:         console-configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
console-server.yml:
----
server:
  http:
    port: 8385
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/xml,text/plain,text/css,application/javascript
    min-response-size: 2048

---
spring:
  thymeleaf:
    prefix: classpath:/static
  application:
    name: console-service
  profiles:
     active: native
  servlet:
    multipart:
      max-file-size: 30MB
      max-request-size: 30MB
---
host:
  gateway: http://apigateway:4000
  webhook: http://localhost:9000

my deployment yml is:

apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: consoleservice1
spec:
  selector:
    matchLabels:
      app: consoleservice
  replicas: 1 # tells deployment to run 3 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: consoleservice
    spec:
      containers:
      - name: consoleservice
        image: ms-console
        ports:
        - containerPort: 8384
        imagePullPolicy: Always
        envFrom:
        - configMapRef:
            name: console-configmap
      imagePullSecrets:
        - name: regcresd

My doubt is, I commented config folder in the Dockerfile, so while running pods, it's throwing exception because of no configuration, how I will inject this console-configmap to my deployment, what I tried already shared, but getting same issues.

-- Chintamani
kubernetes

2 Answers

7/3/2019

do you need to load key:value pairs from the config file as environment variables then below spec would work

envFrom:
        - configMapRef:
            name: console-configmap

if you need the config as a file inside pod then mount the configmap as volume. following link would be helpful https://kubernetes.io/docs/tutorials/configuration/configure-redis-using-configmap/

-- P Ekambaram
Source: StackOverflow

7/3/2019

First of all, how are you consuming the .yml file in your application? If you consume your yml file contents as environment variables, your config should just work fine. But I suspect that you want to consume the contents from the config file inside the container. If that is the case you have to create a volume out of the configmap as follows:

apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: consoleservice1
spec:
  selector:
    matchLabels:
      app: consoleservice
  replicas: 1 # tells deployment to run 3 pods matching the template
  template: # create pods using pod definition in this template
    metadata:
      labels:
        app: consoleservice
    spec:
      containers:
      - name: consoleservice
        image: ms-console
        ports:
        - containerPort: 8384
        imagePullPolicy: Always
        volumeMounts:
          - mountPath: /app/config
            name: config
      volumes:
        - name: config
          configMap:
            name: console-configmap
      imagePullSecrets:
        - name: regcresd

The file will be available in the path /app/config/console-server.yml. You have to modify it as per your needs.

-- Malathi
Source: StackOverflow