Skip to content

Latest commit

 

History

History
920 lines (761 loc) · 36.6 KB

File metadata and controls

920 lines (761 loc) · 36.6 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 Jakarta REST 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 Jakarta REST 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.1 or later.

All you need to build this project is Java SE 17.0 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, Jakarta REST, 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 Locally

Package the Backend

The backend is packaged and deployed on a provisioned server:

$ mvn clean package

Run a Local PostgreSQL Database

Before running the backend locally, we need to have a local PostgreSQL database that we can connect to. We use the postgresql docker image to create one:

$ docker run --name todo-backend-db -e POSTGRES_USER=todos -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 postgres

This will create a database named todos that we can connect to on localhost:5432 with the credentials todos / mysecretpassword.

Run the Application

With the PostgreSQL database running, we can start the backend by passing the required environment variables to connect to the database:

$ ./target/server/bin/standalone.sh -Denv.POSTGRESQL_DATABASE=todos -Denv.POSTGRESQL_DATASOURCE=ToDos -Denv.POSTGRESQL_SERVICE_HOST=localhost -Denv.POSTGRESQL_SERVICE_PORT=5432 -Denv.POSTGRESQL_USER=todos -Denv.POSTGRESQL_PASSWORD=mysecretpassword

The backend is running, and we can use the HTTP API to manage a list of todos:

# get a list of todos
$ curl http://localhost:8080/todo-backend
[]

# create a todo with the title "This is my first todo item!"
$ curl -X POST -H "Content-Type: application/json"  -d '{"title": "This is my first todo item!"}' http://localhost:8080/todo-backend
{"completed":false,"id":1,"order":0,"title":"This is my first todo item!","url":"https://localhost:8080/1"}%

# get a list of todos with the one that was just created
$ curl http://localhost:8080/todo-backend
[{"completed":false,"id":1,"order":0,"title":"This is my first todo item!","url":"https://localhost:8080/1"}]

Please note that the quickstart includes integration tests, which may be executed using the following command:

$ mvn verify -Pintegration-testing

Run the Backend on OpenShift

Building and running the quickstart application with OpenShift

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

On OpenShift, the S2I build with Apache Maven uses an openshift Maven profile to provision a JBoss EAP server, deploy and run the quickstart in 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>
                <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>...</layers>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            ...
        </plugins>
    </build>
</profile>

You may note that it uses the cloud feature pack which enables a configuration tuned for the OpenShift environment.

Getting Started with JBoss EAP for OpenShift and Helm Charts

This section contains the basic instructions to build and deploy this quickstart to JBoss EAP for OpenShift or JBoss EAP for OpenShift Online using Helm Charts.

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/eap81         ...             ...             A Helm chart to build and deploy EAP 8.1 applications

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 charts --wait --timeout=10m0s 
NAME: todo-backend
...
STATUS: deployed
REVISION: 1

This command will return once the application has successfully deployed. In case of a timeout, you can check the status of the application with the following command in another terminal:

oc get deployment todo-backend

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

apiVersion: v2
name: todo-backend-chart
description: A Helm chart to deploy a JBoss EAP todo-backend application and its Postgresql database
type: application
version: 1.0.0
appVersion: 8.1
dependencies:
    - name: postgresql
      repository: https://charts.bitnami.com/bitnami
      version: 13.1.5
    - name: eap81
      repository: https://jbossas.github.io/eap-charts/
      version: 1.0.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/eap81

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.

Run the Integration Tests with OpenShift

The integration tests included with this quickstart, which verify that the quickstart runs correctly, may also be run with the quickstart running on OpenShift.

Note

The 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 integration-testing profile activated and the proper URL:

$ mvn verify -Pintegration-testing -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.

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.

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.

To be able to connect to the backend from the ToDo Backend Specs or Client, then prepend the host with https://, and append the relative web context /todo-backend. For the previous example host this would be https://todo-backend-jmesnil1-dev.apps.sandbox.x8i5.p1.openshiftapps.com/todo-backend.

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

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.

The use of a Server Provisioned deployment makes it seamless to move from a local deployment for development to a deployment on cloud platforms such as OpenShift and Kubernetes.