Friday, March 21, 2025

How to Create a ConfigMap with Multiple Files in Kubernetes?

 

How to Create a ConfigMap with Multiple Files in Kubernetes

In Kubernetes, a ConfigMap is used to store configuration data such as environment variables, configuration files, or command-line arguments. When working with multiple configuration files, you may need to create a ConfigMap where each file name acts as a key. This blog explains how to achieve that using different methods.

1. Creating a ConfigMap from Multiple Files

If you have multiple files and want to use their names as keys, you can use the following command:

kubectl create configmap my-config --from-file=/path/to/file1.txt --from-file=/path/to/file2.yaml --from-file=/path/to/file3.json -n my-namespace

Example:

If you have three files:

  • /config/file1.txt
  • /config/file2.yaml
  • /config/file3.json

Run:

kubectl create configmap my-config --from-file=/config/file1.txt --from-file=/config/file2.yaml --from-file=/config/file3.json

This will create a ConfigMap where:

  • file1.txt, file2.yaml, and file3.json will be keys.
  • The contents of these files will be stored as values.

2. Creating a ConfigMap from a Directory

If all configuration files are stored in a directory, you can create a ConfigMap from the entire directory:

kubectl create configmap my-config --from-file=/config-directory -n my-namespace

This will include all files in /config-directory as keys in the ConfigMap.

3. Creating a ConfigMap Using YAML

You can manually create a ConfigMap using a YAML file:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
  namespace: my-namespace
data:
  file1.txt: |
    This is the content of file1.
  file2.yaml: |
    key: value
  file3.json: |
    { "name": "example", "type": "json" }

Apply it using:

kubectl apply -f my-config.yaml

4. Mounting the ConfigMap in a Pod

Once created, you can mount the ConfigMap as files inside a pod:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: my-namespace
spec:
  containers:
    - name: my-container
      image: busybox
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config  # Files will be available here
  volumes:
    - name: config-volume
      configMap:
        name: my-config

5. Accessing ConfigMap Data

Once the ConfigMap is mounted inside the pod, you can access the files:

kubectl exec -it my-pod -- cat /etc/config/file1.txt

Conclusion

Using ConfigMaps in Kubernetes helps manage configuration files efficiently. Whether you create a ConfigMap from individual files, directories, or manually via YAML, Kubernetes makes it easy to inject configurations into your applications.

Let us know if you have any questions! 🚀

Reference

For more details, visit the official Kubernetes documentation: Kubernetes ConfigMap

No comments:

Post a Comment

Troubleshooting Docker Image Format: Ensuring Docker v2 Instead of OCI

  Troubleshooting Docker Image Format: Ensuring Docker v2 Instead of OCI Introduction While working with Docker 27+ , I encountered an iss...