Guided Exercise: Externalize the Configuration of Applications

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 two files.

  • 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

Instructions

  1. Create a web application deployment named webconfig from the web console. Use the registry.ocp4.example.com:8443/rhscl/httpd-24-rhel7:latest container image.

    1. Log in to the OpenShift cluster as the developer user with the developer password by using the OpenShift web console https://console-openshift-console.apps.ocp4.example.com URL.

    2. Change to the Administrator perspective, and change to the storage-configs project. Select the Workloads → Deployments option and click the Create Deployments button. Add the webconfig deployment name and the registry.ocp4.example.com:8443/rhscl/httpd-24-rhel7:latest image name, and leave the default for the other values.

    3. Select the Create button. Verify the successful deployment of three pods.

  2. Expose the web application to external access from the web console. Use the following information to create a service and a route for the web application, and leave the hostname and path fields blank.

    Service fieldService value
    Service name webconfig-svc
    App selector webconfig
    Port number 8080
    Target port 8080
    Route fieldRoute value
    Route name webconfig-rt
    Service name webconfig-svc
    Target port 8080
    1. Select the Networking → Services option and click the Create Service button to create the webconfig-svc service that exposes the webconfig deployment. Update the property values and click Create. Verify the status of the service.

      Alternatively, create the service from the CLI by using the oc expose deploy/webconfig --name 'webconfig-svc' --selector 'app=webconfig' --target-port '8080' --port '8080' command.

    2. Select the Networking → Routes option and click the Create Route button to create the webconfig-rt service that exposes the webconfig-svc service. Update the property values and click Create. Verify the status of the route.

      Alternatively, create the route from the CLI by using the oc expose svc/webconfig-svc --name 'webconfig-rt' command.

    3. Use a web browser to navigate to the http://webconfig-rt-storage-configs.apps.ocp4.example.com route. A testing page is displayed by default because of the missing files.

  3. Use the missing files to create a configuration map from the web console. Use the following information to create the configuration map for the web application:

    Configuration map fieldValue
    Name webfiles
    Data key index.html
    Data valueContent from index.html
    Binary data key redhatlogo.png
    Binary data valueNon-printable content from the redhatlogo.png file
    1. Select the Workloads → ConfigMaps option and click Create ConfigMap to view the configuration map form. Using webfiles for the name, create the configuration map by using the redhatlogo.png file and the index.html file in the /home/student/DO180/labs/storage-configs directory. Add a Data key, define the index.html name as the Key value, and open the /home/student/DO180/labs/storage-configs/index.html file to associate the contents of the file with the data key.

      Add a Binary Data key, define the redhatlogo.png name as the Key value, and open the /home/student/DO180/labs/storage-configs/redhatlogo.png file to associate the binary contents with the binary key.

    2. Click Create to create the configuration map.

  4. Log in to the OpenShift cluster from the command line, and mount the webfiles configuration map as a volume in the webconfig deployment from the command line.

    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. Select the storage-configs project.

      [student@workstation ~]$ oc project storage-configs
      Now using project "storage-configs" on server "https://api.ocp4.example.com:6443".
    3. Mount the webfiles configuration map as a volume.

      [student@workstation ~]$ oc set volume deployment/webconfig \
        --add --type configmap --configmap-name webfiles \
        --name webfiles-vol --mount-path /var/www/html/
      deployment.apps/webconfig volume updated
    4. 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/rhscl/httpd-24-rhel7:latest
          deployment #2 running for 2 minutes - 3 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...
    5. Return to the web browser, and navigate to the webconfig-rt-storage-configs.apps.ocp4.example.com route. Click the Click me! link to open the file.

      The configuration map successfully added the missing files 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