Replacing properties file in container using configmaps in kubernetes

Viewed 1717

I am trying to replace properties file in container using configMap and volumeMount in deployment.yaml file. Below is my deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
   name: deployment-properties
spec:
   selector:
      matchLabels:
      app: agent-2
   replicas: 2
   template:
      metadata:
      labels:
         app: agent-2
      spec:
         containers:
         - name: agent-2
           image: agent:latest
           ports:
         - containerPort: 8080
           volumeMounts:
         -  mountPath: "/usr/local/tomcat/webapps/agent/WEB-INF/classes/conf/application.properties"
           name: "applictaion-conf"
           subPath: "application.properties"
         volumes:
          - name: applictaion-conf
            configMap:
             name: dddeagent-configproperties
             items:
              - key: "application.properties"
                path: "application.properties"

Below is snippet from configMap:

apiVersion: v1
kind: ConfigMap
metadata:
    name: agent-configp
data:
   application.properties: |-
      AGENT_HOME = /var/ddeagenthome
      LIC_MAXITERATION=5
    LIC_MAXDELAY=10000

After deployment, complete folder structure is getting mounted instead of single file. Because of which all the files are getting deleted from existing folder.

Version - 1.21.13

1 Answers

I checked this configuration and there are few misspelling. You are referring to config map "dddeagent-configproperties" but you have defined a ConfigMap object named as "agent-configp".

configMap: name: dddeagent-configproperties

Should be:

configMap: name: agent-configp

Besides that there a few indentation errors, so I will paste a fixed files at the end of the answer.

To the point of your question: your approach is correct and as I tested in my setup everything was working properly without any issues. I created a sample pod with mounted the ConfigMap the same way you are doing it (in the directory where there are other files). The ConfigMap was mounted as a file as it should and other files were still available in the directory.

Mounts: /app/upload/test-folder/file-1 from application-conf (rw,path="application.properties")

Your approach is the same as described here.

Please double check that on the pod without mounted config map the directory /usr/local/tomcat/webapps/agent/WEB-INF/classes/conf really exists and other files are here. As your image is not public avaiable, I checked with the tomcat image and /usr/local/tomcat/webapps/ directory is empty. Note that even if this directory is empty, the Kubernetes will create agent/WEB-INF/classes/conf directories and application.properties file here, when you want to mount a file.

Fixed deployment and ConfigMap files with good indentation and without misspellings:

Deployment file:

apiVersion: apps/v1
kind: Deployment
metadata:
   name: deployment-properties
spec:
   selector:
      matchLabels:
        app: agent-2
   replicas: 2
   template:
     metadata:
       labels:
         app: agent-2
     spec:
       containers:
       - name: agent-2
         image: agent:latest
         ports:
         - containerPort: 8080
         volumeMounts:
         - mountPath: "/usr/local/tomcat/webapps/agent/WEB-INF/classes/conf/application.properties"
           name: "application-conf"
           subPath: "application.properties"
       volumes:
       - name: application-conf
         configMap:
           name: agent-configp
           items:
           - key: "application.properties"
             path: "application.properties"

Config file:

apiVersion: v1
kind: ConfigMap
metadata:
    name: agent-configp
data:
   application.properties: |-
      AGENT_HOME = /var/ddeagenthome
      LIC_MAXITERATION=5
      LIC_MAXDELAY=1000
Related