Skip to main content
This lesson demonstrates how to create and consume a Kubernetes ConfigMap from a Deployment (example: CartsDB). You’ll see:
  • The ConfigMap manifest and the key/value pairs it stores.
  • The relevant Deployment snippet that consumes those keys via configMapKeyRef.
  • The commands and expected behavior when the Deployment is applied before the ConfigMap exists, and what changes after the ConfigMap is created.

ConfigMap manifest

File: carts-configmap.yaml
This ConfigMap stores simple key/value pairs: ConfigMaps are intended for non-sensitive configuration data. For secrets, use Kubernetes Secrets instead.

Deployment snippet that consumes the ConfigMap

File: carts-db-deployment.yaml This is the essential part of the Deployment that consumes the ConfigMap values via configMapKeyRef. It focuses on the container, ports, and environment variables:
Note how env.valueFrom.configMapKeyRef references both:
  • the ConfigMap resource name: cartsdb-config
  • the specific key within it: MONGODB_USER and MONGODB_DATABASE
The ConfigMap must exist in the same namespace so the kubelet can populate those environment variables when creating the container.
A ConfigMap is a separate Kubernetes resource. If the Deployment references a ConfigMap that does not exist when the Pod is created, the Pod will fail to start with an error like CreateContainerConfigError until the ConfigMap is created.

Reproduce the failure (apply Deployment before ConfigMap)

Try deploying the Deployment before creating the ConfigMap to observe the failure:
When the referenced ConfigMap doesn’t exist, the Pod will fail to start. In the OpenShift console the Pod status will show CreateContainerConfigError (example screenshot):
Screenshot of the Red Hat OpenShift web console showing the "Pods" view in the default project. A pod named "carts-db-67d8869bbf-..." is listed with status "CreateContainerConfigError" and 0/1 ready.
Inspect the Pod events in the console or run:
You will see an event explaining that the ConfigMap cartsdb-config was not found.

Create the ConfigMap and recover the Pod

Create the ConfigMap, then reapply the Deployment or delete the failed Pod so the ReplicaSet recreates it:
Example output when creating the ConfigMap:
After the ConfigMap exists, the Pod can resolve the referenced keys and should transition to Running. Example OpenShift console view showing the Pod in Running state:
A screenshot of the Red Hat OpenShift web console showing Pod details for "carts-db-67d8869bbf-nk974" in the "default" project with status "Running." The page displays metadata like namespace, labels, restart policy and Pod IP, with the left sidebar highlighting Workloads → Pods.
You can also inspect the Pod YAML to confirm metadata and IP information:

Quick reference

Summary

  • Store non-sensitive configuration data in a ConfigMap as key/value pairs.
  • Reference ConfigMap values from a Pod using env.valueFrom.configMapKeyRef.
  • Ensure the ConfigMap exists before the Pod is created; otherwise the Pod will fail with CreateContainerConfigError.
  • For secrets or sensitive information, use Kubernetes Secrets instead of ConfigMaps.
Do not store sensitive credentials in a ConfigMap. Use Kubernetes Secrets for passwords, tokens, and private keys.

Watch Video