Bookmark this page

Guided Exercise: Customizing an Existing S2I Base Image

Customize the assemble and run scripts of an Apache HTTP server builder image.

Override the default built-in scripts.

Outcomes

  • Customize the assemble and run scripts of an Apache HTTP server builder image.

  • Override the default built-in scripts.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

[student@workstation ~]$ lab start builds-s2i

Instructions

  1. Explore the S2I scripts packaged in the rhscl/httpd-24-rhel7 builder image.

    1. Authenticate Podman running on the workstation by logging in to the classroom image registry.

      [student@workstation ~]$ podman login \
      -u developer -p developer \
      registry.ocp4.example.com:8443
      Login Succeeded!
    2. Use Podman to create a container from the httpd-24 container image. Override the container entry point to run a shell.

      [student@workstation ~]$ podman run --name webserver -it  --rm \
      registry.ocp4.example.com:8443/ubi9/httpd-24 bash
      ...output omitted...
      bash-5.1$
    3. Inspect the S2I scripts packaged in the builder image. The S2I scripts are located in the /usr/libexec/s2i folder.

      bash-5.1$ cat /usr/libexec/s2i/assemble
      ...output omitted...
      bash-5.1$ cat /usr/libexec/s2i/run
      ...output omitted...
      bash-5.1$ cat /usr/libexec/s2i/usage
      ...output omitted...

      Exit from the container.

      bash-5.1$ exit
  2. Review the application source code with the custom S2I scripts.

    1. Change to the directory containing the application.

      [student@workstation ~]$ cd ~/DO288/DO288-apps/labs/builds-s2i/s2i-scripts
      no output expected
    2. Inspect the index.html file.

      [student@workstation s2i-scripts]$ cat index.html
      Hello Class! DO288 rocks!!!
    3. The custom S2I scripts are in the .s2i/bin folder. Inspect the assemble script.

      [student@workstation s2i-scripts]$ cat .s2i/bin/assemble
      ...output omitted...
       CUSTOMIZATION STARTS HERE 
      
      echo "---> Installing application source"
      cp -Rf /tmp/src/*.html ./
      
      DATE=date "+%b %d, %Y @ %H:%M %p"
      
      echo "---> Creating info page"
      echo "Page built on $DATE" >> ./info.html
      echo "Proudly served by Apache HTTP Server version $HTTPD_VERSION" >> ./info.html
      
       CUSTOMIZATION ENDS HERE 
      ...output omitted...

      This script copies the index.html file from the application source to the web server document root at /opt/app-root/src. It also creates an info.html file containing page build time and environment information.

    4. Inspect the run script.

      [student@workstation s2i-scripts]$ cat .s2i/bin/run
      ...output omitted...
      # Make Apache show 'debug' level logs during startup
      exec run-httpd -e debug $@

      This script changes the default log level of the startup messages in the web server to debug.

  3. Deploy the application to a Red Hat OpenShift cluster. Verify that the custom S2I scripts are executed.

    1. Log in to OpenShift as the developer user.

      [student@workstation s2i-scripts]$ oc login -u developer -p developer \
      https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
    2. Ensure that you use the builds-s2i project.

      [student@workstation s2i-scripts]$ oc project builds-s2i
      ...output omitted...
    3. Create a new application called bonjour from the provided sources. You need to prefix the Git URL with the httpd:2.4 image stream, by using the tilde notation (~), to ensure that the application uses the rhscl/httpd24-rhel7 builder image. Ignore the security warning.

      [student@workstation s2i-scripts]$ oc new-app --name bonjour \
      --context-dir labs/builds-s2i/s2i-scripts \
      registry.ocp4.example.com:8443/ubi9/httpd-24~https://git.ocp4.example.com/\
      developer/DO288-apps
      ...output omitted...
      --> Creating resources ...
      imagestream.image.openshift.io "httpd-24" created
      imagestream.image.openshift.io "bonjour" created
      ...output omitted...
      buildconfig.build.openshift.io "bonjour" created
      deployment.apps "bonjour" created
      service "bonjour" created
      --> Success
      ...output omitted...
    4. Wait until the build finishes and the application container image is pushed to the OpenShift internal registry.

      [student@workstation s2i-scripts]$ oc get build
      NAME       ...  STATUS     ...
      bonjour-1  ...  Complete ...
    5. View the build logs.

      [student@workstation s2i-scripts]$ oc logs -f bc/bonjour
      ...output omitted...
      Cloning "https://git.ocp4.example.com/developer/DO288-apps" ...
      ...output omitted...
      STEP 9/10: RUN /tmp/scripts/assemble
      ---> Enabling s2i support in httpd24 image
          AllowOverride All
      ---> Installing application source
      ---> Creating info page
      STEP 10/10: CMD /tmp/scripts/run
      COMMIT temp.builder.openshift.io/builds-s2i/bonjour-1:986469c8
      ...output omitted...
      Push successful

      Observe that the custom S2I scripts provided by the application are executed instead of the built-in S2I scripts from the builder image.

  4. Test the application.

    1. Wait until the application is deployed and then view the status of the application pod. The application pod should be in the Running state.

      [student@workstation s2i-scripts]$ oc get po
      NAME                      READY   STATUS      RESTARTS   AGE
      bonjour-1-build           0/1     Completed   0          105s
      bonjour-7bc86dd97-wjvhx   1/1     Running     0          72s
    2. Expose the application for external access by using a route.

      [student@workstation s2i-scripts]$ oc expose svc bonjour
      route.route.openshift.io/bonjour exposed
    3. Obtain the route URL by using the oc get route command.

      [student@workstation s2i-scripts]$ oc get route
      NAME      HOST/PORT                                  ...
      bonjour   bonjour-builds-s2i.apps.ocp4.example.com   ...
    4. Retrieve the index page of the application with the URL from the previous step.

      [student@workstation s2i-scripts]$ curl \
      http://bonjour-builds-s2i.apps.ocp4.example.com
      Hello Class! DO288 rocks!!!
    5. Retrieve the info page of the application.

      [student@workstation s2i-scripts]$ curl \
      http://bonjour-builds-s2i.apps.ocp4.example.com/info.html
      Page built on ...output omitted...
      Proudly served by Apache HTTP Server version 2.4
    6. Inspect the logs for the application pod. Recall that the startup log level was changed to debug in the run script. You should see debug level log messages being displayed at startup.

      [student@workstation s2i-scripts]$ oc logs deploy/bonjour
      ...output omitted...
      10.8.0.2 - - [16/Oct/2023:20:38:50 +0000] "GET / HTTP/1.1" 200 28 "-" "curl/7.61.1"
      10.8.0.2 - - [16/Oct/2023:20:40:25 +0000] "GET /info.html HTTP/1.1" 200 87 "-" "curl/7.61.1"

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 builds-s2i

Revision: do288-4.12-0d49506