How can I mount a file with the contents of key/value of a ConfigMap without clearing the folder? How can I mount a file with the contents of key/value of a ConfigMap without clearing the folder? kubernetes kubernetes

How can I mount a file with the contents of key/value of a ConfigMap without clearing the folder?


Yes, i am using this to mount default configs. Just use subPath and the file name in subPath. Find below sample it works like a charm

spec:  selector:     matchLabels:       name: some_name  template:    spec:      containers:      - args:        - bash        - entrypoint.sh        image: xyz        imagePullPolicy: IfNotPresent        name: some_name        volumeMounts:        - mountPath: /full/path/to/be/mounted/default.json          name: config          subPath: default.json      volumes:      - configMap:          defaultMode: 420          name: config


You can mount as a file configured in the ConfigMap using "subPath" as follows.This demonstration show how to mount only test.txt file configured into ConfigMap as "/etc/test.txt".

// Create test pod and deploymentconfig.$ oc run test --image registry.redhat.io/rhel7 -- tail -f /dev/nulldeploymentconfig.apps.openshift.io/test created// Create test.txt$ cat <<EOF > test.txtTest fileEOF// Create testmap ConfigMap using above test.txt file.$ oc create configmap testmap --from-file=test.txt// Modify volumes and volumeMounts for mounting only test.txt file to "/etc/test.txt".$ oc edit dc/test:containers:- name: test  :  volumeMounts:  - mountPath: /etc/test.txt    name: testtxt    subPath: test.txt:terminationGracePeriodSeconds: 30volumes:- configMap:    defaultMode: 420    items:    - key: test.txt      path: test.txt    name: testmap  name: testtxt:// You can verify test.txt after redeploying test pod after modification.$ oc rsh dc/test cat /etc/test.txtTest file// you can also verify test.txt file is only mounted to /etc directory as one specified file by subPath, not all directory.$ oc rsh dc/test ls -l /etc/total 888:drwxr-xr-x.  4 root root          151 Aug  3 09:13 systemddrwxr-xr-x.  2 root root            6 Aug 15  2017 terminfo-rw-r--r--.  1 root 1000110000     10 Aug 14 16:02 test.txt:drwxr-xr-x.  1 root root            6 Aug  3 09:37 yum.repos.d


Before we will apply desired configuration, this is default nginx configuration /usr/share/nginx/html:

root@ng:/usr/share/nginx/html# ls -latotal 16drwxr-xr-x 2 root root 4096 Aug 14 00:36 .drwxr-xr-x 3 root root 4096 Aug 14 00:36 ..-rw-r--r-- 1 root root  494 Aug 11 14:50 50x.html-rw-r--r-- 1 root root  612 Aug 11 14:50 index.html

Example custom configuration:

wget https://kubernetes.io/examples/configmap/game.properties wget https://kubernetes.io/examples/configmap/ui.propertieskubectl create configmap game --from-file=game.properties --from-file=ui.propertiesapiVersion: v1kind: Podmetadata:  name: myspec:    containers:    - name: nginx      image: nginx      volumeMounts:      - mountPath: /usr/share/nginx/html/index.html        name: data        subPath: game                    # make a reference to existing CM Key      - mountPath: /usr/share/nginx/html/ui.properties.txt        name: data        subPath: ui.properties.txt        # make a reference to existing CM Key    volumes:    - name: data      configMap:        name: game        items:        - key: game.properties          path: game        - key: ui.properties          path: ui.properties.txt

After pod deploy kubectl apply -f <your_pod_yaml>

root@my:/usr/share/nginx/html# ls -latotal 24drwxr-xr-x 1 root root 4096 Aug 17 12:26 .drwxr-xr-x 1 root root 4096 Aug 14 00:36 ..-rw-r--r-- 1 root root  494 Aug 11 14:50 50x.html-rw-r--r-- 1 root root  157 Aug 17 12:26 index.html-rw-r--r-- 1 root root   83 Aug 17 12:26 ui.properties.txt

verify index.html

root@my:/usr/share/nginx/html# curl localhostenemies=alienslives=3enemies.cheat=trueenemies.cheat.level=noGoodRottensecret.code.passphrase=UUDDLRLRBABASsecret.code.allowed=truesecret.code.lives=30

verify ui.properties.txt

root@my:/usr/share/nginx/html# cat ui.properties.txt color.good=purplecolor.bad=yellowallow.textmode=truehow.nice.to.look=fairlyNice

While sourcing your different files from ConfigMap make sure you are referencing proper keys