Using Property files in your Openshift applications

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
  creationTimestamp: 2017-03-17T19:12:11Z
  name: sample-config
  namespace: default
data: hello world |-

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~   --context-dir=openshift-demo

Now we will have the following Deployment Configuration available:

$ oc get dc
myapp     0          1         0         config,image(myapp:latest)

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

$ oc create configmap sample-config

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 ->

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

Follow us on Twitter