ConfigMap YAML Manifests
You'll learn how to define ConfigMaps declaratively using YAML files with structured data and binary content fields.
The Declarative Approach
While you've created ConfigMaps imperatively with kubectl create configmap, production environments favor declarative manifests—YAML files that describe exactly what you want. This approach provides version control, repeatability, and clear documentation of your configuration.
Think of it like writing a recipe down versus calling out cooking instructions: the written recipe can be shared, reviewed, and reproduced exactly.
Basic ConfigMap Structure
A ConfigMap manifest has a standard Kubernetes structure with two key fields for content:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: default
data:
database.host: "postgres.example.com"
database.port: "5432"
log.level: "INFO"
The data field holds key-value pairs where values are UTF-8 strings. Keys become filenames when mounted as volumes or environment variable names when injected.
Multi-Line Configuration Files
For complex configs, use the pipe (|) character for multi-line strings:
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
server {
listen 80;
location / {
root /usr/share/nginx/html;
}
}
Binary Data Support
For non-UTF-8 content (images, compiled files), use binaryData with base64-encoded values:
apiVersion: v1
kind: ConfigMap
metadata:
name: binary-config
binaryData:
icon.png: iVBORw0KGgoAAAANS...
Apply any manifest with kubectl apply -f configmap.yaml to create or update the ConfigMap.
Key Takeaway: ConfigMap YAML manifests use data for text key-value pairs and binaryData for base64-encoded binary content, enabling declarative, version-controlled configuration management.