Guided Exercise: Externalize the Configuration of Applications

·

4 min read

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

Outcomes

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 registry.ocp4.example.com:8443/redhattraining/httpd-noimage:v1 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 \
       https://api.ocp4.example.com:6443
       ..output omitted...
      
    2. Change to the storage-configs project.

       [student@workstation ~]$ oc project storage-configs
       Now using project "storage-configs" on server "https://api.ocp4.example.com:6443".
       ...output omitted...
      
    3. Create the webconfig deployment. Ignore the warning message.

       [student@workstation ~]$ oc create deployment webconfig \
         --image registry.ocp4.example.com:8443/redhattraining/httpd-noimage:v1
       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   172.30.38.8   <none>        8080/TCP   ...
      
       [student@workstation ~]$ oc get pods -o wide
       NAME                        READY  ...  IP         ...
       webconfig-6cd5bddcf7-9ncdg  1/1    ...  10.8.0.96  ...
      
    3. Expose the webconfig-svc service to create the webconfig-rt route.

       [student@workstation ~]$ oc expose service webconfig-svc --name webconfig-rt
       route.route.openshift.io/webconfig-rt exposed
      
       [student@workstation ~]$ oc get routes
       NAME          HOST/PORT                                          ... SERVICES      ...
       webconfig-rt  webconfig-rt-storage-configs.apps.ocp4.example.com ... webconfig-svc ...
      
    4. Use a web browser to navigate to the http://webconfig-rt-storage-configs.apps.ocp4.example.com 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 \
         --from-file=/home/student/DO180/labs/storage-configs/redhatlogo.png
       configmap/webfiles created
      
    2. Verify the creation of the configuration map.

       [student@workstation ~]$ oc describe configmap webfiles
       Name:         webfiles
       Namespace:    storage-configs
       ...output omitted...
       BinaryData
       ====
       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...
       http://webconfig-rt-storage-configs.apps.ocp4.example.com to pod port 8080 (svc/webconfig-svc)
         deployment/webconfig deploys registry.ocp4.example.com:8443/redhattraining/httpd-noimage:v1
           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 webconfig-rt-storage-configs.apps.ocp4.example.com 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.

Finish

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