DEV Community

Cover image for Step-by-Step Guide: Deploying a Static Web Application in OpenShift Using a Custom S2I Builder Image
Oloruntobi Olurombi
Oloruntobi Olurombi

Posted on

Step-by-Step Guide: Deploying a Static Web Application in OpenShift Using a Custom S2I Builder Image

This guide takes you through the process of creating a custom Source-to-Image (S2I) builder image, using it to deploy a static web application in OpenShift, and making the application accessible externally by updating the route with your cluster's public IP.

By the end of this guide, you'll have a fully functional static web application running on your OpenShift cluster.

Prerequisites

Before diving into the process, ensure the following are set up:

  1. OpenShift CLI (oc): Installed and configured to connect to your OpenShift cluster.

  2. OpenShift Cluster: Access to a running OpenShift cluster with a project where you can create resources.

  3. Git: Installed on your system for cloning repositories.

  4. Environment Variable: The OpenShift cluster's public IP should be stored in an environment variable called OPENSHIFT_CLUSTER_PUBLICIP.

  5. Basic Terminal Skills: Knowledge of executing commands and navigating files in the terminal.

Part 1: Verify OpenShift Setup

Step 1: Open the Terminal
  • Open a terminal on your local system or through the OpenShift web console:
  • In the web console, navigate to Terminal ==> New Terminal.
  • Check the version of OpenShift to confirm your setup is active:
oc version
Enter fullscreen mode Exit fullscreen mode

Part 2: Create a Custom S2I Builder Image

S2I (Source-to-Image) builder images are designed to take application source code and package it into a runnable container image. In this part, you'll create a custom builder image tailored for static web applications.

Step 2: Define the S2I Repository

Set the GitHub repository URL that contains the S2I source code for a simple static web server:

CLOUDACADEMY_GITHUB_S2I_URL=https://github.com/cloudacademy/openshift-s2i-simplewebsvr.git
Enter fullscreen mode Exit fullscreen mode
Step 3: Create the Builder Image

Run the following command to create a new build named static-webapp-builder using the S2I repository:

oc new-build --strategy=docker --name static-webapp-builder --code $CLOUDACADEMY_GITHUB_S2I_URL
Enter fullscreen mode Exit fullscreen mode

Image description

This command initialises a build process using the provided source code and Docker strategy.

Step 4: Verify the Build
  • Check if the build is completed:
oc get builds
Enter fullscreen mode Exit fullscreen mode

Image description

  • Inspect all resources created in the current project:
oc get all
Enter fullscreen mode Exit fullscreen mode

Image description

  • View the build logs to confirm its success:
oc logs build/static-webapp-builder-1
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Part 3: Deploy the Static Web Application

Now that your S2I builder image is ready, you’ll use it to build and deploy a static web application.

Step 5: Build and Launch the Application
Run the following command to create and deploy an application named dashboard using the static-webapp-builder image:

oc new-app static-webapp-builder~https://github.com/ColorlibHQ/gentelella.git --name dashboard
Enter fullscreen mode Exit fullscreen mode

Image description

This command automatically creates the build and deployment configuration, service, and image stream.

Step 6: Monitor the Build Process

Check the build logs to ensure everything works correctly:

oc logs -f bc/dashboard
Enter fullscreen mode Exit fullscreen mode

Image description

Verify all created resources:

oc get all
Enter fullscreen mode Exit fullscreen mode

Image description

Confirm that the dashboard pod is running:

oc get pods --field-selector status.phase=Running
Enter fullscreen mode Exit fullscreen mode

Image description

Ensure the dashboard service has been registered:

oc get svc
Enter fullscreen mode Exit fullscreen mode

Image description

Part 4: Expose the Application to External Traffic

By default, OpenShift services are not exposed outside the cluster. In this section, you’ll expose the application and update the route.

Step 7: Expose the Service

Expose the dashboard service to create a route:

oc expose svc/dashboard 
Enter fullscreen mode Exit fullscreen mode

Image description

verify the route:

oc get routes 
Enter fullscreen mode Exit fullscreen mode

Image description

Step 8: Update the Route

The route created in the previous step uses a default Fully Qualified Domain Name (FQDN). You’ll now update it to match the public IP address of your OpenShift cluster.

Install yq for YAML Processing

Download and install the yq utility:

curl -O --location https://github.com/mikefarah/yq/releases/download/2.4.1/yq_linux_amd64
sudo mv yq_linux_amd64 /usr/bin/yq
sudo chmod +x /usr/bin/yq
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Export and Edit the Route Configuration

Export the current route configuration:

oc get route dashboard -o yaml > route.yaml
Enter fullscreen mode Exit fullscreen mode

Image description

Update the FQDN using yq:

yq w --inplace route.yaml spec.host dashboard.$OPENSHIFT_CLUSTER_PUBLICIP.nip.io
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Apply the Updated Route

Image description

Delete the existing route:

oc delete route dashboard
Enter fullscreen mode Exit fullscreen mode

Image description

Recrate the route with the update configuration:

oc apply -f route.yaml
Enter fullscreen mode Exit fullscreen mode

Image description

Confirm the updated route:

oc get route dashboard
Enter fullscreen mode Exit fullscreen mode

Image description

Part 5: Test the Application

Step 9: Verify Connectivity

Test the application’s connectivity using curl:

curl -I -s http://dashboard.$OPENSHIFT_CLUSTER_PUBLICIP.nip.io/production/
Enter fullscreen mode Exit fullscreen mode

Image description

Step 10: Access the Application

Generate and display the application URL:

echo http://dashboard.$OPENSHIFT_CLUSTER_PUBLICIP.nip.io/production/
Enter fullscreen mode Exit fullscreen mode

Image description

Copy the URL and open it in your browser to view the deployed static web application.

Image description

Summary

You have successfully:

  • Created a custom S2I builder image.

  • Built and deployed a static web application using OpenShift.

  • Exposed the application to external traffic by configuring a route.

This hands-on exercise demonstrates the flexibility of OpenShift in building, deploying, and managing applications seamlessly. Explore further by customising your static application or integrating advanced features! 🚀

Top comments (0)