Skip to content

Latest commit

 

History

History
1118 lines (907 loc) · 43.8 KB

README.adoc

File metadata and controls

1118 lines (907 loc) · 43.8 KB

todo-backend: quickstart for backend deployment on OpenShift

The todo-backend quickstart demonstrates how to implement a backend that exposes a HTTP API with JAX-RS to manage a list of ToDo which are persisted in a database with JPA.

This quickstart shows how to deploy a JBoss EAP application on OpenShift that connects to a PostgreSQL database also hosted on OpenShift.

What is it?

The todo-backend quickstart demonstrates how to implement a backend that exposes a HTTP API with JAX-RS to manage a list of ToDo which are persisted in a database with JPA.

  • The backend exposes a HTTP API to manage a list of todos that complies with the specs defined at todobackend.com.

  • It requires a connection to a PostgreSQL database to persist the todos.

  • It can be build with JBoss EAP S2I images for cloud deployment

  • It is deployed on OpenShift using the Helm Chart for JBoss EAP.

System Requirements

The application this project produces is designed to be run on Red Hat JBoss Enterprise Application Platform 8.0 or later.

All you need to build this project is Java 11.0 (Java SDK 11) or later and Maven 3.6.0 or later. See Configure Maven to Build and Deploy the Quickstarts to make sure you are configured correctly for testing the quickstarts.

Architecture

Architecture with S2I

This backend is built using JBoss EAP S2I Builder and Runtime images.

When the image is built, org.jboss.eap.plugins:eap-maven-plugin plugin provisions the JBoss EAP application server and all the feature packs it needs for its features. The layers are defined in the pom.xml file in the <configuration> section of the org.jboss.eap.plugins:eap-maven-plugin plugin:

<layers>
  <layer>cloud-server</layer>
  <layer>postgresql-datasource</layer>
</layers>

The cloud-server layer provides everything needed to run the backend on OpenShift. This also includes access to Jakarta EE APIs such as CDI, JAX-RS, JPA, etc. These two layers comes from the JBoss EAP feature pack provided in the JBoss EAP S2I builder image.

The postgresql-datasource layer provides a JDBC driver and DataSource to connect to a PostgreSQL database. It is also provided by the org.jboss.eap:eap-datasources-galleon-pack feature pack.

The Git repository for this feature pack is hosted at https://github.com/jbossas/eap-datasources-galleon-pack. It provides JDBC drivers and datasources for different databases but for this quickstart, we will only need the postgresql-datasource.

Connection to the PostgreSQL database

As mentioned, the JDBC drivers and datasource configuration that the backend uses to connect to the PostgreSQL database is provided by the org.jboss.eap:eap-datasources-galleon-pack feature pack.

By default, it exposes a single datasource. In the backend, the name of this datasource is ToDos and is specified in the persistence.xml to configure JPA:

<persistence-unit name="primary">
  <jta-data-source>java:jboss/datasources/ToDos</jta-data-source>
</persistence-unit>

At runtime, we only need a few environment variables to establish the connection from JBoss EAP to the external PostgreSQL database:

  • POSTGRESQL_DATABASE - the name of the database (that will be called todos)

  • POSTGRESQL_SERVICE_HOST - the host to connect to the database

  • POSTGRESQL_SERVICE_PORT - The port to connect to the database

  • POSTGRESQL_USER & POSTGRESQL_PASSWORD - the credentials to connect to the database

  • POSTGRESQL_DATASOURCE - The name of the datasources (as mentioned above, it will be ToDos)

Filters for Cross-Origin Resource Sharing (CORS)

The Web frontend for this quickstart uses JavaScript calls to query the backend’s HTTP API. We must enable Cross-Origin Resource Sharing (CORS) filters in the undertow subsystem of JBoss EAP to allow these HTTP requests to succeed.

Configuration with JBoss EAP S2I

As we use S2I to provision the server and build the application, we provide a CLI script that contains all the commands to create and configure the CORS filters in Undertow. This script is located in the src/scripts/cors_filters.cli.

This script is executed at build time and will provide the following HTTP headers to enabled CORS:

  • Access-Control-Allow-Origin: *

  • Access-Control-Allow-Methods: GET, POST, OPTION, PUT, DELETE, PATCH

  • Access-Control-Allow-Headers: accept, authorization, content-type, x-requested-with

  • Access-Control-Allow-Credentials: true

  • Access-Control-Max-Age: 1

By default, the backend accepts requests from any origin (*). This is only simplicity. It is possible to restrict the allowed origin using the environment variable CORS_ORIGIN at runtime.

Run the Backend on OpenShift

Build the JBoss EAP Source-to-Image (S2I) Quickstart to OpenShift with Helm Charts

On OpenShift, the S2I build with Apache Maven will use an openshift profile used to provision a JBoss EAP server to deploy and run the quickstart in OpenShift environment. You can activate the Maven profile named openshift when building the quickstart:

$ mvn clean package -Popenshift

The provisioned JBoss EAP server for OpenShift, with the quickstart deployed, can then be found in the target/server directory, and its usage is similar to a standard server distribution. You may note that it uses the cloud feature pack which enables a configuration tuned for OpenShift environment.

The server provisioning functionality is provided by the EAP Maven Plugin, and you may find its configuration in the quickstart pom.xml:

        <profile>
            <id>openshift</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jboss.eap.plugins</groupId>
                        <artifactId>eap-maven-plugin</artifactId>
                        <version>${version.eap.maven.plugin}</version>
                        <configuration>
                            <feature-packs>
                                <feature-pack>
                                    <location>org.jboss.eap:wildfly-ee-galleon-pack</location>
                                </feature-pack>
                                <feature-pack>
                                    <location>org.jboss.eap.cloud:eap-cloud-galleon-pack</location>
                                </feature-pack>
                            </feature-packs>
                            <layers>
                                <layer>cloud-server</layer>
                            </layers>
                            <filename>ROOT.war</filename>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>package</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
Note

Since the plugin configuration above deploys quickstart on root web context of the provisioned server, the URL to access the application should not have the /todo-backend path segment after HOST:PORT.

Prerequisites

  • You must be logged in OpenShift and have an oc client to connect to OpenShift

  • Helm must be installed to deploy the backend on OpenShift.

Once you have installed Helm, you need to add the repository that provides Helm Charts for JBoss EAP:

$ helm repo add jboss-eap https://jbossas.github.io/eap-charts/
"jboss-eap" has been added to your repositories
$ helm search repo jboss-eap
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
jboss-eap/eap8         ...             ...             A Helm chart to build and deploy 8.0 applications

Deploy a PostgreSQL Database on OpenShift

$ oc new-app postgresql-ephemeral \
   -p DATABASE_SERVICE_NAME=todo-backend-db \
   -p POSTGRESQL_DATABASE=todos

This will create a PostgreSQL database named todos on OpenShift that can be accessed on the port 5432 on the service todo-backend-db. We don’t need to copy the credentials to connect to the database as we will retrieve them later using the todo-backend-db secret that was created when the database is deployed.

The backend will be built and deployed on OpenShift with a Helm Chart for JBoss EAP.

Deploy the JBoss EAP Source-to-Image (S2I) Quickstart to OpenShift with Helm Charts

Log in to your OpenShift instance using the oc login command. The backend will be built and deployed on OpenShift with a Helm Chart for JBoss EAP.

Navigate to the root directory of this quickstart and run the following command:

$ helm install todo-backend -f charts/helm.yaml jboss-eap/eap8
NAME: todo-backend
...
STATUS: deployed
REVISION: 1

The Helm Chart for this quickstart contains all the information to build an image from the source code using S2I on Java 17:

build:
  uri: https://github.com/jboss-developer/jboss-eap-quickstarts.git
  ref: 8.0.x
  contextDir: todo-backend
deploy:
  replicas: 1

This will create a new deployment on OpenShift and deploy the application.

If you want to see all the configuration elements to customize your deployment you can use the following command:

$ helm show readme jboss-eap/eap8

Let’s wait for the application to be built and deployed:

$ oc get deployment todo-backend -w
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
todo-backend   1         1         1            0           12s
...
todo-backend   1         1         1            1           2m

Get the URL of the route to the deployment.

$ oc get route todo-backend -o jsonpath="{.spec.host}"

Access the application in your web browser using the displayed URL.

Note

The Maven profile named openshift is used by the Helm chart to provision the server with the quickstart deployed on the root web context, and thus the application should be accessed with the URL without the /todo-backend path segment after HOST:PORT.

Undeploy the JBoss EAP Source-to-Image (S2I) Quickstart from OpenShift with Helm Charts

$ helm uninstall todo-backend

Environment variables for PostgreSQL

The Helm Chart also contains the environment variables required to connect to the PostgreSQL database.

For OpenShift, we rely on secrets so that the credentials are never copied outside OpenShift:

deploy:
  env:
    - name: POSTGRESQL_PASSWORD
      valueFrom:
        secretKeyRef:
          key: database-password
          name: todo-backend-db

When the application is deployed, the value for the POSTGRESQL_PASSWORD will be taken from the key database-password in the secret todo-backend-db.

Run the Arquillian Integration Tests with OpenShift

This quickstart includes Arquillian integration tests. They are located under the src/test/ directory. The integration tests verify that the quickstart runs correctly when deployed on the server.

Note

The Arquillian integration tests expect a deployed application, so make sure you have deployed the quickstart on OpenShift before you begin.

Run the integration tests using the following command to run the verify goal with the arq-remote profile activated and the proper URL:

$ mvn clean verify -Parq-remote -Dserver.host=https://$(oc get route todo-backend --template='{{ .spec.host }}')
Note

The tests are using SSL to connect to the quickstart running on OpenShift. So you need the certificates to be trusted by the machine the tests are run from.

Use the todobackend Web Frontend

Once the backend is deployed on OpenShift, it can be accessed from the route todo-backend. Let’s find the host that we can use to connect to this backend:

$ oc get route todo-backend -o jsonpath="{.spec.host}"
todo-backend-jmesnil1-dev.apps.sandbox.x8i5.p1.openshiftapps.com

This value will be different for every installation of the backend.

Warning

Make sure to prepend the host with https:// to be able to connect to the backend from the ToDo Backend Specs or Client. The host must also be publicly accessible.

We can verify that this application is properly working as a ToDo Backend by running its specs on it.

Once all tests passed, we can use the todobackend client to have a Web application connected to the backend.

Note

todobackend.com is an external service used to showcase this quickstart. It might not always be functional but does not impact the availability of this backend.

Clean Up

Remove the Backend

The backend can be deleted from OpenShift by running the command:

$ helm uninstall todo-backend
release "todo-backend" uninstalled

Remove the Database

The PostresSQL database can be deleted from OpenShift by running the commands:

$ oc delete all -l template=postgresql-ephemeral-template
replicationcontroller "todo-backend-db-1" deleted
service "todo-backend-db" deleted
deploymentconfig.apps.openshift.io "todo-backend-db" deleted
$ oc delete secret todo-backend-db
secret "todo-backend-db" deleted

Conclusion

This quickstart shows how the datasource feature pack provided by JBoss EAP simplifies the deployment of a JBoss EAP Jakarta EE backend on OpenShift to connect to an external database and exposes an HTTP API.