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 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
Create a web application deployment named
webconfig. Use theregistry.ocp4.example.com:8443/redhattraining/httpd-noimage:v1container image.Log in to the OpenShift cluster as the
developeruser with thedeveloperpassword.[student@workstation ~]$ oc login -u developer -p developer \ https://api.ocp4.example.com:6443 ..output omitted...Change to the
storage-configsproject.[student@workstation ~]$ oc project storage-configs Now using project "storage-configs" on server "https://api.ocp4.example.com:6443". ...output omitted...Create the
webconfigdeployment. 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 createdVerify the deployment status.
[student@workstation ~]$ oc get pods NAME READY STATUS ... webconfig-6cd5bddcf7-9ncdg 1/1 Running ...
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|Expose the
webconfigdeployment to create thewebconfig-svcservice.[student@workstation ~]$ oc expose deployment webconfig --name webconfig-svc \ --port 8080 --target-port 8080 service/webconfig-svc exposedVerify 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 ...Expose the
webconfig-svcservice to create thewebconfig-rtroute.[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 ...Use a web browser to navigate to the
http://webconfig-rt-storage-configs.apps.ocp4.example.comroute, and then click the displayed link. An error message is displayed, because a file was not found on the server.
Use the missing file to create a configuration map.
Return to the terminal. Then, create a configuration map named
webfilesby using theredhatlogo.pngfile in the/home/student/DO180/labs/storage-configsdirectory.[student@workstation ~]$ oc create configmap webfiles \ --from-file=/home/student/DO180/labs/storage-configs/redhatlogo.png configmap/webfiles createdVerify 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....
Mount the
webfilesconfiguration map as a volume in thewebconfigdeployment.Mount the
webfilesconfiguration 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 updatedVerify 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...Return to the web browser, and navigate to the
webconfig-rt-storage-configs.apps.ocp4.example.comroute. 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
