How to create a Kubernetes ConfigMap

Application settings in Kubernetes often need to move independently from container images. A ConfigMap stores non-secret key-value data in the cluster so Pods can receive runtime values without rebuilding the image or hardcoding environment-specific strings.

ConfigMaps are namespaced API objects with data fields for UTF-8 strings and binaryData fields for base64-encoded bytes. kubectl create configmap can build one from literal values, files, directories, or environment-style files; literal values fit small application settings that need a quick cluster-side object.

Passwords, tokens, certificates, and API keys belong in Secrets rather than ConfigMaps. A disposable namespace keeps validation isolated while non-secret keys such as APP_MODE and WELCOME_MESSAGE prove that the ConfigMap exists and that a Pod can read its values.

Steps to create a Kubernetes ConfigMap:

  1. Create a namespace for the ConfigMap test.
    $ kubectl create namespace app-config
    namespace/app-config created

    Use the application namespace instead of app-config when adding a real ConfigMap to an existing workload.

  2. Create the ConfigMap from non-secret literal values.
    $ kubectl create configmap web-config --namespace app-config --from-literal=APP_MODE=production --from-literal="WELCOME_MESSAGE=Hello from ConfigMap"
    configmap/web-config created

    Each --from-literal value becomes one key under the ConfigMap data field.

  3. Inspect the stored ConfigMap data.
    $ kubectl describe configmap web-config --namespace app-config
    Name:         web-config
    Namespace:    app-config
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    APP_MODE:
    ----
    production
    
    WELCOME_MESSAGE:
    ----
    Hello from ConfigMap
    
    
    BinaryData
    ====
    
    Events:  <none>
  4. Create a Pod manifest that imports all ConfigMap keys as environment variables.
    configmap-check-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: configmap-check
      namespace: app-config
    spec:
      restartPolicy: Never
      containers:
        - name: configmap-check
          image: busybox:1.36
          command: ["sh", "-c", "echo APP_MODE=$APP_MODE; echo WELCOME_MESSAGE=$WELCOME_MESSAGE; sleep 300"]
          envFrom:
            - configMapRef:
                name: web-config

    Keys imported through envFrom must be valid environment variable names. Use a ConfigMap volume mount when the application expects a configuration file instead of environment variables.

  5. Apply the Pod manifest.
    $ kubectl apply -f configmap-check-pod.yaml
    pod/configmap-check created
  6. Wait for the Pod to become ready.
    $ kubectl wait --for=condition=Ready pod/configmap-check --namespace app-config --timeout=90s
    pod/configmap-check condition met

    The short sleep command keeps the test container running long enough for the readiness check and log readback.

  7. Read the Pod logs to confirm the ConfigMap values reached the container.
    $ kubectl logs configmap-check --namespace app-config
    APP_MODE=production
    WELCOME_MESSAGE=Hello from ConfigMap
  8. Delete the lab namespace when the ConfigMap test is finished.
    $ kubectl delete namespace app-config
    namespace "app-config" deleted

    Skip this command for a real application namespace. Delete only the test Pod and test ConfigMap if the namespace contains other workloads.