Deploy a CAP Application with PostgreSQL in SAP BTP, Kyma Runtime
Deploy PostgreSQL in SAP BTP, Kyma runtime for your CAP Bookstore application.
Overview
You will learn
- How to add and configure PostgreSQL to your Kyma environment.
- How to deploy a CAP application in your Kyma environment.
Prerequisites
Prerequisites
- SAP BTP, Kyma runtime enabled
- kubectl installed
- Node.js and cds-dk installed
- Docker installed
- Sufficient quota to add PostgreSQL entitlements in your environment
Steps
Intro
In this tutorial, you will add PostgreSQL entitlements to your subaccount and deploy a CAP Application in the Kyma environment.
From your subaccount overview in the SAP BTP cockpit, go to Entitlements and choose Edit.
Choose Add Service Plans and search for PostgreSQL, Hyperscaler Option.
Select free, and choose Add 1 Service Plan.
NOTE: The available service plans depend on your account configuration. If the free plan is not available, select the plan that matches your subaccount entitlements.
Choose Save.
You are now ready to deploy a PostgreSQL instance in your subaccount.
For the sake of this tutorial, the chosen entitlement units are sufficient. However, when sizing and configuring your environment, consider the necessary amount of entitlement units (2GB/4GB memory blocks and 5GB storage blocks) to support your architecture. Depending on the hyperscaler, configurations can consume different numbers of entitlement units. See Sizing and Service Plans and Entitlements.
Prepare your sample project:
Shellcds init bookshop --add sample,nodejs && cd bookshopGo to
db/schema.cdsand fordescrchangelocalized Stringtolocalized LargeString.CDSentity Books : managed { key ID : Integer; author : Association to Authors @mandatory; title : localized String @mandatory; descr : localized LargeString; genre : Association to Genres; stock : Integer; price : Price; currency : Currency; }
Add PostgreSQL deployment configuration:
Shellcds add postgres --for productionTIP: For more information, see Add PostgreSQL Deployment Configuration.
In
package.json, change[production].modelfromdb/hanatodb/postgresand add theauthsection:JSON"cds": { "requires": { "db-ext": { "[development]": { "model": "db/sqlite" }, "[production]": { "model": "db/postgres" } }, "[production]": { "db": "postgres" }, "auth": { "[production]": { "kind": "dummy" } } } }With the dummy authentication, you pass all the authorization checks. It’s intended only for development and should not be used in production environments. For more information, see Dummy Authentication.
Generate the Helm chart structure:
Shellcds add kymaWhen prompted, provide your registry username and cluster domain, for example:
- registry server: user123
- cluster domain: abc123.kyma.ondemand.com
TIP: To get your Kyma cluster domain, run:
Codekubectl get gateways.networking.istio.io -n kyma-system kyma-gateway \ -o jsonpath='{.spec.servers[0].hosts[0]}'This creates:
- chart/Chart.yaml โ Helm chart metadata
- chart/values.yaml โ Configuration file
- containerize.yaml โ Configuration for building and publishing container images
In your
values.yamlfile, changepostgres-db-deployertopostgres-deployerand changepostgres.servicePlanNameto match the service plan you added in the entitlements step.YAML... postgres-deployer: image: repository: bookshop-postgres-deployer bindings: postgres: serviceInstanceName: postgres ... postgres: serviceOfferingName: postgresql-db servicePlanName: free ...NOTE: The
servicePlanNamevalue must match the service plan available in your account. Replacefreewith the plan you selected in the entitlements step if different.If you want to connect to the PostgreSQL instance during local development, add your static internet IP address to the
allow_accessparameter invalues.yaml. The Kyma NAT IP Gateway is already added bycds add kyma. Append your static IP after a comma:YAMLpostgres: serviceOfferingName: postgresql-db servicePlanName: free parameters: allow_access: "<kyma-nat-ip-gateway>,<your-static-internet-ip-address>"TIP: To find your public IP address, you can run
curl ifconfig.mein your terminal.
Create the
cap-bookstorenamespace and enableistio:Shellkubectl create namespace cap-bookstore kubectl label namespaces cap-bookstore istio-injection=enabledDeploy your application to Kyma:
Shellcds up -2 k8s --namespace cap-bookstoreYou might get a warning that your registry server is invalid. If so, simply enter your Docker username again.
Create an image pull secret when prompted.
This command executes the following actions:
- Builds your CAP application (
cds build --production). - Generates all necessary Helm templates in
gen/chart/templates/. - Creates container images for srv and database deployer using Cloud Native Buildpacks.
- Pushes images to your container registry (configured in
containerize.yaml). - Deploys using Helm to your Kyma cluster.
- Runs the database deployer Job to initialize PostgreSQL with the schema and CSV data.
NOTE: The deployment process might timeout, especially during the first deployment when images are being built and pushed. If this happens, the deployment will continue in the background. You can check the status using:
Codekubectl get pods -n cap-bookstoreWait until all Pods show
Runningstatus before proceeding to verification.- Builds your CAP application (
- In Kyma dashboard, go to your
cap-bookstorenamespace, and select API Rules. - Choose
bookshop-srvand select the link underHostsfrom the Status section. - Add
/odata/v4/catalog/Booksat the end of the link. You should see your books and authors.
Congratulations! You have now deployed your CAP application with PostgreSQL in SAP BTP, Kyma runtime.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.