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

3

5

I created a deploymentconfig.yml to deploy an app and a want to mount a file which contents are stored in a ConfigMap. When mounting, the files in the folder that is mounted are replaced. The goal though is to only add/remove/overwrite a specific file in that folder.

spec:
    containers:
          volumeMounts:
          - mountPath: /path/toaSubPath/
            name: somename

Is this possible in this deploymentconfig? If so, how can I do that?

Woodborer answered 14/8, 2020 at 12:31 Comment(0)
C
6

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
Criticism answered 14/8, 2020 at 18:55 Comment(1)
suppose you want to mount the default.json, simply create a configmap with > kubctl create cm config --from-file default.jsonCriticism
L
5

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

root@ng:/usr/share/nginx/html# ls -la
total 16
drwxr-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.properties

kubectl create configmap game --from-file=game.properties --from-file=ui.properties

apiVersion: v1
kind: Pod
metadata:
  name: my
spec:
    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 -la
total 24
drwxr-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 localhost

enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30

verify ui.properties.txt

root@my:/usr/share/nginx/html# cat ui.properties.txt 
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

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

Lecture answered 17/8, 2020 at 13:4 Comment(1)
Great answer! Should be in the documentation.Valeric
R
4

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/null
deploymentconfig.apps.openshift.io/test created

// Create test.txt
$ cat <<EOF > test.txt
Test file
EOF

// 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: 30
volumes:
- 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.txt
Test 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 systemd
drwxr-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
Revenge answered 14/8, 2020 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.