ConfigMap configuration
Why separate configuration from an image
Imagine a bookstore that uses the same application image in testing and production, but needs a different theme or page size. Configuration is the information that changes how the program behaves without changing its code. A ConfigMap stores non-confidential configuration as a Kubernetes object. It does not rebuild an image or change a program by itself: the Pod must consume its values, and the program must know what they mean. Use ConfigMaps for things like a display theme, feature settings and non-sensitive service names. Do not put credentials in them.
Create a ConfigMap
A ConfigMap is a namespaced core API object (apiVersion: v1). Its data maps keys to strings, while binaryData is for base64-encoded binary values. It has no spec section. Keys can include letters, digits, hyphens, underscores and dots. The same key cannot occur in both data fields. Quote numeric-looking and Boolean-looking values when they represent strings. A ConfigMap is limited to 1 MiB of data; use an appropriate storage system for large files. The following is an original offline fixture, not an instruction to apply it.
apiVersion: v1kind: ConfigMapmetadata: name: book-settings namespace: book-labdata: THEME: "light" MAX_RESULTS: "20" settings.properties: | theme=light pageSize=20There are three keys. MAX_RESULTS is the string 20, not a YAML integer; settings.properties is a multiline string. The application decides how to parse the number and file. Kubernetes does not interpret the properties format.
Inject selected keys or mount files
A Pod can read configuration through env[].valueFrom.configMapKeyRef, or use envFrom to import eligible keys as environment variables (optionally with a prefix). For configuration files, use a ConfigMap-backed volume and a volume mount. The items list can select keys and rename the files they become. These ordinary references resolve within the Pod namespace. A program that reads the Kubernetes API directly instead would need its own authorization and error handling.
apiVersion: v1kind: Podmetadata: name: book-reader namespace: book-labspec: automountServiceAccountToken: false containers: - name: reader image: example.invalid/bytemind/reader:offline env: - name: BOOK_THEME valueFrom: configMapKeyRef: name: book-settings key: THEME volumeMounts: - name: settings mountPath: /config readOnly: true resources: requests: cpu: 10m memory: 16Mi limits: cpu: 100m memory: 64Mi volumes: - name: settings configMap: name: book-settings items: - key: settings.properties path: settings.propertiesThe reader gets BOOK_THEME=light and a file at /config/settings.properties; MAX_RESULTS is not injected by this manifest. The image is intentionally non-deployable. The resource fields bound the illustrative workload; detailed resource management comes next.
Diagnose missing or incorrect settings
Check the object name, namespace and exact key spelling. A missing required ConfigMap or key can prevent the container from starting; with an optional reference a missing environment key is omitted rather than replaced by the literal text "null". A Pod may start with incomplete optional configuration, so the application needs a sensible default or a clear error. For ordinary non-secret ConfigMaps, the following inspection is appropriate only in an explicitly approved disposable cluster. It is not executed here.
kubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab get configmap book-settingskubectl --kubeconfig ./lab.kubeconfig --context bytemind-lab -n book-lab describe pod book-readerCheck the Pod events and compare the expected object name. A successful lookup does not prove that the application loaded the setting. Do not change namespace or credentials merely to work around missing permissions.
Practice: choose a delivery method
Read the offline book-settings.yaml, book-reader.yaml and key-resolution.json fixtures. Predict the environment variable and mounted file, then explain each missing-key case. Choose an environment variable for a small startup-only setting, and a mounted file for structured configuration that the application can reopen. In the next lesson, separate confidential values from ordinary settings.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
ConfigMap configuration — knowledge check
Apply the lesson concepts to original offline scenarios.
Which resource should hold the public display theme?
What is the type of the quoted MAX_RESULTS value in book-settings.yaml?
What file does the reader manifest mount?
The THEME key exists only in another namespace. What happens to the required reference?
Which mechanism imports eligible keys as environment variables?