8 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
The api version of this git repo do not work anymore. Here are the working versions:
configmap-demo-build.yml:
---
kind: List
apiVersion: v1
metadata: {}
items:
- kind: BuildConfig
  apiVersion: build.openshift.io/v1
  metadata:
    name: configmap-demo
    labels:
      app: configmap-demo
  spec:
    triggers:
    - type: GitHub
      github:
        secret: secret
    - type: Generic
      generic:
        secret: secret
    - type: ConfigChange
    - type: ImageChange
      imageChange: {}
    source:
      type: Git
      git:
        uri: https://github.com/tosin2013/configmap-demo.git
    strategy:
      type: Source
      sourceStrategy:
        from:
          kind: ImageStreamTag
          namespace: openshift
          name: nodejs:latest
    output:
      to:
        kind: ImageStreamTag
        name: configmap-demo:latest
    resources: {}
    postCommit: {}

configmap-demo-deployment.yml:
---
kind: List
apiVersion: v1
metadata: {}
items:
- kind: ImageStream
  apiVersion: image.openshift.io/v1
  metadata:
    name: configmap-demo
    generation: 1
    labels:
      app: configmap-demo
  spec:
    tags:
    - name: latest
      from:
        kind: ImageStreamTag
        name: configmap-demo:latest
- kind: DeploymentConfig
  apiVersion: apps.openshift.io/v1
  metadata:
    name: configmap-demo
    labels:
      app: configmap-demo
  spec:
    strategy:
      type: Rolling
      rollingParams:
        updatePeriodSeconds: 1
        intervalSeconds: 1
        timeoutSeconds: 600
        maxUnavailable: 25%
        maxSurge: 25%
      resources: {}
    triggers:
    - type: ConfigChange
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
        - configmap-demo
        from:
          kind: ImageStreamTag
          name: configmap-demo:latest
    replicas: 1
    test: false
    selector:
      app: configmap-demo
      deploymentconfig: configmap-demo
    template:
      metadata:
        labels:
          app: configmap-demo
          deploymentconfig: configmap-demo
      spec:
        containers:
        - name: configmap-demo
          image: configmap-demo
          ports:
          - containerPort: 8080
            protocol: TCP
          env:
          - name: OPENSHIFT_NODEJS_PORT
            value: '8080'
          - name: BACKGROUND_MSG
            valueFrom:
              configMapKeyRef:
                name: config
                key: message
          volumeMounts:
          - name: app-config
            mountPath: "/etc/node-app/"
          resources: {}
          terminationMessagePath: "/dev/termination-log"
          imagePullPolicy: Always
        volumes:
        - name: app-config
          configMap:
            name: config
            items:
            - key: ui.properties
              path: node-app.config
        restartPolicy: Always
        terminationGracePeriodSeconds: 30
        dnsPolicy: ClusterFirst
        securityContext: {}
- kind: Service
  apiVersion: v1
  metadata:
    name: configmap-demo
    labels:
      app: configmap-demo
  spec:
    ports:
    - name: 8080-tcp
      protocol: TCP
      port: 8080
      targetPort: 8080
    selector:
      app: configmap-demo
      deploymentconfig: configmap-demo
    type: ClusterIP
    sessionAffinity: None
- kind: Route
  apiVersion: route.openshift.io/v1
  metadata:
    name: configmap-demo
    labels:
      app: configmap-demo
  spec:
    host: ''
    to:
      kind: Service
      name: configmap-demo
    port:
      targetPort: 8080-tcp

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.