• Home
  • Docker
  • Kubernetes
  • Java
  • Ubuntu
  • Maven
  • Big Data
  • CI
  • Install
  • Samples
  • Archived
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
    Please visit 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):
    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:

      Check the ConfigMap:

      Describe the ConfigMap:

      View the YAML file of the ConfigMap:

    • --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):

      Create a sample file "file1.txt" (single line text):

      Create a sample file "file2.txt" (multiple lines text):

      • Create the ConfigMap from a file without a key:

        View the ConfigMap (notice that the content of the file is indented properly):

      • Create the ConfigMap from a file with a key:

        View the ConfigMap (notice that the content of the file is indented properly):

      • Create the ConfigMap from a directory:

        View the ConfigMap (notice that the content of the file is indented properly):

    • --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:

      Create the ConfigMap:

      View the ConfigMap (notice that each entry in the property file is represented by a key:value pairs in the ConfigMap):
  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:

    To apply the file:

    Here's a sample YAML with multi-line values entries:
  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":
  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":


      Note: You can give the environment variable a different name than the ConfigMap key name (i.e. name: mykey1).

      Apply the Pod:

      Check the environment variables:

    • 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:


      Apply the Pod:

      Check the environment variables:
  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).


      Apply the Pod:

      Check the files in the volume (notice two files are created: key1, key2):

      Check the content of the files:

    • 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".


      Apply the Pod:

      Check the files in the volume (notice the "key1path" file was created):

      Check the content of the files:
  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.


    Apply the Pod:

    Check the logs of the Pod:
  9. Delete ConfigMaps
    To delete a ConfigMap using the manifest file:

    To delete a ConfigMap using its name:
© 2025  mtitek