Kubernetes
ConfigMap
YAML
Configuration
From-File

kubernetes configmap set from-file in yaml configuration

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

kubectl create configmap --from-file is a convenient client-side generation feature, but Kubernetes YAML itself does not have a native from-file field. So if you want file-based ConfigMap input in a declarative workflow, the normal answer is to generate the YAML first or use a generation tool such as Kustomize.

What --from-file Actually Is

This command works:

bash
kubectl create configmap app-config   --from-file=app.properties=./config/app.properties   --from-file=log4j2.xml=./config/log4j2.xml

But this is not a real ConfigMap schema field:

yaml
# not valid Kubernetes ConfigMap schema
fromFile: ./config/app.properties

That distinction is the heart of the issue. File loading happens on the client side before the manifest reaches the API server.

Generate YAML from the Files

If you want the resulting manifest in version control, generate it explicitly:

bash
kubectl create configmap app-config   --from-file=app.properties=./config/app.properties   --from-file=log4j2.xml=./config/log4j2.xml   --namespace app   --dry-run=client -o yaml > app-config.yaml

Now app-config.yaml is an ordinary declarative manifest that can be reviewed and applied like any other Kubernetes resource.

What the Generated Manifest Looks Like

The generated ConfigMap just stores the file contents under keys in data.

yaml
1apiVersion: v1
2kind: ConfigMap
3metadata:
4  name: app-config
5  namespace: app
6data:
7  app.properties: |
8    log.level=INFO
9    feature.enabled=true
10  log4j2.xml: |
11    <Configuration status="WARN"></Configuration>

If you do not provide an explicit key name, the source filename becomes the key automatically.

Mounting the ConfigMap as Files

A common reason to load from file is to mount the content back into a container as files.

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: app
5  namespace: app
6spec:
7  replicas: 1
8  selector:
9    matchLabels:
10      app: app
11  template:
12    metadata:
13      labels:
14        app: app
15    spec:
16      containers:
17        - name: app
18          image: ghcr.io/example/app:1.0.0
19          volumeMounts:
20            - name: app-config-vol
21              mountPath: /etc/app
22      volumes:
23        - name: app-config-vol
24          configMap:
25            name: app-config

In that setup, each ConfigMap key becomes a mounted filename under /etc/app.

Kustomize Is the Declarative-Friendly Alternative

If you want to keep source files rather than generated YAML as the main artifact, Kustomize is usually the better choice.

yaml
1configMapGenerator:
2  - name: app-config
3    files:
4      - app.properties=./config/app.properties
5      - log4j2.xml=./config/log4j2.xml

Kustomize performs the generation before apply time, which fits a Git-based workflow more naturally than editing large multi-line data blocks by hand.

ConfigMap Updates and Rollouts

Even when the ConfigMap is generated correctly, workloads do not always reload the new content automatically. Some applications read the file only at startup, so teams often pair ConfigMap changes with a rollout restart or a checksum annotation on the pod template.

The generation step and the rollout step are separate concerns. Getting the ConfigMap right is only half of the operational workflow.

Common Pitfalls

  • Expecting native ConfigMap YAML to understand a from-file field is the most common misunderstanding.
  • Forgetting that the ConfigMap key becomes the mounted filename can break application config paths.
  • Mixing generated YAML and source files without a clear source of truth creates drift.
  • Using ConfigMaps for secrets simply because file generation is convenient is a security mistake.
  • Assuming an updated ConfigMap automatically means the application reloads its configuration can lead to surprise stale state.

Keep one authoritative source, either generated YAML or source files, so updates stay predictable.

Summary

  • '--from-file is a kubectl generation feature, not a built-in ConfigMap YAML field.'
  • Use kubectl ... --dry-run=client -o yaml if you want file-based input and declarative output.
  • Use Kustomize when you want generation to remain part of the deployment workflow.
  • ConfigMap keys become filenames when mounted.
  • Plan explicitly for how workloads consume ConfigMap updates after the manifest changes.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.