Guided Exercise: Externalize the Configuration of Applications

Jul 12, 2023·

4 min read

Deploy a web server taking configuration files from a configuration map.


In this exercise, you deploy a web application to mount the missing files from a configuration map.

  • Create a web application deployment.

  • Expose the web application deployment to external access.

  • Create a configuration map from a file.

  • Mount the configuration map in the web application deployment.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise. This command ensures that the cluster is accessible.

[student@workstation ~]$ lab start storage-configs

Procedure 5.1. Instructions

  1. Create a web application deployment named webconfig. Use the container image.

    1. Log in to the OpenShift cluster as the developer user with the developer password.

       [student@workstation ~]$ oc login -u developer -p developer \
       ..output omitted...
    2. Change to the storage-configs project.

       [student@workstation ~]$ oc project storage-configs
       Now using project "storage-configs" on server "".
       ...output omitted...
    3. Create the webconfig deployment. Ignore the warning message.

       [student@workstation ~]$ oc create deployment webconfig \
       Warning: would violate PodSecurity "restricted:v1.24":
       ...output omitted...
       deployment.apps/webconfig created
    4. Verify the deployment status.

       [student@workstation ~]$ oc get pods
       NAME                          READY  STATUS  ...
       webconfig-6cd5bddcf7-9ncdg  1/1    Running ...
  2. Expose the web application to external access. Use the following information to create a service and a route for the web application.

    | Service Field | Service Value | | --- | --- | | Service name | webconfig-svc | | Port number | 8080 | | Target port | 8080 |

    | Route Field | Route Value | | --- | --- | | Route name | webconfig-rt | | Service name | webconfig-svc |

    1. Expose the webconfig deployment to create the webconfig-svc service.

       [student@workstation ~]$ oc expose deployment webconfig --name webconfig-svc \
         --port 8080 --target-port 8080
       service/webconfig-svc exposed
    2. Verify the status of the service.

       [student@workstation ~]$ oc get services
       NAME          TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    ...
       webconfig-svc ClusterIP   <none>        8080/TCP   ...
       [student@workstation ~]$ oc get pods -o wide
       NAME                        READY  ...  IP         ...
       webconfig-6cd5bddcf7-9ncdg  1/1    ...  ...
    3. Expose the webconfig-svc service to create the webconfig-rt route.

       [student@workstation ~]$ oc expose service webconfig-svc --name webconfig-rt exposed
       [student@workstation ~]$ oc get routes
       NAME          HOST/PORT                                          ... SERVICES      ...
       webconfig-rt ... webconfig-svc ...
    4. Use a web browser to navigate to the route, and then click the displayed link. An error message is displayed, because a file was not found on the server.

  3. Use the missing file to create a configuration map.

    1. Return to the terminal. Then, create a configuration map named webfiles by using the redhatlogo.png file in the /home/student/DO180/labs/storage-configs directory.

       [student@workstation ~]$ oc create configmap webfiles \
       configmap/webfiles created
    2. Verify the creation of the configuration map.

       [student@workstation ~]$ oc describe configmap webfiles
       Name:         webfiles
       Namespace:    storage-configs
       ...output omitted...
       redhatlogo.png: 3406 bytes
       ...output omitted....
  4. Mount the webfiles configuration map as a volume in the webconfig deployment.

    1. Mount the webfiles configuration map as a volume. Ignore the warning message.

       [student@workstation ~]$ oc set volume deployment/webconfig \
         --add --type configmap --configmap-name webfiles \
         --mount-path /var/www/html/redhatlogo.png
       Warning: would violate PodSecurity "restricted:v1.24":
       ...output omitted...
       deployment.apps/webconfig volume updated
    2. Verify the deployment status. Verify that a new pod was created.

       [student@workstation ~]$ oc status
       ...output omitted... to pod port 8080 (svc/webconfig-svc)
         deployment/webconfig deploys
           deployment #2 running for 2 minutes - 1 pod
           deployment #1 deployed 17 minutes ago
       ...output omitted...
       [student@workstation ~]$ oc get pods
       NAME                          READY  STATUS   ...
       webconfig-654bcf6cf-wcnnk  1/1    Running  ...
       ...output omitted...
    3. Return to the web browser, and navigate to the route. Then, click the link to open a list of files. Click redhatlogo.png to open the file.

      The configuration map successfully added the missing image file to the web application.


On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish storage-configs