In the Pipelines with OpenShift example we promoted the application from Development to Testing. But what about those pesky environment variables? What if they must take on different values depending on the deployed environment? Welcome to the kubernetes’s configmap feature, brought to you by Selby. This lab is another in the OpenShift MiniLabs series.
Objective
Configmaps enable you to separate dynamic configuration from static runtime software. This is recommended as it simplifies promotion. Application configuration details are injected into the image at runtime. The alternative is error-prone and fragile environment specific configuration instructions. OpenShift makes using configmaps easy. ConfigMaps can be created containing one or more text files as well as literal string values.
We will revisit the COTD application which defaults to a “cats” theme unless an alternative is defined using 1) an environment variable known as “SELECTOR” or 2) via a properties file if present. This behaviour can be inspected in data/selector.php .
Setup
Initial Attempt
This tutorial assumes you have completed the OpenShift MiniLabs installation procedure. Then refresh before continuing.
Repeat Attempt
To reset your environment to repeat this tutorial do the following:
$ cd ~/containersascode $ ./oc-cluster-wrapper/oc-cluster up containersascode $ oc login -u developer - p developer $ oc delete project configmap
Instructions
Create Default COTD Application
Let’s create the COTD application in a new project. Once built and deployed, the URL http://cities-configmap.127.0.0.1.xip.io/ will show a cats theme. We will then cause COTD to change theme by using an environment variable (–from-literal) and properties file (–from-file) respectively.
$ oc login -u developer - p developer
$ oc new-project configmap --display-name='Config Map' --description='Config Map Example'
$ oc project configmap
$ oc new-app --name='cities' -l name='cities' php~https://bitbucket.org/emergile/cotd.git
$ oc expose service cities --name=cities -l name='cities'
–from-literal Example
This technique can be used to populate environment variables. In the COTD case this would be “SELECTOR”. Note how a lower-case “selector” name/value pair is injected and then mapped automatically as upper-case SELECTOR environment variable consumption inside the application’s environment. The URL http://cities-configmap.127.0.0.1.xip.io/ should now show a cities theme.
$ oc login -u developer - p developer $ oc project configmap $ oc create configmap cities-config --from-literal=selector=cities $ oc volume dc/cities --overwrite --add -t configmap -m /etc/config --name=cities-config --configmap-name=cities-config $ export POD=`oc get pods -l name=cities --no-headers=true | cut -d ' ' -f1 ` $ oc rsh $POD ls /etc/config $ oc env dc/cities --from=configmap/cities-config $ export POD=`oc get pods -l name=cities --no-headers=true | cut -d ' ' -f1 ` $ oc describe pod $POD $ oc env dc/cities --list
Back to the Default COTD Application
Now remove the SELECTOR environment variable by doing the following. The URL http://cities-configmap.127.0.0.1.xip.io/ should now revert back to the cats theme.
$ oc env dc/cities SELECTOR-
–from-file Example
This technique can be used to create and populate a “properties file” consumed by the application. In the COTD case this would be /etc/config/cotd.properties . The application will ingest this file if present. The URL http://cities-configmap.127.0.0.1.xip.io/ should now show a cities theme.
$ cat > cotd.properties << EOF! [cotd] selector = cities EOF! $ oc project configmap $ oc delete configmap cities-config $ oc create configmap cities-config --from-file=cotd.properties $ oc get configmaps cities-config -o yaml $ oc volume dc/cities --overwrite --add -t configmap -m /etc/config --name=cities-config --configmap-name=cities-config $ export POD=`oc get pods -l name=cities --no-headers=true | cut -d ' ' -f1 ` $ oc rsh $POD ls /etc/config $ oc env dc/cities --from=configmap/cities-config $ export POD=`oc get pods -l name=cities --no-headers=true | cut -d ' ' -f1 ` $ oc describe pod $POD $ oc env dc/cities --list
Back to the Default COTD Application
Now remove the properties file by doing the following. The URL http://cities-configmap.127.0.0.1.xip.io/ should now revert back to the cats theme.
$ oc env dc/cities COTD_PROPERTIES-
$ oc volume dc/cities --remove --name=cities-config
Trivia
Find out more about configmaps . Thanks to Noel O’Connor for helping to trail-blaze the way to these examples, https://mobile.twitter.com/noeloc.