Manually deploy a Java application with Open Liberty/WebSphere Liberty on an Azure Red Hat OpenShift cluster

This article provides step-by-step manual guidance for running Open/WebSphere Liberty on an Azure Red Hat OpenShift cluster. It walks you through preparing a Liberty application, building the application Docker image and running the containerized application on an Azure Red Hat OpenShift cluster.

Specifically, you learn how to accomplish the following tasks:

  • Prepare the Liberty application
  • Build the application image
  • Run the containerized application on an Azure Red Hat OpenShift cluster using the GUI and the CLI

For a more automated solution that accelerates your journey to Azure Red Hat OpenShift cluster, see Deploy IBM WebSphere Liberty and Open Liberty on Azure Red Hat OpenShift using Azure Marketplace offer.

For more information on Open Liberty, see the Open Liberty project page. For more information on WebSphere Liberty, see the WebSphere Liberty product page.

This article is intended to help you quickly get to deployment. Before going to production, you should explore Tuning Liberty.

If you're interested in providing feedback or working closely on your migration scenarios with the engineering team developing WebSphere on Azure solutions, fill out this short survey on WebSphere migration and include your contact information. The team of program managers, architects, and engineers will promptly get in touch with you to initiate close collaboration.

Important

While Azure Red Hat OpenShift is jointly engineered, operated, and supported by Red Hat and Microsoft to provide an integrated support experience, the software you run on top of Azure Red Hat OpenShift, including that described in this article, is subject to its own support and license terms. For more information about support for Azure Red Hat OpenShift, see Support lifecycle for Azure Red Hat OpenShift 4. For more information about support for the software described in this article, see the main pages for that software as listed in the article.

Note

Azure Red Hat OpenShift requires a minimum of 40 cores to create and run an OpenShift cluster. The default Azure resource quota for a new Azure subscription does not meet this requirement. To request an increase in your resource limit, see Standard quota: Increase limits by VM series. Note that the free trial subscription isn't eligible for a quota increase, upgrade to a Pay-As-You-Go subscription before requesting a quota increase.

Prerequisites

  • A local machine with Unix-like operating system installed - for example, Ubuntu, macOS, or Windows Subsystem for Linux.

  • A Java Standard Edition (SE) implementation, version 17 or later - or example, Eclipse Open J9.

  • Maven, version 3.9.8 or higher.

  • Docker for your OS.

  • Azure CLI, version 2.61.0 or later.

  • An Azure Red Hat OpenShift 4 cluster. To create the cluster, follow the instructions in Create an Azure Red Hat OpenShift 4 cluster while using the following instructions:

    • Though the "Get a Red Hat pull secret" step is labeled as optional, it's required for this article. The pull secret enables your Azure Red Hat OpenShift cluster to find the Open Liberty Operator.

    • The following environment variables defined in Create an Azure Red Hat OpenShift 4 cluster are used later in this article:

      • RESOURCEGROUP - the name of the resource group in which the cluster is deployed.
      • CLUSTER- the name of the cluster.
    • If you plan to run memory-intensive applications on the cluster, specify the proper virtual machine size for the worker nodes using the --worker-vm-size parameter. For more information, see the following articles:

  • Connect to the cluster by following the steps in Connect to an Azure Red Hat OpenShift 4 cluster while using the following instructions:

    • Be sure to follow the steps in "Install the OpenShift CLI" because you use the oc command later in this article.
    • Save aside the cluster console URL. It should look like https://console-openshift-console.apps.<random>.<region>.aroapp.io/.
    • Take note of the kubeadmin credentials.
    • Be sure to follow the steps in "Connect using the OpenShift CLI" with the kubeadmin credentials.

Install the Open Liberty OpenShift Operator

After you create and connect to the cluster, use the following steps to install the Open Liberty Operator. The main starting page for the Open Liberty Operator is on GitHub.

  1. Sign in to the OpenShift web console from your browser using the kubeadmin credentials.

  2. Navigate to Operators > OperatorHub and search for Open Liberty.

  3. Select Open Liberty from the search results.

  4. Select Install.

  5. On the Install Operator page, use the following steps:

    1. For Update channel, select the latest channel v1.3.
    2. For Installation mode, seelct All namespaces on the cluster (default).
    3. For Update approval, select Automatic.

    Screenshot of the OpenShift web console that shows the Install Operator page.

  6. Select Install and wait a minute or two until the installation completes.

  7. Observe that the Open Liberty Operator is successfully installed and ready for use. If it isn't ready, diagnose and resolve the problem before continuing.

    Screenshot of the OpenShift web console that shows the Installed operator dialog box.

Note

This guide installs the Open Liberty Operator to manage Liberty applications on the Azure Red Hat OpenShift cluster. If you want to use the WebSphere Liberty Operator, follow the steps in Installing the WebSphere Liberty operator to install the WebSphere Liberty Operator on the cluster.

Create an OpenShift namespace for the Java app

Use the following steps to create an OpenShift namespace for use with your app:

  1. Make sure you signed in to the OpenShift web console from your browser using the kubeadmin credentials.

  2. Navigate to Administration > Namespaces > Create Namespace.

  3. Fill in open-liberty-demo for Name and select Create, as shown next.

    Screenshot of the OpenShift web console that shows the Create Namespace dialog box.

Create an Azure Database for MySQL Flexible Server

Azure Database for MySQL Flexible Server deployment model is a deployment mode designed to provide more granular control and flexibility over database management functions and configuration settings than the Azure Database for MySQL single server deployment mode. This section shows you how to create an Azure Database for MySQL Flexible Server instance using the Azure CLI. For more information, see Quickstart: Create an instance of Azure Database for MySQL - Flexible Server by using the Azure CLI.

Run the following command in your terminal to create an Azure Database for MySQL Flexible Server instance. Replace <server-admin-password> with a password that meets the password complexity requirements for Azure Database for MySQL Flexible Server.

az mysql flexible-server create \
    --name ${CLUSTER} \
    --resource-group ${RESOURCEGROUP} \
    --admin-user admin${RESOURCEGROUP} \
    --admin-password '<server-admin-password>' \
    --database-name ${RESOURCEGROUP}db \
    --public-access 0.0.0.0 \
    --yes

Note

If you receive an error message No available SKUs in this location, you need to specify a different location using the --location parameter and try again. Use the following command to list available SKUs in a specific location:

az mysql flexible-server list-skus --location <Location>

Find a location that has available SKUs and then repeat the preceding az mysql flexible-server create command, but append the appropriate --location <Location> parameter, leaving all the other parameters unchanged.

It takes a few minutes to create the server, database, admin user, and firewall rule that accepts connections from all Azure resources. If the command is successful, the output looks similar to the following example:

{
  "connectionString": "mysql <database-name> --host <server-name>.mysql.database.azure.com --user <server-admin-username> --password=<server-admin-password>",
  "databaseName": "<database-name>",
  "firewallName": "AllowAllAzureServicesAndResourcesWithinAzureIps_2024-7-10_16-22-8",
  "host": "<server-name>.mysql.database.azure.com",
  "id": "/subscriptions/REDACTED/resourceGroups/<resource-group-of-the-OpenShift-cluster>/providers/Microsoft.DBforMySQL/flexibleServers/<server-name>",
  "location": "West US",
  "password": "<server-admin-password>",
  "resourceGroup": "<resource-group-of-the-OpenShift-cluster>",
  "skuname": "Standard_B1ms",
  "username": "<server-admin-username>",
  "version": "5.7"
}

Prepare the Liberty application

We use a Java EE 8 application as our example in this guide. Open Liberty is a Java EE 8 full profile compatible server, so it can easily run the application. Open Liberty is also Jakarta EE 8 full profile compatible.

Run the application on Open Liberty

To run the application on Open Liberty, you need to create an Open Liberty server configuration file so that the Liberty Maven plugin can package the application for deployment. The Liberty Maven plugin isn't required to deploy the application to OpenShift. However, we use it in this example with Open Liberty's developer (dev) mode. Developer mode lets you easily run the application locally. To learn more about the liberty-maven-plugin, see Building a web application with Maven.

Follow the steps in this section to prepare the sample application for later use in this article. These steps use Maven and the liberty-maven-plugin.

Check out the application

Use the following commands to clone the sample code for this guide. The sample is on GitHub.

git clone https://github.com/Azure-Samples/open-liberty-on-aro.git
cd open-liberty-on-aro
export BASE_DIR=$PWD
git checkout 20240920
cd ${BASE_DIR}/3-integration/connect-db/mysql

If you see a message about being in "detached HEAD" state, this message is safe to ignore. It just means you checked out a tag.

There are a few samples in the repository. We use open-liberty-on-aro/3-integration/connect-db/mysql. Here's the file structure of the application:

open-liberty-on-aro/3-integration/connect-db/mysql
├─ src/main/
│  ├─ aro/
│  │  ├─ db-secret.yaml
│  │  ├─ openlibertyapplication.yaml
│  ├─ liberty/config/
│  │  ├─ server.xml
│  ├─ java/
│  ├─ resources/
│  ├─ webapp/
├─ Dockerfile
├─ Dockerfile-wlp
├─ pom.xml

The directories java, resources, and webapp contain the source code of the sample application. The code declares and uses a data source named jdbc/JavaEECafeDB.

In the aro directory, we placed two deployment files. db-secret.xml is used to create Secrets with database connection credentials. The file openlibertyapplication.yaml is used to deploy the application image.

In the root directory, we placed two Dockerfiles. Dockerfile and Dockerfile-wlp are used for local debugging and to build the image for an Azure Red Hat OpenShift deployment, working with Open Liberty and WebSphere Liberty, respectively.

In the liberty/config directory, the server.xml is used to configure the database connection for the Open Liberty and WebSphere Liberty cluster.

Build the project

Using the environment variables defined previously, run the following commands in your terminal to build the project. The POM file for the project reads many properties from the environment.

cd ${BASE_DIR}/3-integration/connect-db/mysql

# The following variables are used for deployment file generation
export DB_SERVER_NAME=$CLUSTER.mysql.database.azure.com
export DB_PORT_NUMBER=3306
export DB_NAME=${RESOURCEGROUP}db
export DB_USER=admin${RESOURCEGROUP}
export DB_PASSWORD='<server-admin-password>'
export NAMESPACE=open-liberty-demo

mvn clean install

(Optional) Test your application locally

Optionally, you can run the application locally to verify that it works as expected. First, you need to add a firewall rule to allow your local machine to connect to the Azure Database for MySQL Flexible Server instance. Use the following steps to add the firewall rule:

  1. Sign in to the Azure portal and navigate to the Azure Database for MySQL Flexible Server instance you created earlier.
  2. In the left pane, select Settings > Networking.
  3. Select Add current client IP address.
  4. Select Save and wait for the firewall rule to be added.

Use the following steps to run the liberty:devc command to locally run and test the project and container image before dealing with any Azure complexity. For more information on liberty:devc, see the Liberty Plugin documentation.

  1. Start your local Docker environment if needed. The instructions for doing this vary depending on the host operating system.

  2. Use the following commands to start the application in liberty:devc mode:

    cd ${BASE_DIR}/3-integration/connect-db/mysql
    
    # If you are running with Open Liberty
    mvn liberty:devc -DcontainerRunOpts="-e DB_SERVER_NAME=${DB_SERVER_NAME} -e DB_PORT_NUMBER=${DB_PORT_NUMBER} -e DB_NAME=${DB_NAME} -e DB_USER=${DB_USER} -e DB_PASSWORD=${DB_PASSWORD}" -Dcontainerfile=Dockerfile
    
    # If you are running with WebSphere Liberty
    mvn liberty:devc -DcontainerRunOpts="-e DB_SERVER_NAME=${DB_SERVER_NAME} -e DB_PORT_NUMBER=${DB_PORT_NUMBER} -e DB_NAME=${DB_NAME} -e DB_USER=${DB_USER} -e DB_PASSWORD=${DB_PASSWORD}" -Dcontainerfile=Dockerfile-wlp
    
  3. Verify the application works as expected. You should see a message similar to [INFO] [AUDIT] CWWKZ0003I: The application javaee-cafe updated in 1.930 seconds. in the command output if successful. Go to https://localhost:9443/ in your browser and verify the application is accessible and all functions are working.

  4. To stop liberty:devc mode, press Control+C.

Prepare the application image

To deploy and run your Liberty application on an Azure Red Hat OpenShift cluster, containerize your application as a Docker image using Open Liberty container images or WebSphere Liberty container images.

Build the application and push to the image stream

Since you already successfully ran the app in the Liberty Docker container using the liberty:devc command, you're going to build the image remotely on the cluster by using the following steps:

  1. Make sure you sign in to the OpenShift CLI using the kubeadmin credentials.

  2. Use the following commands to identify the source directory and Dockerfile:

    cd ${BASE_DIR}/3-integration/connect-db/mysql
    
    # If you are building with the Open Liberty base image, the existing Dockerfile is ready for you
    
    # If you are building with the WebSphere Liberty base image, uncomment and execute the following two commands to rename Dockerfile-wlp to Dockerfile
    # mv Dockerfile Dockerfile.backup
    # mv Dockerfile-wlp Dockerfile
    
  3. Use the following command to change project to open-liberty-demo:

    oc project open-liberty-demo
    
  4. Use the following command to create an image stream:

    oc create imagestream javaee-cafe-mysql
    
  5. Use the following command to create a build configuration that specifies the image stream tag of the build output:

    oc new-build --name javaee-cafe-mysql-config --binary --strategy docker --to javaee-cafe-mysql:v1
    
  6. Use the following command to start the build to upload local contents, containerize, and output to the image stream tag specified before:

    oc start-build javaee-cafe-mysql-config --from-dir . --follow
    

Deploy application on the Azure Red Hat OpenShift cluster

Now you can deploy the sample Liberty application to the Azure Red Hat OpenShift cluster you created earlier when working through the prerequisites.

Deploy the application from the web console

Because we use the Open Liberty Operator to manage Liberty applications, we need to create an instance of its Custom Resource Definition, of type OpenLibertyApplication. The Operator takes care of all aspects of managing the OpenShift resources required for deployment. Use the following steps to create this instance:

  1. Sign in to the OpenShift web console from your browser using the kubeadmin credentials.

  2. Select the project by visiting Home > Projects > open-liberty-demo.

  3. Navigate to Workloads > Secrets.

  4. Select Create > From YAML.

  5. Replace the generated yaml with yours, which is located at <path-to-repo>/3-integration/connect-db/mysql/target/db-secret.yaml.

  6. Select Create. You're returned to the Secret details page.

  7. Navigate to Operators > Installed Operators.

  8. In the middle of the page, you see Open Liberty.

  9. From Provided APIs, select OpenLibertyApplication. The navigation of items in the user interface mirrors the actual containment hierarchy of technologies in use.

    Diagram of Azure Red Hat OpenShift Java Containment.

  10. Select Create OpenLibertyApplication.

  11. Select YAML view for Configure via.

  12. Replace the generated yaml with yours, which is located at <path-to-repo>/3-integration/connect-db/mysql/target/openlibertyapplication.yaml.

  13. Select Create. You're returned to the list of OpenLibertyApplications.

  14. Navigate to Operators > Installed Operators > Open Liberty > OpenLibertyApplication.

  15. Select javaee-cafe-mysql.

  16. In the middle of the page, select Resources.

  17. In the table, select the link for javaee-cafe-mysql with the Kind of Route.

  18. On the page that opens, select the link below Location.

The application home page opens in the browser.

Delete the application from the web console

When you're done with the application, use the following steps to delete the application from Open Shift:

  1. In the left navigation pane, expand the entry for Operators.
  2. Select Installed Operators.
  3. Select Open Liberty.
  4. In the middle of the page, select OpenLibertyApplication.
  5. For javaee-cafe-mysql, select the vertical ellipsis (three vertical dots) then select Delete OpenLibertyApplication.
  6. Select Delete to delete the application.

Use the following steps to delete the secret from Open Shift:

  1. Navigate to Workloads > Secrets.
  2. Select db-secret-mysql.
  3. Select Actions > Delete Secret.
  4. Select Delete to delete the secret.

Clean up resources

Delete the Azure Red Hat OpenShift cluster by following the steps in Tutorial: Delete an Azure Red Hat OpenShift 4 cluster. Make sure the database and any associated resources are deleted too.

Next steps

You can learn more from references used in this guide:

To explore options to run WebSphere products on Azure, see What are solutions to run the WebSphere family of products on Azure?