How to put a whole directory and its subdirectories with a ConfigMap in a Kubernetes Pod on a multi-node cluster?
Example files in the directory:
.├── test21.css├── test22.css├── test2.xml└── test.xmlCreate configmap: kubectl create configmap example --from-file=./
example configmap:
apiVersion: v1data: test.xml: | test1 test1 test2.xml: | test2 test2 test21.css: | test21 test21 test22.css: | test22 test22kind: ConfigMap
Example pod with the volume where ConfigMap keys are projected:
apiVersion: v1kind: Podmetadata: name: busyspec: 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: v1kind: Podmetadata: name: busyspec: 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:
- Configure a projected volume for a pod
- Configure a Pod to Use a ConfigMap
- unofficial-kubernetes
- Getting Started with Kubernetes InitContainers and Volume pre-population
Hope this help.
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/
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.