This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Questo contenuto non è disponibile nella lingua selezionata.
Chapter 15. Creating and using config maps
The following sections define config maps and how to create and use them.
15.1. Understanding ConfigMaps Copia collegamentoCollegamento copiato negli appunti!
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 in order 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
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.
15.2. Creating a config map in the OpenShift Container Platform web console Copia collegamentoCollegamento copiato negli appunti!
You can create a config map in the OpenShift Container Platform web console.
Procedure
To create a config map as a cluster administrator:
-
In the Administrator perspective, select
Workloads
Config Maps
. - At the top right side of the page, select Create Config Map.
- Enter the contents of your config map.
- Select Create.
-
In the Administrator perspective, select
To create a config map as a developer:
-
In the Developer perspective, select
Config Maps
. - At the top right side of the page, select Create Config Map.
- Enter the contents of your config map.
- Select Create.
-
In the Developer perspective, select
15.3. Creating a config map Copia collegamentoCollegamento copiato negli appunti!
You can use the following command to create a config map from directories, specific files, or literal values.
Procedure
Create a config map:
oc create configmap <configmap_name> [options]
$ oc create configmap <configmap_name> [options]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.3.1. Creating a config map from a directory Copia collegamentoCollegamento copiato negli appunti!
You can create a config map from a directory. This method allows you to use multiple files within a directory to create a config map.
Procedure
The following example procedure outlines how to create a config map from a directory.
Start with a directory with some files that already contain the data with which you want to populate a config map:
ls example-files
$ ls example-files
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
game.properties ui.properties
game.properties ui.properties
Copy to Clipboard Copied! Toggle word wrap Toggle overflow cat example-files/game.properties
$ cat example-files/game.properties
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow cat example-files/ui.properties
$ cat example-files/ui.properties
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice
color.good=purple color.bad=yellow allow.textmode=true how.nice.to.look=fairlyNice
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
ConfigMap
holding the content of each file in this directory by entering the following command:oc create configmap game-config \ --from-file=example-files/
$ oc create configmap game-config \ --from-file=example-files/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow When the
--from-file
option points to a directory, each file directly in that directory is used to populate a key in theConfigMap
, where the name of the key is the file name, and the value of the key is the content of the file.For example, the previous command creates the following
ConfigMap
:oc describe configmaps game-config
$ oc describe configmaps game-config
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can see that the two keys in the map are created from the file names in the directory specified in the command. Because the content of those keys might be large, the output of
oc describe
only shows the names of the keys and their sizes.Enter the
oc get
command for the object with the-o
option to see the values of the keys:oc get configmaps game-config -o yaml
$ oc get configmaps game-config -o yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.3.2. Creating a ConfigMap from a file Copia collegamentoCollegamento copiato negli appunti!
You can create a config map from a file.
Procedure
The following example procedure outlines how to create a config map from a file.
If you create a config map from a file, you can include files containing non-UTF8 data that are placed in this field without corrupting the non-UTF8 data. OpenShift Container Platform detects binary files and transparently encodes the file as MIME
. On the server, the MIME
payload is decoded and stored without corrupting the data.
You can pass the --from-file
option multiple times to the CLI. The following example yields equivalent results to the creating from directories example.
Create a
ConfigMap
specifying a specific file:oc create configmap game-config-2 \ --from-file=example-files/game.properties \ --from-file=example-files/ui.properties
$ oc create configmap game-config-2 \ --from-file=example-files/game.properties \ --from-file=example-files/ui.properties
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the results:
oc get configmaps game-config-2 -o yaml
$ oc get configmaps game-config-2 -o yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
You can specify the key to set in a ConfigMap
for content imported from a file. This can be set by passing a key=value
expression to the --from-file
option. For example:
Create a
ConfigMap
specifying a key-value pair:oc create configmap game-config-3 \ --from-file=game-special-key=example-files/game.properties
$ oc create configmap game-config-3 \ --from-file=game-special-key=example-files/game.properties
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the results:
oc get configmaps game-config-3 -o yaml
$ oc get configmaps game-config-3 -o yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- This is the key that you set in the preceding step.
15.3.3. Creating a config map from literal values Copia collegamentoCollegamento copiato negli appunti!
You can supply literal values for a config map.
Procedure
The --from-literal
option takes a key=value
syntax that allows literal values to be supplied directly on the command line.
Create a
ConfigMap
specifying a literal value:oc create configmap special-config \ --from-literal=special.how=very \ --from-literal=special.type=charm
$ oc create configmap special-config \ --from-literal=special.how=very \ --from-literal=special.type=charm
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify the results:
oc get configmaps special-config -o yaml
$ oc get configmaps special-config -o yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.4. Use Cases: Consuming ConfigMaps in pods Copia collegamentoCollegamento copiato negli appunti!
The following sections describe some uses cases when consuming ConfigMap
objects in pods.
15.4.1. Populating environment variables in containers by using config maps Copia collegamentoCollegamento copiato negli appunti!
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
ConfigMap
with one environment variable
Procedure
You can consume the keys of this
ConfigMap
in a pod usingconfigMapKeyRef
sections.Sample
Pod
specification configured to inject specific environment variablesCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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 5
- Name of the
ConfigMap
to pull specific environment variables from. - 4 6
- Environment variable to pull from the
ConfigMap
. - 7
- Makes the environment variable optional. As optional, the Pod will be started even if the specified
ConfigMap
and keys do not exist. - 8
- Stanza to pull all environment variables from a
ConfigMap
. - 9
- 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_LEVEL_KEY=very log_level=INFO
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
SPECIAL_TYPE_KEY=charm
is not listed in the example output because optional: true
is set.
15.4.2. Setting command-line arguments for container commands with ConfigMaps Copia collegamentoCollegamento copiato negli appunti!
A ConfigMap 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 ConfigMaps:
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 variablesCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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
very charm
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
15.4.3. Injecting content into a volume by using config maps Copia collegamentoCollegamento copiato negli appunti!
You can inject content into a volume by using config maps.
Example ConfigMap
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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- File containing key.
When this pod is run, the output of the cat command will be:
very
very
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You can also control the paths within the volume where
ConfigMap
keys are projected:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Path to
ConfigMap
key.
When this pod is run, the output of the cat command will be:
very
very
Copy to Clipboard Copied! Toggle word wrap Toggle overflow