ConfigMap

This page describes Kubernetes' ConfigMap object and its use in Kubernetes Engine.

What is a ConfigMap?

ConfigMaps bind configuration files, command-line arguments, environment variables, port numbers, and other configuration artifacts to your Pods' containers and system components at runtime. ConfigMaps allow you to separate your configurations from your Pods and components, which helps keep your workloads portable, makes their configurations easier to change and manage, and prevents hardcoding configuration data to Pod specifications.

ConfigMaps are useful for storing and sharing non-sensitive, unencrypted configuration information. To use sensitive information in your clusters, you must use Secrets.

Creating a ConfigMap

You create a ConfigMap using the following command:

kubectl create configmap [NAME] [DATA]

[DATA] can be:

  • a path to a directory containing one or more configuration files, indicated using the --from-file flag
  • key-value pairs, each specified using --from-literal flags

For more information about kubectl create, refer to the reference documentation.

You can also create a ConfigMap by defining a ConfigMap object in a YAML manifest file and deploying the object using kubectl create -f [FILE].

From files

To create a ConfigMap from one or more files, use --from-file. You specify files in any plaintext format, such as .properties, .txt, or .env, as long as the files contain key-value pairs.

You can pass in a single file or multiple files:

kubectl create configmap [NAME] --from-file [/PATH/TO/FILE.PROPERTIES] --from-file [/PATH/TO/FILE2.PROPERTIES]

You can also pass in a directory containing multiple files:

kubectl create configmap [NAME] --from-file [/PATH/TO/DIRECTORY]

When you create a ConfigMap based on files, the key defaults to the basename of the file, and the value defaults to the file's content. You can also specify an alternate key.

For ConfigMaps based on directories, each file whose basename is a valid key in the directory is packaged into the ConfigMap. Non-regular files, such as symlinks, devices, and pipes, are ignored by kubectl. Subdirectories are also ignored; kubectl create configmap does not recurse into subdirectories.

For example, the following command creates a ConfigMap called game-data from a directory, game-configs, which contains two properties files: game.properties and ui.properties:

kubectl create configmap game-data --from-file game-configs/

Running kubectl get configmap game-data -o yaml returns the following output:

apiVersion: v1
data:
  game.properties: |
    enemies=aliens
    lives=3
    ...
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    ...
kind: ConfigMap
metadata:
  creationTimestamp: ...
  name: game-data
  namespace: default
  resourceVersion: ...
  selfLink: /api/v1/namespaces/default/configmaps/game-data
  uid: ...

From literal values

To create a ConfigMap from literal values, use --from-literal.

For example, the following command creates a ConfigMap named literal-data with two key-value pairs:

kubectl create configmap literal-data --from-literal key1=value1 --from-literal key2=value2

You specify --from-literal for each key-value pair.

Running kubectl get configmap literal-data -o yaml returns the following output:

apiVersion: v1
data:
  key1: value1
  key2: value2
kind: ConfigMap
metadata:
  creationTimestamp: ...
  name: literal-data
  namespace: default
  resourceVersion: ....
  selfLink: /api/v1/namespaces/default/configmaps/literal-data
  uid: ...

Using ConfigMaps

To use a ConfigMap with your workloads, you can specify an environment variable that reference the ConfigMap's values, or mount a volume containing the ConfigMap.

To learn about using ConfigMaps, refer to Configure a Pod to Use a ConfigMap.

What's next

Was this page helpful? Let us know how we did:

Send feedback about...

Kubernetes Engine