Deploying Java EE Apps on OpenShift: A WildFly & MySQL Guide

Snippet of programming code in IDE
Published on

Deploying Java EE Apps on OpenShift: A WildFly & MySQL Guide

In this blog post, we'll discuss how to deploy a Java EE application on OpenShift using WildFly as the application server and MySQL as the database. OpenShift is a popular cloud platform for deploying, managing, and scaling modern applications.

Prerequisites

Before we dive into the deployment process, make sure you have the following prerequisites in place:

  • An OpenShift account
  • OpenShift CLI (oc) installed
  • A Java EE application with a WAR file
  • WildFly application server
  • MySQL database

If you don't have these components ready, you can refer to the official OpenShift documentation to get started.

Setting Up the OpenShift Environment

First, let's log in to OpenShift using the CLI:

oc login <OpenShift URL>

Once logged in, create a new project:

oc new-project <project-name>

Deploying WildFly on OpenShift

WildFly can be easily deployed to OpenShift using the WildFly source-to-image (S2I) builder. This builder takes your application source code and builds a deployable WildFly image.

oc new-app wildfly:latest~<path-to-your-app-source-code> --name=<app-name>

Now, expose the service to make it accessible from outside the OpenShift cluster:

oc expose svc/<app-name>

Configuring MySQL Database

Next, we need to set up a MySQL database on OpenShift. You can use the official MySQL container image from the OpenShift Container Platform catalog. Ensure that you configure the necessary environment variables such as database name, username, and password.

oc new-app mysql:latest --name=<db-name> -e MYSQL_USER=<username> -e MYSQL_PASSWORD=<password> -e MYSQL_DATABASE=<db-name>

Expose the MySQL service:

oc expose svc/<db-name>

Connecting WildFly to MySQL

To allow the WildFly application to access the MySQL database, create a secret in OpenShift to store the database credentials:

oc create secret generic <db-secret> --from-literal=username=<username> --from-literal=password=<password> --from-literal=database=<db-name>

Then, configure the WildFly deployment to use this secret:

oc set env dc/<app-name> --from=secret/<db-secret>

Deploying the Java EE Application

If your Java EE application uses JPA or JDBC to connect to the database, ensure that the data source is configured in the WildFly standalone.xml or standalone-full.xml configuration file.

Once the data source is configured, build and deploy the application to OpenShift:

oc start-build <app-name> --from-file=<path-to-your-war-file>
oc rollout latest dc/<app-name>

Verifying the Deployment

After deploying the application, verify that it's running and accessible. Retrieve the route URL for the WildFly service:

oc get route <app-name>

Use this URL to access your Java EE application and ensure that it can connect to the MySQL database without any issues.

Scaling and Monitoring

OpenShift provides robust scaling and monitoring capabilities for your deployed applications. You can scale the WildFly pods based on resource utilization and set up monitoring dashboards using built-in OpenShift features or integrate with external monitoring tools.

Lessons Learned

Deploying a Java EE application on OpenShift with WildFly and MySQL is straightforward and provides a scalable and manageable environment for your enterprise applications. OpenShift's container orchestration capabilities, combined with WildFly's Java EE support and MySQL's reliability, make it a compelling choice for Java developers.

Now that you've learned how to deploy Java EE applications on OpenShift, experiment with different configurations, explore advanced deployment options, and leverage OpenShift's features to optimize your Java application's performance and resilience.

For more in-depth information on OpenShift, visit the OpenShift documentation and WildFly documentation to enhance your understanding and skills in deploying Java applications.

Start leveraging the power of OpenShift for your Java EE applications today!