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:
OpenShift CLI (oc): Installed and configured to connect to your OpenShift cluster.
OpenShift Cluster: Access to a running OpenShift cluster with a project where you can create resources.
Git: Installed on your system for cloning repositories.
Environment Variable: The OpenShift cluster's public IP should be stored in an environment variable called OPENSHIFT_CLUSTER_PUBLICIP.
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
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
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
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
- Inspect all resources created in the current project:
oc get all
- View the build logs to confirm its success:
oc logs build/static-webapp-builder-1
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
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
Verify all created resources:
oc get all
Confirm that the dashboard
pod is running:
oc get pods --field-selector status.phase=Running
Ensure the dashboard
service has been registered:
oc get svc
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
verify the route:
oc get routes
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
Export and Edit the Route Configuration
Export the current route configuration:
oc get route dashboard -o yaml > route.yaml
Update the FQDN using yq
:
yq w --inplace route.yaml spec.host dashboard.$OPENSHIFT_CLUSTER_PUBLICIP.nip.io
Apply the Updated Route
Delete the existing route:
oc delete route dashboard
Recrate the route with the update configuration:
oc apply -f route.yaml
Confirm the updated route:
oc get route dashboard
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/
Step 10: Access the Application
Generate and display the application URL:
echo http://dashboard.$OPENSHIFT_CLUSTER_PUBLICIP.nip.io/production/
Copy the URL and open it in your browser to view the deployed static web application.
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)