How to put a whole directory and its subdirectories with a ConfigMap in a Kubernetes Pod on a multi-node cluster?

10/15/2019

Problem

  • We want to reuse the same image
  • We have different static files (e.g. css) that should vary among different deployments. The folder-structure of these files should be preserved.

e.g:

  1. test1.xml
  2. test2.xml
  3. layout

    3.1 test1.css

    3.1 test2.css

We need to put those files once (at startup) into the Pod.

As far as I know, I can only create one ConfigMap of all files in one directory-level and a I have to recreate the structure.

e.g.

kubectl create configmap style-files --from-file=.

In our example above, this command would only create a ConfigMap for the xml-files.

Expected Behaviour

Something that behaves like a ConfigMap, but is able to create a single ConfigMap that contains all files in the current directory and all files of the subdirectory while preserving the folder structure.

Question

Is there some concept that is made for my use case or do I have to use ConfigMaps?

-- Andre Hartmann
configmap
docker
kubernetes
volume

3 Answers

10/15/2019

I would suggest using configmaps but you'll probably need to use two of them with two mounts to recreate the hierarchy since configmaps are flat.

Mount one with your XML to /your/path/ and the other containing the CSS to /your/path/layout.

May require clever use of the subpath key to avoid the first cm overwriting the 2nd.

-- switchboard.op
Source: StackOverflow

10/15/2019

You could provide an archive file at a location accessible from within the cluster (f.e. s3 storage or simple http server) and use the config map to configure the right URL for the pod to download the files. To download and extract the archive you can make use of an init container combined with a volume mount.

More information on init containers: https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

-- Thomas
Source: StackOverflow

10/16/2019

Example files in the directory:

.
├── test21.css
├── test22.css
├── test2.xml
└── test.xml

Create configmap:
    kubectl create configmap example --from-file=./

example configmap:

apiVersion: v1
data:
  test.xml: |
    test1
    test1
  test2.xml: |
    test2
    test2
  test21.css: |
    test21
    test21
  test22.css: |
    test22
    test22
kind: ConfigMap

Example pod with the volume where ConfigMap keys are projected:

apiVersion: v1
kind: Pod
metadata:
  name: busy
spec:
  containers:
  - name: busybox
    image: k8s.gcr.io/busybox
    command: ["/bin/sh"]
    args: ["-c", "sleep 200"]
    volumeMounts:
    - mountPath: /test
      name: data1
  volumes:
    - name: data1
      configMap:
        name: example
        items:
        - key: test.xml
          path: test.xml
        - key: test2.xml
          path: test2.xml
        - key: test21.css
          path: layout/test21.css
        - key: test22.css
          path: layout/test22.css

Note:

You can project keys to specific paths and specific permissions on a per-file basis.

You can combine this example with different sources like secrets and configmaps using projected volume: A projected volume maps several existing volume sources into the same directory.

apiVersion: v1
kind: Pod
metadata:
  name: busy
spec:
  containers:
  - name: busybox
    image: k8s.gcr.io/busybox
    command: ["/bin/sh"]
    args: ["-c", "sleep 200"]
    volumeMounts:
    - mountPath: /test
      name: data1
  volumes:
    - name: data1
      projected:
        sources:
        - configMap:
            name: example
            items:
              - key: test.xml
                path: test.xml
              - key: test2.xml
                path: test2.xml
              - key: test21.css
                path: layout/test21.css
              - key: test22.css
                path: layout/test22.css

Another approach is to use zip/jar file as configmap (configmap support binary file) so after mounting it can be unzipped into desired path inside your container or using init container to prepare appropriate folder structure or build images with repopulated data.

Resources:

Hope this help.

-- Hanx
Source: StackOverflow