Sub topic 2
What a ConfigMap is
This chapter assumes you can use kubectl, read YAML manifests, and know what a Pod and a namespace are. It covers the structure of a ConfigMap and its limits. Later chapters cover creating ConfigMaps and consuming them in Pods.
The idea
A ConfigMap is a namespaced Kubernetes object that stores non-sensitive configuration as key-value pairs. It lets you keep settings out of the container image. Pods read the values at runtime, either as environment variables or as files.
A ConfigMap has no spec. Its content sits at the top level of the object, in the data and binaryData fields.
Anatomy
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
LOG_LEVEL: "info"
app.properties: |
cache.ttl=60
cache.size=100This ConfigMap has two keys:
LOG_LEVELholds a short string.app.propertiesholds a multi-line value. The|is YAML block syntax that keeps the line breaks. The key looks like a file name because it can later be mounted as a file with that name.
Both values are stored as strings. Quote values that YAML could read as another type, such as "true", "8080" or "info". An unquoted 8080 is parsed as an integer, and the API server rejects it because data values must be strings.
data and binaryData
| Field | Holds | Encoding in the manifest |
|---|---|---|
| `data` | UTF-8 text | Plain text |
| `binaryData` | Arbitrary bytes, such as a small certificate or keystore | Base64 |
A key can appear in only one of the two fields. Both fields are optional, so an empty ConfigMap is valid.
Key names
Keys may contain letters, digits, -, _ and .. When you mount a ConfigMap, each key becomes a file name, so keep names valid for that. Environment variable consumers have an extra constraint: a key such as app.properties is not a valid shell variable name, so it cannot be used as one by default.
Limits and scope
A ConfigMap can hold at most 1 MiB in total, because it is stored in etcd, the cluster's data store. Larger files need a volume, an image layer, or a download at startup.
A ConfigMap exists in one namespace. A Pod can reference only ConfigMaps in its own namespace. To share settings across namespaces, you must create a copy in each one.
Inspecting a ConfigMap
kubectl get configmap app-config -o yaml
kubectl describe configmap app-configget -o yaml shows the stored object. describe prints each key and its value in a readable form.
How it fits together
Exercise
A teammate writes data: { PORT: 8080 } in a ConfigMap and kubectl apply fails. What is the cause, and what is the fix?
Show answer
Values under data must be strings, and YAML parses an unquoted 8080 as an integer. Quote it: PORT: "8080".
Exercise
Your application needs a 2 MiB settings file, and a Pod in namespace team-b needs to use a ConfigMap that exists in team-a. Can a ConfigMap do either?
Show answer
No to both. A ConfigMap is limited to 1 MiB in total, and a Pod can reference only ConfigMaps in its own namespace. Use another way to deliver the large file, and create a copy of the ConfigMap in team-b.