DEV Community

Kubernetes - Using ConfigMap SubPaths to Mount Files

Josh Duffney on November 13, 2019

What if you want to mount a configuration file from a ConfigMap, but do not want to mount it as a volume? You can accomplish this by using SubPaths...
Collapse
 
thoki123 profile image
thoki123

I tried your steps with OpenShift v4.19 (Kubernetes 1.19) and it didn't work. The directory (which is a Tomcat 9 deployment) is still overwritten by the single file (logback.xml) in the volume

kind: ConfigMap
apiVersion: v1
metadata:
  name: logback-configmap
data:
  logback: |
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration scan="true" scanPeriod="60 seconds">
[...]
Enter fullscreen mode Exit fullscreen mode

and in the deployment config:

[...]
spec:
      volumes:
        - name: logback-configmap-volume
          configMap:
            name: logback-configmap
            items:
              - key: logback
                path: logback.xml
            defaultMode: 420
[..]
         volumeMounts:
            - name: logback-configmap-volume
              mountPath: /deployments/zfaRouter/WEB-INF/classes/logback.xml
              subPath: logback.xml
[..]
Enter fullscreen mode Exit fullscreen mode
sh-4.4$ ls -la /deployments/zfaRouter/WEB-INF/classes/
total 4
drwxr-xr-x. 2 root root         25 May  4 16:35 .
drwxr-xr-x. 3 root root         21 May  4 16:35 ..
-rw-r--r--. 1 root 1015170000 1084 May  4 16:35 logback.xml
Enter fullscreen mode Exit fullscreen mode

Do you have any idea why this is not working?

Collapse
 
reshma profile image
reshmagcp • Edited

I have followed the method mentioned in the blog and it is working. Thanks for this really useful content. But I am facing one issue now which is mentioned below.

I have a kind: Deployment as below where I am mounting rs-config.yaml ConfigMap(which contains a ss.yml file) as volume. With the below configuration I am able to get the ss.yml file in the specified mountPath: C:................\App_Data
Apart from ss.yml I need another directory named DATA in the same path(C:................\App_Data) for which I used the subPath. With the below code I am able to get DATA directory but the contents in the DATA directory are not getting completely copied(subdirectories and some files are missing which are required for our application to be up).

name: volume-rs
configMap:
name: rs-config
items:
key: ss.yml
path: remote/AppData/
containers:
name: remote
image: registry_name
volumeMounts:
name: volume-rs
mountPath: C:................\App_Data\ss.yml
subPath: remote/AppData/

Can anyone suggest a workaround for this? Any help would be grateful. Thanks in advance.

Collapse
 
tejsinghrana profile image
Tej-Singh-Rana

Appreciated well explained.

Collapse
 
nicksc423 profile image
nicksc423

I think there's a typo, you config map is named: mysql-config but you always seem to reference: mysql-configmap in the deployment

Collapse
 
joshduffney profile image
Josh Duffney

great catch, indeed a typo. I appreciate you taking the time to point that out. I've updated the configmap name to mysql-configmap. :)

Collapse
 
bumarcell profile image
bumarcell

I believe you don't have to list the items inside the volume for it to work.