9.4. ConfigMaps
Similar to environment variables, ConfigMaps allow you to separate the configuration for an application from the image. Pods can access those variables at runtime which allows maximum portability for applications running in containers. In this lab, you will learn how to create and use ConfigMaps.
ConfigMap creation
A ConfigMap can be created using the oc create configmap
command as follows:
oc create configmap <name> <data-source> --namespace <namespace>
Where the <data-source>
can be a file, directory, or command line input.
Task 9.4.1: Java properties as ConfigMap
A classic example for ConfigMaps are properties files of Java applications which can’t be configured with environment variables.
First, create a file called java.properties
with the following content:
key=value
key2=value2
oc create configmap javaconfiguration --from-file=./java.properties --namespace <namespace>
Verify that the ConfigMap was created successfully:
oc get configmaps --namespace <namespace>
NAME DATA AGE
javaconfiguration 1 7s
Have a look at its content:
oc get configmap javaconfiguration -o yaml --namespace <namespace>
Which should yield output similar to this one:
apiVersion: v1
kind: ConfigMap
metadata:
name: javaconfiguration
data:
java.properties: |
key=value
key2=value2
Task 9.4.2: Attach the ConfigMap to a container
Next, we want to make a ConfigMap accessible for a container. There are basically the following possibilities to achieve this :
- ConfigMap properties as environment variables in a Deployment
- Command line arguments via environment variables
- Mounted as volumes in the container
In this example, we want the file to be mounted as a volume inside the container.
As in 8. Persistent storage, we can use the oc set volume
command to achieve this:
Note
If you are using Windows and your shell uses the POSIX-to-Windows path conversion, remember to prepend your command with MSYS_NO_PATHCONV=1
if the resulting mount path was mistakenly converted.
oc set volume deploy/example-web-app --add --configmap-name=javaconfiguration --mount-path=/etc/config --name=config-volume --type configmap --namespace <namespace>
Note
This task doesn’t have any effect on the example application inside the container. It is for demonstration purposes only.
This results in the addition of the following parts to the Deployment (check with oc get deploy example-web-app -o yaml
):
...
volumeMounts:
- mountPath: /etc/config
name: config-volume
...
volumes:
- configMap:
defaultMode: 420
name: javaconfiguration
name: config-volume
...
This means that the container should now be able to access the ConfigMap’s content in /etc/config/java.properties
. Let’s check:
oc exec <pod> --namespace <namespace> -- cat /etc/config/java.properties
Note
On Windows, you can use Git Bash withwinpty oc exec -it <pod> --namespace <namespace> -- cat //etc/config/java.properties
.key=value
key2=value2
Like this, the property file can be read and used by the application inside the container. The image stays portable to other environments.
Task 9.4.3: ConfigMap environment variables
Use a ConfigMap by populating environment variables into the container instead of a file.