Most applications require configuration using environment variables, configuration files and command line arguments. These configuration artifacts should be externalized form the application and the docker image content in order to keep the image portable across environments.
In previous labs, the nationalparks
application was configured
with database credentials using environment variables. While environment
variables is a useful way to configure applications, it is difficult to manage
hundreds of environment variables which are scattered across various containers
in a project. Fortunately, there is a convenient and platform-independent
mechanism in OpenShift to configure applications, which is called ConfigMap
.
The ConfigMap
object in OpenShift provides mechanisms to provide configuration
data to the application container while keeping the application images both
portable across environments and independent of OpenShift Container Platform. A
ConfigMap
can be used to store key-value properties, configuration files, JSON
blobs and alike.
In this lab, you will replace the environment variables provided in the
previous labs and use a ConfigMap
instead to configure the
nationalparks
application.
You can create a ConfigMap
by pointing at a file containing the application
configuration. Download this properties file to your local machine which
contains the database credentials:
http://gitlab-ce-workshop-infra.{{ROUTER_ADDRESS}}/{{GITLAB_USER}}/nationalparks/raw/{{NATIONALPARKS_VERSION}}/ose3/application-dev.properties
Note
|
Verify that the contents of the file are correct. If you have downladed the file with Internet Explorer it might contain incorrect chars or different charset. Try to use "Google Chrome", "Firefox" or "curl". |
Create a ConfigMap
using the following command in the {{EXPLORE_PROJECT_NAME}}{{USER_SUFFIX}}
project:
$ oc create configmap nationalparks --from-file=application.properties=./application-dev.properties
The --from-file
option specifies a key-value pair with the key used as the
file name that is provided to the application and value as the content of the
file. In the above command, the content of application-dev.properties
file
will be provided to the application container as a properties file called
application.properties
List and verify that the ConfigMap
is created successfully containing the
database credentials:
$ oc describe configmap nationalparks
Name: nationalparks
Namespace: demo
Labels: <none>
Annotations: <none>
Data
====
application.properties: 123 bytes
You can review the content of the ConfigMap
using the oc get
command:
$ oc get configmap nationalparks -o yaml
apiVersion: v1
data:
application.properties: |
# NationalParks MongoDB
mongodb.server.host=mongodb
mongodb.user=mongodb
mongodb.password=mongodb
mongodb.database=mongodb
kind: ConfigMap
metadata:
creationTimestamp: 2016-11-16T09:17:02Z
name: nationalparks
namespace: {{EXPLORE_PROJECT_NAME}}{{USER_SUFFIX}}
resourceVersion: "8421"
selfLink: /api/v1/namespaces/demo/configmaps/nationalparks
uid: 6f4536cf-abdd-11e6-9282-525400c3c0db
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
The nationalparks
Spring Boot application can be configured through a
properties file called application.properties
which should reside in a specific
location in the container filesystem. Using the following command to mount the
ConfigMap
inside the nationalparks
pod:
$ oc set volumes dc/nationalparks --add -m /deployments/config --configmap-name=nationalparks
The above command makes the content of the configmap ConfigMap
, which you
created from a file, called application.properties
, available in the
/opt/openshift/config
directory. The nationalparks
DeploymentConfiguration
detects the configuration change, and automatically deploys the Pod with
the new configuration.
Also, as we have configured nationalparks
through ConfigMap
, you can remove
the database environment variables set in the previous labs:
$ oc env dc/nationalparks MONGODB_USER- MONGODB_PASSWORD- MONGODB_DATABASE- MONGODB_SERVER_HOST-
You have now externalized nationalparks
configuration. Visit the nationalparks
web
service to very the database connection is working correctly.:
http://nationalparks-{{EXPLORE_PROJECT_NAME}}{{USER_SUFFIX}}.{{ROUTER_ADDRESS}}/ws/data/all/
If you check the new Pod’s logs once it comes up, you should see no errors.