0
0
KubernetesHow-ToBeginner · 3 min read

How to Create ConfigMap from File in Kubernetes

Use the kubectl create configmap command with the --from-file option to create a ConfigMap from a file. For example, kubectl create configmap my-config --from-file=path/to/file creates a ConfigMap named my-config containing the file's data.
📐

Syntax

The basic syntax to create a ConfigMap from a file is:

  • kubectl create configmap <configmap-name> --from-file=<file-path>

Here:

  • kubectl create configmap: Command to create a ConfigMap.
  • <configmap-name>: Name you assign to the ConfigMap.
  • --from-file=<file-path>: Path to the file whose contents will be stored in the ConfigMap.
bash
kubectl create configmap <configmap-name> --from-file=<file-path>
💻

Example

This example creates a ConfigMap named app-config from a file called settings.conf. The file contains configuration data that your application can use.

bash
kubectl create configmap app-config --from-file=settings.conf
Output
configmap/app-config created
⚠️

Common Pitfalls

Common mistakes when creating ConfigMaps from files include:

  • Using a wrong file path or filename, causing the command to fail.
  • Not specifying the --from-file flag, which results in an error.
  • Expecting the ConfigMap to update automatically when the file changes; ConfigMaps are static after creation.

Always double-check the file path and name before running the command.

bash
kubectl create configmap my-config settings.conf
# This is incorrect because --from-file is missing

kubectl create configmap my-config --from-file=wrongfile.conf
# This fails if wrongfile.conf does not exist

kubectl create configmap my-config --from-file=settings.conf
# Correct usage
Output
error: unknown flag: --from-file error: the path specified does not exist configmap/my-config created
📊

Quick Reference

CommandDescription
kubectl create configmap my-config --from-file=path/to/fileCreate ConfigMap named my-config from a single file
kubectl create configmap my-config --from-file=dir/Create ConfigMap from all files in a directory
kubectl get configmap my-config -o yamlView the ConfigMap content in YAML format
kubectl delete configmap my-configDelete the ConfigMap named my-config

Key Takeaways

Use 'kubectl create configmap --from-file=' to create a ConfigMap from a file.
Ensure the file path is correct to avoid errors during creation.
ConfigMaps do not update automatically if the source file changes; recreate to update.
You can create ConfigMaps from single files or entire directories.
Use 'kubectl get configmap -o yaml' to inspect the ConfigMap content.