Use and Seed SAP BTP PostgreSQL in SAP BTP, Kyma Runtime
Use a BTP-managed PostgreSQL instance with Kyma workloads and seed it with sample data.
Overview
You will learn
- How to use a Kyma Service Binding Secret that points to an SAP BTP PostgreSQL instance
- How to seed the PostgreSQL database with sample schema and data using a Kubernetes Job
Prerequisites
Prerequisites
Steps
Intro
In this tutorial, you will provision a managed PostgreSQL instance on SAP BTP, bind it to your Kyma workload namespace, configure network access, and seed the database with a sample schema and data using a Kubernetes Job.
Go to the kyma-runtime-samples repository. This repository contains a collection of Kyma sample applications which will be used during the tutorial.
Use the green Code button to choose one of the options to download the code locally, or simply run the following command using your CLI at your desired folder location:
Shell/Bashgit clone https://github.com/SAP-samples/kyma-runtime-samples
Open the
database-postgresdirectory in your desired editor.Explore the content of the sample.
Within the
k8sfolder you can findseed-job.yaml, which includes a ConfigMap containing the SQL and a Kubernetes Job that runs thepsqlclient (imagepostgres:15). The Job expects the PostgreSQL connection details to come from a Service Binding Secret namedpostgres-binding. Adjust the Secret name and key names to match your binding if they differ.
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 click 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.
Create the
devnamespace and enableIstio:Shell/Bashkubectl create namespace dev kubectl label namespaces dev istio-injection=enabledNamespaces separate objects inside a Kubernetes cluster. Choosing a different namespace requires adjustments to the provided samples.
Adding the
istio-injection=enabledlabel to the namespace enablesIstio.Istiois the service mesh implementation used by SAP BTP, Kyma runtime.Use the provided
postgres-instance-binding.yamlmanifest to create the PostgreSQL instance and binding:NOTE: The
postgres-instance-binding.yamlfile usesfreeas theservicePlanName. If the free plan is not available in your subaccount, open the file and change theservicePlanNamevalue to match the plan you entitled in the previous step.Shell/Bashkubectl -n dev apply -f ./k8s/postgres-instance-binding.yamlIt takes some time for the instance and binding to get created. To see if they are in the
Createdstate, run:Shell/Bashkubectl -n dev get serviceinstance postgres-instance kubectl -n dev get servicebinding postgres-bindingOnce ready, both resources show
Createdin theSTATUScolumn:
Patch the
ServiceInstanceyou applied above (namespace and name may differ) so it includes both your public IP and the Kyma NAT IP:Shell/BashMY_IP=$(curl -s https://api.ipify.org) KYMA_NAT_IPS=$(kubectl --namespace kyma-system get configmap kyma-info -o json | jq -r '.data["cloud.natGatewayIps"]') kubectl -n dev patch serviceinstance postgres-instance --type=merge \ -p "{\"spec\":{\"parameters\":{\"allow_access\":\"${MY_IP},${KYMA_NAT_IPS}\"}}}"It takes some time before the changes are applied. To see if the instance is updated, run:
Shell/Bashkubectl -n dev get serviceinstance postgres-instance
Apply the ConfigMap and Job to seed the database. Run the following commands from the
database-postgresqldirectory using your CLI:Shell/Bashkubectl -n dev apply -f ./k8s/seed-job.yaml kubectl -n dev get jobs seed-postgresqlWait until the Job shows
1/1in theCOMPLETIONScolumn:CodeNAME COMPLETIONS DURATION AGE seed-postgresql 1/1 12s 30s
Run a temporary Pod that maps the Service Binding Secret keys to
PGHOST,PGPORT,PGDATABASE,PGUSER,PGPASSWORD, andPGSSLMODEand executes a query. Replace the Secret name and keys if your binding differs.Shell/Bashkubectl -n dev apply -f - <<'EOF' apiVersion: v1 kind: Pod metadata: name: pg-client spec: restartPolicy: Never containers: - name: psql image: postgres:15 env: - name: PGHOST valueFrom: secretKeyRef: name: postgres-binding key: hostname - name: PGPORT valueFrom: secretKeyRef: name: postgres-binding key: port - name: PGDATABASE valueFrom: secretKeyRef: name: postgres-binding key: dbname - name: PGUSER valueFrom: secretKeyRef: name: postgres-binding key: username - name: PGPASSWORD valueFrom: secretKeyRef: name: postgres-binding key: password - name: PGSSLMODE valueFrom: secretKeyRef: name: postgres-binding key: sslmode optional: true command: ["psql"] args: ["-v", "ON_ERROR_STOP=1", "-c", "SELECT order_id, description, created FROM orders;"] EOFCheck the Pod logs.
Shell/Bashkubectl -n dev logs pod/pg-clientYou should see a table with two sample orders:
Codeorder_id | description | created ----------+--------------+------------------------- 10000001 | Sample Order 1 | 2024-01-01 00:00:00+00 10000002 | Sample Order 2 | 2024-01-01 00:00:00+00 (2 rows)If you want to delete the Pod, run:
Shell/Bashkubectl -n dev delete pod/pg-client
If you want to remove the seeding assets, run:
kubectl -n dev delete job seed-postgresql
kubectl -n dev delete configmap postgresql-sample-sqlThe PostgreSQL instance itself remains running on SAP BTP and can now be consumed by your Kyma workloads.
Resources
Discussion
Share feedback on this tutorial or join the conversation in SAP Community.