A common need for most application is to store its configuration in an external configuration files. This logic can indeed be pursued also when moving to the cloud, by using a resource named ConfigMap. Let's see how.

The ConfigMap API can be used to hold key-value pairs of configuration data that can be used in pods where applications are running. ConfigMap are pretty much the same as Secrets, just they don't hold sensitive information but just plain Strings.  Here's the most basic example of a ConfigMap:

kind: ConfigMap
apiVersion: v1
metadata:
  creationTimestamp: 2017-03-17T19:12:11Z
  name: sample-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

As you can see, the data field contains in the ConfigMap resource can hold fine-grained information like individual properties or coarse-grained information like the contents of configuration files.

Configuring a Property File for WildFly

ConfigMaps can be used as well in a separate property file which can be made available through the Deployment Configuration. Let's see how, start by creating a new WildFly application:

$ oc new-app --name=myapp openshift/wildfly~https://github.com/fmarchioni/mastertheboss   --context-dir=openshift-demo

Now we will have the following Deployment Configuration available:

$ oc get dc
NAME      REVISION   DESIRED   CURRENT   TRIGGERED BY
myapp     0          1         0         config,image(myapp:latest)

Now we will create a config file named sample-config from a Property file named file.properties:

$ oc create configmap sample-config  --from-file=file.properties

Done with the ConfigMap, we can inject it into the Pod in two ways:

Populating the ConfigMap in a Volume:

$ oc volume dc/myapp --overwrite --add -t configmap  -m /opt/app-root/src --name=sample-config --configmap-name=sample-config

With the above command we have mounted the ConfigMap as a Volume which is available in the Path /opt/app-root/src of the Pod. As a proof of concept let's do a remote login on the pod:

$ oc rsh eap-4-3wcnd
sh-4.2$ ls -al
drwxrwsrwx. 3 root    1000040000 4096 Mar 31 13:03 .
drwxr-xr-x. 4 default root       4096 Feb  1 12:35 ..
lrwxrwxrwx. 1 root    root         22 Mar 31 13:03 file.properties -> ..data/file.properties

Mounting ConfigMap into the Environment

The other option is mounting your ConfigMap as environment variable. This is simpler as it does not require

$ oc env dc/myapp --from=configmap/sample-config 

You can check that your environment contains the properties by listing it as follows:

$ oc env dc/myapp --list
0
0
0
s2smodern

Related articles available on mastertheboss.com

Openshift installation quick tutorial

IMPORTANT: This tutorial is now outdated. Openishift origin has b

Java EE example application on Openshift

In this tutorial we will learn how to deploy a Java EE applicatio

Running tomcat Docker image on Openshift

A common issue when porting Docker images on Openshift is that th

Deploy Docker images on Openshift

This tutorial will teach you how you can build and deploy a custo

Accessing Openshift services remotely

In this article we will learn how to connect to services running

Helloworld NodeJS on Openshift

NodeJS provides an event-driven and asynchronous platform for ser