How to mount entire directory in Kubernetes using configmap?

1/8/2018

I want to be able to mount unknown number of config files in /etc/configs

I have added some files to the configmap using:

kubectl create configmap etc-configs --from-file=/tmp/etc-config

Number of files and file names are never going to be known and I would like to recreate the configmap and the folder in the Kubernetes container should updated after sync interval.

I have tried to mount this but I'm not able to do so, the folder are always empty but I have data in the configmap.

bofh$ kubectl describe configmap etc-configs
Name:         etc-configs
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
file1.conf:
----
{
 ... trunkated ...
}
file2.conf:
----
{
 ... trunkated ...
}
file3.conf:
----
{
 ... trunkated ...
}

Events:  <none>

I'm using this one in the container volumeMounts:

- name: etc-configs
  mountPath: /etc/configs

And this is the volumes:

- name: etc-configs
  configMap:
    name: etc-configs

I can mount individual items but not an entire directory.

Any suggestions how to solve this?

-- Johan Ryberg
docker
kubernetes

2 Answers

1/9/2018

I'm feeling really stupid now.

Sorry, My fault.

The Docker container did not start so I was manually staring it using docker run -it --entrypoint='/bin/bash' and I could not see any files from the configMap.

This does not work since docker don't know anything about my deployment until Kubernetes starts it.

The docker image was failing and the Kubernetes config was correct all the time.

I was debugging it wrong.

-- Johan Ryberg
Source: StackOverflow

1/8/2018

With your config, you're going to mount each file listed in your configmap.

If you need to mount all file in a folder, you shouldn't use configmap, but a persistenceVolume and persistenceVolumeClaims:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-jenkins
spec:
  capacity:
    storage: 50Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/pv-jenkins"

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-jenkins
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 50Gi

In your deployment.yml:

 volumeMounts:
        - name: jenkins-persistent-storage
          mountPath: /data

  volumes:
        - name: jenkins-persistent-storage
          persistentVolumeClaim:
            claimName: pv-claim-jenkins

You can also use the following:

kubectl create configmap my-config --from-file=/etc/configs

to create the config map with all files in that folder.

Hope this helps.

-- Nicola Ben
Source: StackOverflow