Mounting ConfigMap as Volume deletes application folder content

8/6/2021

I am trying to mount configMap as a volume inside the Pod. But when I tried using the subPath option, it doesn't seem to work. My application folder under tomcat/webapps/myapp-0.1 gets deleted and gets replaced with the data mentioned in mountPath (check deployment YAML)

Given below is the YAML file. I have commented out few things. But I tried running the YAML with and without the subPath option.

Deployment.yml

---
apiVersion: v1
kind: Service
metadata:
  name: web-app-test
  namespace: pocns
spec:
  selector:
    app: web-app-test
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80
      nodePort: 30036
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app-test
  labels:
    app: web-app-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-app-test
  template:
    metadata:
      labels:
        app: web-app-test
    spec:
      containers:
      - name: web-app-test
        image: xznacr.azurecr.io/testrepo/webappimage:147
        ports:
        - containerPort: 80
        volumeMounts:
        - name: configmap-vol 
          mountPath: /usr/local/tomcat/webapps/myapp-0.1/WEB-INF/classes/config.properties
          subPath: config.properties
      volumes:
        - name: configmap-vol
          configMap:
            name: dev-cm
      #       items:
      #       - key: config.properties
      #         path: config.properties
      # restartPolicy: Always

Config map: Given below is the config map

apiVersion: v1
data:
  config.properties: DEV=testing_in_development
kind: ConfigMap
metadata:
  creationTimestamp: "2021-08-05T12:25:37Z"
  name: dev-cm
  namespace: pocns
  resourceVersion: "458829"
  selfLink: /api/v1/namespaces/pocns/configmaps/dev-cm
  uid: 03a393ba-ad75-49db-9230-4086ccf256f5

When I get inside the shell of the pod I could see the rest of the contents under myapp-0.1 folder is wiped out-

# kubectl exec -it -n=pocns web-app-test-b494b55fc-smd7x bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

# cd webapps
# ls -l
total 20
drwxr-xr-x 1 root root 4096 Aug  6 09:41 ./
drwxr-xr-x 1 root root 4096 Jul 29 00:08 ../
drwxr-xr-x 3 root root 4096 Aug  6 09:41 myapp-0.1/
-rw-r--r-- 1 root root 3621 Aug  5 06:40 myapp-0.1.war

# cd myapp-0.1
# ls -l
total 12
drwxr-xr-x 3 root root 4096 Aug  6 09:41 ./
drwxr-xr-x 1 root root 4096 Aug  6 09:41 ../
drwxr-xr-x 3 root root 4096 Aug  6 09:41 WEB-INF/

# cd WEB-INF/classes/
# ls -l
total 12
drwxr-xr-x 2 root root 4096 Aug  6 09:41 ./
drwxr-xr-x 3 root root 4096 Aug  6 09:41 ../
-rw-r--r-- 1 root root   26 Aug  6 09:41 config.properties

Expected Output: Below folders are the application folders and I would want only the file (configMap) to get mounted under /usr/local/tomcat/webapps/myapp-0.1/WEB-INF/classes without removing other files.

# ls -l
total 20
drwxr-x--- 4 root root 4096 Aug  6 09:55 ./
drwxr-xr-x 1 root root 4096 Aug  6 09:55 ../
-rw-r----- 1 root root  600 Aug  5 06:38 index.jsp
drwxr-x--- 3 root root 4096 Aug  6 09:55 META-INF/
drwxr-x--- 3 root root 4096 Aug  6 09:55 WEB-INF/

Can you please advise how to mount the configMap as a volume under a specific app directory without rest of the files and folders from getting deleted?

-- tiny
configmap
containers
kubernetes
properties-file

1 Answer

8/6/2021

You mount the file in the directory try

apiVersion: v1
data:
  config.properties: |-
    DEV=testing_in_development
kind: ConfigMap
metadata:
  name: dev-cm
  namespace: pocns

deployment file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app-test
  labels:
    app: web-app-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-app-test
  template:
    metadata:
      labels:
        app: web-app-test
    spec:
      containers:
      - name: web-app-test
        image: xznacr.azurecr.io/testrepo/webappimage:147
        ports:
        - containerPort: 80
        volumeMounts:
        - name: configmap-vol 
          mountPath: /usr/local/tomcat/webapps/myapp-0.1/WEB-INF/classes
      volumes:
        - name: configmap-vol
          configMap:
            name: dev-cm

now if you to subpath specific file

containers:
- volumeMounts:
  - name: demo-config
    mountPath: /usr/local/tomcat/webapps/myapp-0.1/WEB-INF/classes/config.properties
    subPath: config.properties
volumes:
- name: demo-config
  configMap:
    name: dev-cm

you can also check out : https://github.com/kubernetes/kubernetes/issues/44815#issuecomment-297077509

-- Harsh Manvar
Source: StackOverflow