Running a CAP app with PostgreSQL in OpenShift 4
With CAP, SAP has a nice, modern development model. One of the benefits is that it is using a modern architecture combined with commonly used tools. While the default usage scenario for CAP is focused on SAP and their strategy (Cloud Foundry, HANA, BTP), it’s open architecture allows to run it on other platforms like Kubernetes or OpenShift. The obvious benefit is: this allows for running cost efficiently CAP apps in the environment customers and their developer teams are used to. Instead of learning Cloud Foundry – a not so sought-after skillset – developers, architects or DevOps people can stay in their known landscape. Benefitting from their corporate strategy, they can take CAP real multi cloud with K8s or OpenShift and host their app on different hyperscalers.
One containerization software that is using K8s and is available in several clouds is OpenShift. It is very much developer friendly and source code is a first-class citizen. You can deploy source code via S2I, pipelines, Dockerfiles and for standard components, a catalog offering standard services is available. While CAP as provided by SAP is not supporting any other database besides HANA (and SQLite), an adapter for PostgreSQL is available. So why not run CAP apps on OpenShift? The adapter works, better than SQLite and cheaper than HANA.
What do you need? A CAP project, an OpenShift 4 installation, and some time. While I am going to publish blogs that are detailing the setup and individual steps, here I am just showing the high level configuration. When finished, a CAP app with a PostgreSQL database will be running in OpenShift.
Data stored in CAP is persisted as PostgreSQL will use a persistent volume. A restart of the PostgreSQL instance won’t delete the data. Additionally, you can scale up the CAP app by adding new pods. What is not in the example here is a PostgreSQL HA setup. How database changes are handled is also not discussed. The outcome is a working CAP app on OpenShift / K8s. SSO is also a topic for a future blog. There are still many steps involved, maybe a helm chart, a pipeline, is needed to speed up the overall configuration and make it available as an easily to consume template.
Following the steps you can run CAP apps in your K8s environment. A new build can be triggered when the source code of your app changes. Additionally, test can run in the cloud, access to other developers and users provided. See it as an example to show non-SAP developers and teams what is possible without having first finding a sponsor for BTP.
The high level steps are:
- Create project and add configuration
- Deploy PostgreSQL
- Deploy the CAP app
1. Create project
Create a new project in OpenShift for the CAP demo. Add a secrets file and volume.
Go the Secrets and create a new one. Provide the values for the following parameters. These must match the PostgreSQL configuration.
The volume will be used by cds-dbm to store temporary information for the table migration.
2. Deploy PostgreSQL
Deploy a PostgreSQL instance. OpenShift 4 includes a ready to use template that uses persistent storage for the database. The database will automatically start with 1 pod.
3. Deploy cap app
The demo CAP app is available in GitLab: https://gitlab.com/cap16/demo-cap-pg-os4
To add the CAP demo app to the project, add a new deployment from a Dockerfile. While the code is in git, using the “From Git” option is not possible due to cds-dbm. It uses Liquibase to manage the database changes, and therefore depends on Java. The Dockerfile will install Java on the Node.js image.
To add the needed environmental variable for cap to connect to the database, click on “deployment”. The values are matching the environmental variables understood by CAP and the values from the secret. To add values from a secret, click on “Add from ConfigMap or Secret”.
Note By default, cap listens on port 4004. The default route created by OpenShift is pointing to port 8080. Either you change the route afterwards to use port 4004 of the pod or use environment variable PORT to instruct cap to use port 8080.
Click on create to start the build and deploy.
While the image is created, add temporary storage to the deployment configuration.
Wait for the build to finish and OpenShift will start the pod.
CAP automatically creates the database (thanks to cds-dbm), starts and connects to PostgreSQL (thanks to cds-pg) and listens for requests. In OpenShift, the solution consists of CAP and PostgreSQL.
Opening the app in the default route in the browser: http://demo-cap-pg-os-4-test.apps-crc.testing/
Creating some dummy data shows that storing and retrieving data works.
Due to using persistent storage, shutting down and restarting PostgreSQL won’t resolve in lost data. Now you can run your own CAP app in OpenShift.
Giovanni Degani · May 14, 2021 at 10:39
Cool stuff Tobias! Having more sensible alternatives than Cloud Foundry is much needed.
Gheorghe Valer. · June 23, 2021 at 01:03
Hi Tobias. Congratulations for this tutorial. Waiting to see the rest of the series.
I was thinking also to do a cap project with the Openui5 on the client side. The cds build & deploy will work on any Linux server with node environment?
I can participate on this PoC if you need help.
Tobias Hofmann · June 23, 2021 at 13:46
at the reCAP 2021 conference next friday (25 June) I have a presentation and will show how to run CAP inside OpenShift with a PostgreSQL HA Cluster as database and a UI5 app. Communication to the CAP API is protected via OpenID Connect.