How to Add Entire Directory of Files to a ConfigMap

Published: August 26, 2020 by Author's Photo Shane Rainville | Reading time: 2 minutes
Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

In this tutorial, you will learn how to store an entire directory of files in ConfigMap, and avoid having to add them all individually. The kubectl create configmap supports targeting an entire directory of files and storing each on as a data key within a new ConfigMap.

Most use cases of ConfigMap are to store parameters for an application. On occasion, individual files are stored for mounting in a deployment or pod. These files are typically single config files, such as my.conf or mongod.conf.

However, when your use case requires an entire directory of files to added to a ConfigMap, rather then add themes files individually we can add the entire directory.

Create 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.

If the value of the --from-file flag is a directory, kubectl will scan the directory and add each file as a key-value pair in the ConfigMap.

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

To verify each file in the directory was added as key-value pair in the ConfigMap you can kubectl get it. Output the queried resource in YAML to verify each file was added with its contents.

kubectl get configmap config-files -o yaml
apiVersion: v1
data:
  client.conf: |
    parameter2=test
    parameter3=test    
  routes.conf: |
        parameter4=http://www.example.com
  server.conf: |
        parameter1=hello
kind: ConfigMap
metadata:
  creationTimestamp: "2020-08-27T03:56:54Z"
  name: config-files
  namespace: default
  resourceVersion: "536004"
  selfLink: /api/v1/namespaces/default/configmaps/config-files
  uid: 314a5692-987e-4aed-97cf-77615e7812e3

We can see three keys, one for each file that was scanned. The files were client.conf, routes.conf, and server.conf. Each file’s contents can also be seen.

ConfigMaps have a hard 1MB size limit. If the contents of your files exceeds 1MB you will not be able to store them in a single ConfigMap. A Persistent Volume may be a better solution.

Conclusion

While most ConfigMap use cases are for storing parameters or a few individual files, there are times when an entire directory of files must be added. In this tutorial, you learned that the --from-file flag for the kubectl create configmap command can do more than just add single files, it can add an entire directory of files.

Last updated on September 9, 2020 by Shane Rainville: Update introduction 982b47c0ef9f5e54e7ca2db6e3de09ceacee0cd8
Author Photo
Blogger, Developer, pipeline builder, cloud engineer, and DevSecOps specialist. I have been working in the cloud for over a decade and running containized workloads since 2012, with gigs at small startups to large financial enterprises.

How to Configure Node-based apps in Kubernetes

Publised September 9, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Create Immutable Configmaps and Secrets

Publised August 27, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Effectively use Kubernetes Quality of Service

Publised October 6, 2021 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Deploy Jekyll on Kubernetes

Publised September 15, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Update Kubernetes Deployments

Publised September 11, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Backup and Restore MongoDB Deployment on Kubernetes

Publised September 3, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Immediately Start Kubernetes CronJobs Manually

Publised September 2, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.

How to Copy Files to a Pod Container in Kubernetes

Publised August 27, 2020 by Shane Rainville

Learn how to add an entire directory of files to your ConfigMap for mounting in a deployment or pod.