I have multiple configuration files in two directories. For example,
I need to mount these configuration files in the same directory structure to kubernetes pod using ConfigMap
.
Tried using the
kubectl create configmap --from-file=./conf.d --from-file=./conf.d/node1/child1.conf --from-file=./conf.d/node2/child2.conf.
Config map created, as expected, cannot express the nested directory structure.
Is it possible to create ConfigMap recursively from folders and still retain the folder structure in the name of the key entry for the ConfigMap - since the intention is to mount these ConfigMaps into pods?
You can use kubectl create configmap to create a ConfigMap from multiple files in the same directory. When you are creating a ConfigMap based on a directory, kubectl identifies files whose basename is a valid key in the directory and packages each of those files into the new ConfigMap.
When we want to add a file to a ConfigMap we use the --from-file flag with the kubectl create configmap command. The most common use case for --from-file is adding individual files, but it can also target directories as well.
The simplest way to create a ConfigMap is to store a bunch of key-value strings in a ConfigMap YAML file and inject them as environment variables into your Pods. After that, you can reference the environment variables in your applications using whatever methods is necessary for your programming language.
Unfortunately, reflecting directory structure in configmap is not supported currently. Workaround is to express directory hierarchy like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: testconfig
data:
file1: |
This is file1
file2: |
This is file2 in subdir directory
---
apiVersion: v1
kind: Pod
metadata:
name: testpod
spec:
restartPolicy: Never
containers:
- name: test-container
image: gcr.io/google_containers/busybox
command: [ "/bin/sh","-c", "sleep 1000" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: testconfig
items:
- key: file2
path: subdir/file2
- key: file1
path: file1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With