Search code examples
filekubernetesconfigurationconfigmap

Kubernetes ConfigMap mount single file instead of directory


Is it possible in Kubernetes to mount a file from a ConfigMap into an directory that already has other files? E.g.

Base image filesystem:

/app/
  main/
    main.py
    test.py

ConfigMap contains one file, mounted.py, which should be mounted in /app/main/ alongside main.py and test.py.

Desired filesystem after deployment:

/app/
  main/
    main.py
    test.py
    mounted.py

What I have seen so far is that the ConfigMap is mounted to a new directory, so the closest I have come is like this:

/app/
  main/
    main.py
    test.py
    mounted/
      mounted.py

If I mount the ConfigMap to /app/main, then it clobbers the existing files from the base image. E.g.

/app/
  main/
    mounted.py

Is there a good way to inject a single file like that? (Actually multiple files in my real use case.) I would prefer not to have separate directories for everything that will be injected by the ConfigMaps, since it deviates from the standard program architecture.


Solution

  • Use subPath:

    volumeMounts:
      - name: config
        mountPath: /app/main/mounted.py
        subPath: mounted.py
    

    The mountPath shows where it should be mounted, and subPath points to an entry inside the volume.

    Please be aware of the limitation: when using subPath the configMap updates won't be reflected in your file. See the official documentation for details.