2

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

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
Abhijeet
  • 61
  • 3
  • 5

1 Answers1

0

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
Mikolaj S.
  • 2,850
  • 1
  • 5
  • 17
  • Sorry for misspelling. i tried to update files due to IP issue. I tried your suggestion by mounting file in the path "/usr/local/tomcat/webapps" and it is working perfectly fine without deleting existing files. "/usr/local/tomcat/webapps" directory is created by tomcat image and rest of the directories (/agent/WEB-INF/classes/conf) are created after I deploy my application – Abhijeet Aug 10 '21 at 18:46
  • Hi @Abhijeet, is your issue resolved or do you need support? – Mikolaj S. Aug 16 '21 at 10:17
  • Issue has not been resolved but as a WA, I have copied application.properties file to /var and Refereeing it from there – Abhijeet Aug 20 '21 at 06:40
  • It is possible that directory `/agent/WEB-INF/classes/` created by Kubernetes (when mounting file in this directory) has some other permissions so that means your script, application (or whatever you using to copy/create files) can't work properly, that's why directory is empty. Do you have some logs maybe? – Mikolaj S. Aug 20 '21 at 17:06
  • I'm getting below error. Do you have any suggestions? error: error validating "deployment-local.yaml": error validating data: ValidationError(Deployment.spec.template.spec.containers[0]): unknown field "volumes" in io.k8s.api.core.v1.Container; if you choose to ignore these errors, turn validation off with --validate=false – BNJ Sep 02 '22 at 12:33
  • The above validation error of unknown field "volumes" was due to incorrect indentation – BNJ Sep 02 '22 at 14:47