• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Kubernetes | ConfigMaps (configmaps|cm)
  1. Notes
  2. ConfigMaps (configmaps|cm)
  3. Creating ConfigMaps using the kubectl create command
    --from-literal
    --from-file: from file without a key, from file with a key, from a directory
    --from-env-file
  4. Creating ConfigMaps using manifest YAML file
  5. Edit ConfigMaps
  6. Using ConfigMaps: Environment variables
    env/valueFrom/configMapKeyRef
    envFrom/configMapRef
  7. Using ConfigMaps: Files in Volumes
  8. Referencing ConfigMaps' environment variables in the container's command and arguments
  9. Delete ConfigMaps

  1. Notes
    See these pages for more details about ConfigMaps:
    https://kubernetes.io/docs/concepts/configuration/configmap/
    https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
  2. ConfigMaps (configmaps|cm)
    ConfigMaps allows creating data objects that can be consumed by Pods at runtime.

    Here's an example of a ConfigMap (YAML):
    apiVersion: v1
    data:
      key1: value1
      key2: value2
    kind: ConfigMap
    metadata:
      name: configmapexample
    The manifest file of the ConfigMap is pretty simple. It contains the fields apiVersion, kind, and metadata. It also contain the data field that contains the keys-values of the ConfigMap.

    The data in ConfigMaps is stored as key-value entries:
    • Key: is a string value formed by alphanumeric, dot (.), dash (-), and underscore (_) characters.

    • Value: is a string value that can contain any character, including carriage returns. For multiple lines, the data must be indented properly.

    The key and the value, in the ConfigMap, are separated with a colon (key:value).

    The name of the ConfigMap must be a valid DNS subdomain name.

    Examples of values that can be saved in ConfigMaps include:
    • Application settings.

    • Configuration files (json/xml/properties files, shell scripts, ...).

    • Base64 encoded data.

    ConfigMaps are not meant to store sensitive data (credentials, certificates, ...). Kubernetes Secrets should be used instead.

    The data stored in a ConfigMap can be consumed in a container in one the following ways:
    • Environment variables.

    • Command-line arguments of the container command.

    • Files in volumes.

    • Custom code (read the ConfigMap directly from the Kubernetes API).

    Note: If you update a ConfigMap that was already posted to the Kubernetes API server, then Pods that were already created will be able to leverage the new data only if it was injected as files in a volume. ConfigMaps data injected as environment variables won't be updated in a running Pod unless you recreate it.
  3. Creating ConfigMaps using the kubectl create command
    The data of the ConfigMaps can be specified using one of the following methods:

    • --from-literal:
      This command allows you to specify a key and literal value to insert in the ConfigMap.

      Create the ConfigMap:
      $ kubectl create configmap configmapfromliteral --from-literal="key1=value1" --from-literal="key2=value2"
      configmap/configmapfromliteral created
      Check the ConfigMap:
      $ kubectl get configmap configmapfromliteral
      NAME                   DATA   AGE
      configmapfromliteral   2      2m10s
      Describe the ConfigMap:
      $ kubectl describe configmap configmapfromliteral
      Name:         configmapfromliteral
      Namespace:    default
      Labels:       <none>
      Annotations:  <none>
      
      Data
      ====
      key1:
      ----
      value1
      
      key2:
      ----
      value2
      Events:  <none>
      View the YAML file of the ConfigMap:
      $ kubectl get configmap configmapfromliteral -o yaml
      apiVersion: v1
      data:
        key1: value1
        key2: value2
      kind: ConfigMap
      metadata:
        name: configmapfromliteral

    • --from-file:
      This command allows you to specify a file path (--from-file="./configmaps/file1.txt"), in which case the file base name (file1.txt) will be used as the ConfigMap key.

      Optionally you can use a key with the file path (--from-file="mykey1=./configmaps/file1.txt"), in which case the given key (mykey1) will be used as the ConfigMap key.

      If you specify a directory (--from-file="./configmaps/") then kubectl will iterate each file in the directory and create a new entry for each file whose base name is a valid ConfigMap key.

      The path to the file (or directory) can be an absolute path or relative to the location from where the "kubectl create configmap" is executed.

      The content of the file can be anything.

      Let's crate a directory to hold the ConfigMaps data (this can be any location you want):
      $ mkdir ./configmaps/
      Create a sample file "file1.txt" (single line text):
      $ echo -n 'value1' > "./configmaps/file1.txt"
      Create a sample file "file2.txt" (multiple lines text):
      $ echo 'value2-1' > "./configmaps/file2.txt"
      $ echo 'value2-2' >> "./configmaps/file2.txt"
      • Create the ConfigMap from a file without a key:
        $ kubectl create configmap configmap1fromfile --from-file="./configmaps/file1.txt" --from-file="./configmaps/file2.txt"
        configmap/configmap1fromfile created
        View the ConfigMap (notice that the content of the file is indented properly):
        $ kubectl get configmap configmap1fromfile -o yaml
        apiVersion: v1
        data:
          file1.txt: value1 # note that the name of the key is file1.txt and the value is single line
          file2.txt: | # note that the name of the key is file2.txt and the value is multiple lines
            value2-1
            value2-2
        kind: ConfigMap
        metadata:
          name: configmap1fromfile
      • Create the ConfigMap from a file with a key:
        $ kubectl create configmap configmap2fromfile --from-file="mykey1=./configmaps/file1.txt" --from-file="mykey2=./configmaps/file2.txt"
        configmap/configmap2fromfile created
        View the ConfigMap (notice that the content of the file is indented properly):
        $ kubectl get configmap configmap2fromfile -o yaml
        apiVersion: v1
        data:
          mykey1: value1 # note that the name of the key is mykey1 and the value is single line
          mykey2: | # note that the name of the key is mykey2 and the value is multiple lines
            value2-1
            value2-2
        kind: ConfigMap
        metadata:
          name: configmap2fromfile
      • Create the ConfigMap from a directory:
        $ kubectl create configmap configmapfromdirectory --from-file="./configmaps/"
        configmap/configmapfromdirectory created
        View the ConfigMap (notice that the content of the file is indented properly):
        $ kubectl get configmap configmapfromdirectory -o yaml
        apiVersion: v1
        data:
          file1.txt: value1 # note that the name of the key is file1.txt and the value is single line
          file2.txt: | # note that the name of the key is file2.txt and the value is multiple lines
            value2-1
            value2-2
        kind: ConfigMap
        metadata:
          name: configmapfromdirectory

    • --from-env-file:
      This command allows you to specify the path to an environement (properties) file (--from-file="./configmaps/file1.properties") to read lines of key=value pairs to insert in the ConfigMap.

      Create a sample property file:
      $ vi "./configmaps/file1.properties"
      key1=value1
      key2=value2
      Create the ConfigMap:
      $ kubectl create configmap configmapfromenvfile --from-env-file="./configmaps/file1.properties"
      configmap/configmapfromenvfile created
      View the ConfigMap (notice that each entry in the property file is represented by a key:value pairs in the ConfigMap):
      $ kubectl get configmap configmapfromenvfile -o yaml
      apiVersion: v1
      data:
        key1: value1
        key2: value2
      kind: ConfigMap
      metadata:
        name: configmapfromenvfile
  4. Creating ConfigMaps using manifest YAML file
    The manifest YAML file defines ConfigMags with key:value entries.

    You can define ConfigMags with keys and multi-line values entries. The key should be followed by the pipe character (|) and the value can span multiple lines. Note though that all the lines should be indented properly.

    Here's a sample YAML with simple values entries:
    $ vi configmapkeyvalue.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: configmapkeyvalue
    data:
      key1: value1
      key2: value2
    To apply the file:
    $ kubectl apply -f configmapkeyvalue.yaml
    configmap/configmapkeyvalue created
    Here's a sample YAML with multi-line values entries:
    $ vi configmapmultilinevalue.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: configmapmultilinevalue
    data:
      key3: |
        first line
        second line
        third line
  5. Edit ConfigMaps
    If you have created a ConfigMap using the manifest YAML file, then to change that ConfigMap you can adjust the YAML file and run the command kubectl apply -f CONFIGMAP-YAML-FILE-PATH

    You can also use the command kubectl edit configmap CONFIGMAP-NAME to edit a ConfigMap. The command will open the ConfigMap in your default text editor. Make your changes then save and exit the editor. Your changes will be submitted to API Server.

    For example to view and edit the ConfigMap "configmapkeyvalue":
    $ kubectl edit configmap configmapkeyvalue
    # Please edit the object below.
    # Lines beginning with a '#' will be ignored, and an empty file will abort the edit.
    # If an error occurs while saving this file will be reopened with the relevant failures.
    #
    apiVersion: v1
    data:
      key1: value1
      key2: value2
    kind: ConfigMap
    metadata:
      annotations:
        kubectl.kubernetes.io/last-applied-configuration: |
          {"apiVersion":"v1","data":{"key1":"value1","key2":"value2"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"configmapkeyvalue"}}
      name: configmapkeyvalue
    configmap/configmapkeyvalue edited
  6. Using ConfigMaps: Environment variables
    In a Pod YAML file, you can reference the entries of the ConfigMap directly in the "spec.containers.env" field. The environment variables will be set like any other standard Linux environment variables. You can check them using the env command directly from inside the container.

    • env/valueFrom/configMapKeyRef

      Let's use the ConfiMap "configmapkeyvalue" (see above) with the Pod "hello-busybox-configmap-keyref":

      $ vi hello-busybox-configmap-keyref.yaml
      apiVersion: v1
      kind: Pod
      metadata:
        name: hello-busybox-configmap-keyref
      spec:
        containers:
          - name: hello-busybox-configmap-keyref
            image: busybox:latest
            command: ['sh', '-c', 'sleep 300;']
            env:
              - name: mykey1
                valueFrom:
                  configMapKeyRef:
                    name: configmapkeyvalue
                    key: key1
              - name: mykey2
                valueFrom:
                  configMapKeyRef:
                    name: configmapkeyvalue
                    key: key2
      Note: You can give the environment variable a different name than the ConfigMap key name (i.e. name: mykey1).

      Apply the Pod:
      $ kubectl apply -f hello-busybox-configmap-keyref.yaml
      pod/hello-busybox-configmap-keyref created
      Check the environment variables:
      $ kubectl exec hello-busybox-configmap-keyref -- env | grep key
      mykey1=value1
      mykey2=value2
    • envFrom/configMapRef

      Instead of referencing individual entries of the ConfiMap, you can use the "spec.containers.envFrom" field to reference all entries of the ConfiMap:

      $ vi hello-busybox-configmap-ref.yaml
      apiVersion: v1
      kind: Pod
      metadata:
        name: hello-busybox-configmap-ref
      spec:
        containers:
          - name: hello-busybox-configmap-ref
            image: busybox:latest
            command: ['sh', '-c', 'sleep 300;']
            envFrom:
              - configMapRef:
                  name: configmapkeyvalue
      Apply the Pod:
      $ kubectl apply -f hello-busybox-configmap-ref.yaml
      pod/hello-busybox-configmap-ref created
      Check the environment variables:
      $ kubectl exec hello-busybox-configmap-ref -- env | grep key
      key1=value1
      key2=value2
  7. Using ConfigMaps: Files in Volumes
    ConfigMaps can also be referenced using volumes. Usually this is helpful when the ConfigMap contains configuration data (logging, ...).

    Each entry of the ConfigMap will be referenced as a separate file in the volume.

    The changes to the ConfigMap will be reflected directly in the volumes.

    The ConfigMap is defined as a volume (spec.volumes) and mounted to the container (spec.containers.volumeMounts).

    • In the following example, the spec.volumes field creates a volume (named configmapkeyvalue) from the ConfigMap "configmapkeyvalue".

      The spec.containers.volumeMounts field mounts the volume (configmapkeyvalue) into the container (under /tmp/configmapkeyvalue).

      The volume will be populated with the files inside the mounted directory. The files names are the keys of the ConfigMap (key1, key2). The content of the files are the values of the keys (value1, value2).

      $ vi hello-busybox-configmap-volume.yaml
      apiVersion: v1
      kind: Pod
      metadata:
        name: hello-busybox-configmap-volume
      spec:
        containers:
          - name: hello-busybox-configmap-volume
            image: busybox:latest
            command: ['sh', '-c', 'sleep 300;']
            volumeMounts:
              - name: configmapkeyvalue
                mountPath: /tmp/configmapkeyvalue
        volumes:
          - name: configmapkeyvalue
            configMap:
              name: configmapkeyvalue
      Apply the Pod:
      $ kubectl apply -f hello-busybox-configmap-volume.yaml
      pod/hello-busybox-configmap-volume created
      Check the files in the volume (notice two files are created: key1, key2):
      $ kubectl exec hello-busybox-configmap-volume -- ls -l /tmp/configmapkeyvalue
      lrwxrwxrwx    1    root    root    key1 -> ..data/key1
      lrwxrwxrwx    1    root    root    key2 -> ..data/key2
      Check the content of the files:
      $ kubectl exec hello-busybox-configmap-volume -- cat /tmp/configmapkeyvalue/key1
      value1
    • You can also decide what entries of the ConfigMag can be mounted.

      You can also decide the names of the files in the volume "path".

      $ vi hello-busybox-configmap-volume-item.yaml
      apiVersion: v1
      kind: Pod
      metadata:
        name: hello-busybox-configmap-volume-item
      spec:
        containers:
          - name: hello-busybox-configmap-volume-item
            image: busybox:latest
            command: ['sh', '-c', 'sleep 300;']
            volumeMounts:
              - name: configmapkeyvalue
                mountPath: /tmp/configmapkeyvalue
        volumes:
          - name: configmapkeyvalue
            configMap:
              name: configmapkeyvalue
              items:
                - key: key1
                  path: key1path
      Apply the Pod:
      $ kubectl apply -f hello-busybox-configmap-volume-item.yaml
      pod/hello-busybox-configmap-volume-item created
      Check the files in the volume (notice the "key1path" file was created):
      $ kubectl exec hello-busybox-configmap-volume-item -- ls /tmp/configmapkeyvalue
      key1path
      Check the content of the files:
      $ kubectl exec hello-busybox-configmap-volume-item -- cat /tmp/configmapkeyvalue/key1path
      value1
  8. Referencing ConfigMaps' environment variables in the container's command
    You can use the data of the ConfigMap to set the arguments of the container command.

    You need to use the following syntax to reference the environment variable: $(ENV_VAR_NAME).

    As previously mentioned, changes to the ConfigMap won't be reflected on the environment variables already set.

    $ vi hello-busybox-configmap-keyref-arg.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: hello-busybox-configmap-keyref-arg
    spec:
      containers:
        - name: hello-busybox-configmap-keyref-arg
          image: busybox:latest
          command: ['sh', '-c', 'echo "mykey1:$(mykey1) | mykey2:$(mykey2)"; sleep 300;']
          env:
            - name: mykey1
              valueFrom:
                configMapKeyRef:
                  name: configmapkeyvalue
                  key: key1
            - name: mykey2
              valueFrom:
                configMapKeyRef:
                  name: configmapkeyvalue
                  key: key2
    Apply the Pod:
    $ kubectl apply -f hello-busybox-configmap-keyref-arg.yaml
    pod/hello-busybox-configmap-keyref-arg created
    Check the logs of the Pod:
    $ kubectl logs hello-busybox-configmap-keyref-arg
    mykey1:value1 | mykey2:value2
  9. Delete ConfigMaps
    To delete a ConfigMap using the manifest file:
    $ kubectl delete -f configmapkeyvalue.yaml
    configmap "configmapkeyvalue" deleted
    To delete a ConfigMap using its name:
    $ kubectl delete configmap configmapkeyvalue
    configmap "configmapkeyvalue" deleted
© 2025  mtitek