ConfigMaps
What is configmaps¶
Many applications require configuration using some combination of configuration files, command line arguments, and environment variables. These configuration artifacts should be decoupled from image content in order to keep containerized applications portable. The ConfigMap object provides mechanisms to inject containers with configuration data
kind: ConfigMap
apiVersion: v1
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: example-config
namespace: default
data:
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
Configuration data can be consumed in pods in a variety of ways. A ConfigMap can be used to:
- Populate the value of environment variables.
- Set command-line arguments in a container.
- Populate configuration files in a volume.
Both users and system components may store configuration data in a ConfigMap.
Creating Configmaps¶
You can use the following command to create a ConfigMap easily from directories, specific files, or literal values
kubectl create configmap <map-name> <data-source>
Creating from directories¶
You can use the following command to create a ConfigMap holding the content of each file in this directory:
kubectl create configmap game-config --from-file=<somefoler>/
When the --from-file option points to a directory, each file directly in that directory is used to populate a key in the ConfigMap, where the name of the key is the file name, and the value of the key is the content of the file.
$ ls example-files
game.properties
ui.properties
$ cat example-files/game.properties
lives=3
enemies.cheat=true
$ cat example-files/ui.properties
color.good=purple
color.bad=yellow
$ kubectl create configmap game-config --from-file=example-files/
configmap/game-config created
$ kubectl describe configmaps game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
game.properties: 60 bytes
ui.properties: 56 bytes
kubectl get configmaps game-config -o yaml
apiVersion: v1
data:
game.properties: |
lives=3
enemies.cheat=true
ui.properties: |
color.good=purple
color.bad=yellow
kind: ConfigMap
metadata:
creationTimestamp: 2019-01-21T03:30:26Z
name: game-config
namespace: default
resourceVersion: "532531"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: e4cc90d6-1d2c-11e9-8aa0-02c56126773a
You can also pass the --from-file option with a specific file, and pass it multiple times to the CLI.
kubecgtl create configmap game-config-2 \
--from-file=example-files/game.properties \
--from-file=example-files/ui.properties
Use the option --from-env-file to create a ConfigMap from an env-file.
- Env-files contain a list of environment variables.
- Each line in an env file has to be in VAR=VAL format.
- Lines beginning with # (i.e. comments) are ignored.
- Blank lines are ignored.
- There is no special handling of quotation marks (i.e. they will be part of the ConfigMap value)).
$ cat game-env-file.properties
enemies=aliens
lives=3
allowed="true"
# This comment and the empty line above it are ignored
$ kubectl create configmap game-config-env-file --from-env-file=game-env-file.properties
$ kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
allowed: '"true"'
enemies: aliens
lives: "3"
kind: ConfigMap
metadata:
creationTimestamp: 2017-12-27T18:36:28Z
name: game-config-env-file
namespace: default
resourceVersion: "809965"
selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
uid: d9d1ca5b-eb34-11e7-887b-42010a8002b8
Creating from Literal Values¶
You can use kubectl create configmap with the --from-literal argument to define a literal value from the command line:
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: 2016-02-18T19:14:38Z
name: special-config
namespace: default
resourceVersion: "651"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: dadce046-d673-11e5-8cd0-68f728db1985
Consuming Configmaps in pods¶
Consuming in Environment Variables¶
A sample configmap
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.how: very
special.type: charm
You can consume the keys of this ConfigMap in a pod using configMapKeyRef sections:
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:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.how
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: special.type
optional: true
restartPolicy: Never
Configure all key-value pairs in a ConfigMap as container environment variables
Use envFrom to define all of the ConfigMap’s data as container environment variables. The key from the ConfigMap becomes the environment variable name in the Pod.
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
Consuming in Volumes¶
A ConfigMap can also be consumed in volumes.
You have a couple different options for consuming this ConfigMap in a volume. The most basic way 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
The examples in this section refer to a ConfigMap named special-config, shown below.
apiVersion: v1
kind: ConfigMap
metadata:
name: special-config
namespace: default
data:
special.level: very
special.type: charm
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
restartPolicy: Never
When the pod runs, the command ("ls /etc/config/") produces the output below:
special.level
special.type
Caution
1 |
|
Add ConfigMap data to a specific path in the Volume¶
Use the path field to specify the desired file path for specific ConfigMap items. In this case, the special.level item will be mounted in the config-volume volume at /etc/config/keys.
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh","-c","cat /etc/config/keys" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
items:
- key: special.level
path: keys
restartPolicy: Never
When the pod runs, the command ("cat /etc/config/keys") produces the output below:
very