9 Using ConfigMaps

Prerequisites

  • You have access to OpenShift Web Console URL. Ask your workshop coordinator for URL if you don’t have one.

  • You have credentials to login. Ask your workshop coordinator for credentials to log onto the OpenShift cluster

  • Git must be installed on workstation.

Introduction

A ConfigMap object provides mechanisms to inject containers with configuration data while keeping containers agnostic of OpenShift Container Platform. A ConfigMap can be used to store fine-grained information like individual properties or coarse-grained information like entire configuration files or JSON blobs. In the lab below we will modify the properties file of a webpage using a ConfigMap.

The ConfigMap object holds key-value pairs of configuration data that can be consumed in pods or used to store configuration data. ConfigMaps is similar to secrets but it is not recommended for sensitive data.

Exercise

Clone configmap-demo from git

$ git clone https://github.com/tosin2013/configmap-demo.git
$ cd configmap-demo

Login to OpenShift

Lets create a project

$ oc new-project workshop-configmap-lab-YourName
Change userXX to your username provided by your workshop coordinator.

Lets set the color we would like to use

$ export COLOR=red
$ echo "color=$COLOR" > ui.properties

Let’s create a ConfigMap, named config, with both a literal text, message=YOUR MESSAGE, and the configuration file:

$ export PERSONAL_MESSAGE="YOUR MESSAGE"  # Change the message
$ oc create configmap config \
            --from-literal=message="${PERSONAL_MESSAGE}" \
            --from-file=ui.properties

Check the contents of configmap/config

$ oc get configmap/config -o json

Create the app deployment and build the app

$ oc create -f configmap-demo-build.yml
$ oc create -f  configmap-demo-deployment.yml

Check the deployment

$  oc get pods
NAME                      READY   STATUS      RESTARTS   AGE
configmap-demo-1-build    0/1     Completed   0          4m29s
configmap-demo-1-deploy   0/1     Completed   0          3m18s
configmap-demo-1-kshbh    1/1     Running     0          3m9s

Launch Webpage

$ oc get routes

Update configmap

$ export COLOR=green
$ echo "color=$COLOR" > ui.properties

Delete old config map

$ oc delete  configmap config

Lets create a new config map

$ export PERSONAL_MESSAGE="NEW MESSAGE"  # Change the message
$ oc create configmap config \
            --from-literal=message="${PERSONAL_MESSAGE}" \
            --from-file=ui.properties

Get Pod Name

$ oc get pods

Delete old config map pod

$ oc delete pod configmap-demo-1-kshbh

Check pod status

$ oc get pods

Reload Webpage

Delete deployment

$ oc delete -f configmap-demo-build.yml
$ oc delete -f  configmap-demo-deployment.yml

Delete project

$ oc delete project workshop-configmap-lab-YourName

Summary

In this lab we learned how about configmaps and its uses. We also deployed a simple web application and modified the properties and environment using configmaps.