Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount entire directory in Kubernetes using configmap?

I want to be able to mount an 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

The 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 be updated after sync interval.

I have tried to mount this but I'm not able to do so, the folder is 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 about how to solve this?

like image 217
Johan Ryberg Avatar asked Jan 08 '18 12:01

Johan Ryberg


2 Answers

You can mount the ConfigMap as a special volume into your container.

In this case, the mount folder will show each of the keys as a file in the mount folder and the files will have the map values as content.

From the Kubernetes documentation:

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
...
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
like image 107
sola Avatar answered Oct 13 '22 09:10

sola


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.

like image 30
Johan Ryberg Avatar answered Oct 13 '22 08:10

Johan Ryberg