This is a cache of https://docs.openshift.com/container-platform/4.9/applications/config-maps.html. It is a snapshot of the page at 2024-11-22T18:45:00.246+0000.
Using config maps with applications | Building applications | OpenShift Container Platform 4.9
×

Config maps allow you to decouple configuration artifacts from image content to keep containerized applications portable.

The following sections define config maps and how to create and use them.

For information on creating config maps, see Creating and using config maps.

Understanding config maps

Many applications require configuration using some combination of configuration files, command line arguments, and environment variables. In OpenShift Container Platform, these configuration artifacts are decoupled from image content to keep containerized applications portable.

The configmap object provides mechanisms to inject containers with configuration data while keeping containers agnostic of OpenShift Container Platform. A config map can be used to store fine-grained information like individual properties or coarse-grained information like entire configuration files or JSON blobs.

The configmap API object holds key-value pairs of configuration data that can be consumed in pods or used to store configuration data for system components such as controllers. For example:

configmap Object Definition
kind: configmap
apiVersion: v1
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data: (1)
  example.property.1: hello
  example.property.2: world
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3
binaryData:
  bar: L3Jvb3QvMTAw (2)
1 Contains the configuration data.
2 Points to a file that contains non-UTF8 data, for example, a binary Java keystore file. Enter the file data in Base 64.

You can use the binaryData field when you create a config map from a binary file, such as an image.

Configuration data can be consumed in pods in a variety of ways. A config map can be used to:

  • Populate environment variable values in containers

  • Set command-line arguments in a container

  • Populate configuration files in a volume

Users and system components can store configuration data in a config map.

A config map is similar to a secret, but designed to more conveniently support working with strings that do not contain sensitive information.

Config map restrictions

A config map must be created before its contents can be consumed in pods.

Controllers can be written to tolerate missing configuration data. Consult individual components configured by using config maps on a case-by-case basis.

configmap objects reside in a project.

They can only be referenced by pods in the same project.

The Kubelet only supports the use of a config map for pods it gets from the API server.

This includes any pods created by using the CLI, or indirectly from a replication controller. It does not include pods created by using the OpenShift Container Platform node’s --manifest-url flag, its --config flag, or its REST API because these are not common ways to create pods.

Use cases: Consuming config maps in pods

The following sections describe some uses cases when consuming configmap objects in pods.

Populating environment variables in containers by using config maps

Config maps can be used to populate individual environment variables in containers or to populate environment variables in containers from all keys that form valid environment variable names.

As an example, consider the following config map:

configmap with two environment variables
apiVersion: v1
kind: configmap
metadata:
  name: special-config (1)
  namespace: default (2)
data:
  special.how: very (3)
  special.type: charm (3)
1 Name of the config map.
2 The project in which the config map resides. Config maps can only be referenced by pods in the same project.
3 Environment variables to inject.
configmap with one environment variable
apiVersion: v1
kind: configmap
metadata:
  name: env-config (1)
  namespace: default
data:
  log_level: INFO (2)
1 Name of the config map.
2 Environment variable to inject.
Procedure
  • You can consume the keys of this configmap in a pod using configmapKeyRef sections.

    Sample Pod specification configured to inject specific environment variables
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: [ "/bin/sh", "-c", "env" ]
          env: (1)
            - name: SPECIAL_LEVEL_KEY (2)
              valueFrom:
                configmapKeyRef:
                  name: special-config (3)
                  key: special.how (4)
            - name: SPECIAL_TYPE_KEY
              valueFrom:
                configmapKeyRef:
                  name: special-config (3)
                  key: special.type (4)
                  optional: true (5)
          envFrom: (6)
            - configmapRef:
                name: env-config (7)
      restartPolicy: Never
    1 Stanza to pull the specified environment variables from a configmap.
    2 Name of a pod environment variable that you are injecting a key’s value into.
    3 Name of the configmap to pull specific environment variables from.
    4 Environment variable to pull from the configmap.
    5 Makes the environment variable optional. As optional, the pod will be started even if the specified configmap and keys do not exist.
    6 Stanza to pull all environment variables from a configmap.
    7 Name of the configmap to pull all environment variables from.

    When this pod is run, the pod logs will include the following output:

    SPECIAL_LEVEL_KEY=very
    log_level=INFO

SPECIAL_TYPE_KEY=charm is not listed in the example output because optional: true is set.

Setting command-line arguments for container commands with config maps

A config map can also be used to set the value of the commands or arguments in a container. This is accomplished by using the Kubernetes substitution syntax $(VAR_NAME). Consider the following config map:

apiVersion: v1
kind: configmap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
Procedure
  • To inject values into a command in a container, you must consume the keys you want to use as environment variables, as in the consuming configmaps in environment variables use case. Then you can refer to them in a container’s command using the $(VAR_NAME) syntax.

    Sample Pod specification configured to inject specific environment variables
    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: [ "/bin/sh", "-c", "echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ] (1)
          env:
            - name: SPECIAL_LEVEL_KEY
              valueFrom:
                configmapKeyRef:
                  name: special-config
                  key: special.how
            - name: SPECIAL_TYPE_KEY
              valueFrom:
                configmapKeyRef:
                  name: special-config
                  key: special.type
      restartPolicy: Never
    1 Inject the values into a command in a container using the keys you want to use as environment variables.

    When this pod is run, the output from the echo command run in the test-container container is as follows:

    very charm

Injecting content into a volume by using config maps

You can inject content into a volume by using config maps.

Example configmap custom resource (CR)
apiVersion: v1
kind: configmap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
Procedure

You have a couple different options for injecting content into a volume by using config maps.

  • The most basic way to inject content into a volume by using a config map is to populate the volume with files where the key is the file name and the content of the file is the value of the key:

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: [ "/bin/sh", "cat", "/etc/config/special.how" ]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configmap:
            name: special-config (1)
      restartPolicy: Never
    1 File containing key.

    When this pod is run, the output of the cat command will be:

    very
  • You can also control the paths within the volume where config map keys are projected:

    apiVersion: v1
    kind: Pod
    metadata:
      name: dapi-test-pod
    spec:
      containers:
        - name: test-container
          image: gcr.io/google_containers/busybox
          command: [ "/bin/sh", "cat", "/etc/config/path/to/special-key" ]
          volumeMounts:
          - name: config-volume
            mountPath: /etc/config
      volumes:
        - name: config-volume
          configmap:
            name: special-config
            items:
            - key: special.how
              path: path/to/special-key (1)
      restartPolicy: Never
    1 Path to config map key.

    When this pod is run, the output of the cat command will be:

    very