In this blog post I will guide you through the process of attaching a pre-provisioned PostgreSQL database to an application that’s running on Tanzu Platform for Kubernetes (TP for K8S). For this tutorial, I am using a demo app called Emoji Inclusion (original here, TP for K8s ‘version’ here – kudos to both Timo and Julien). This Spring Boot app will connect to a persistent PostgreSQL database using Kubernetes Service Bindings in combination with Spring Cloud Bindings.
Step 1: Clone and Deploy the Application
First, clone and deploy the application by running the following command:
tanzu deploy -y
Note: If this is the first time you’re working with Tanzu Platform for Kubernetes, I recommend reviewing this post or this example in the official documentation to familiarize yourself with the deployment process.
Once the Emoji Inclusion app has been successfully deployed, the interface will display information about the type of database connection in use.
Initially, it will connect to an in-memory H2 database. We aim to replace this with a PostgreSQL database by attaching a pre-provisioned PostgreSQL service, which in this example is accessible at the IP address 10.100.184.42
.
Step 2: Define Egress Point
To enable the application to connect to the PostgreSQL database, we need to define an egress point. This will allow traffic to flow between the app and the external database. The required egress settings are as follows:
- Database: PostgreSQL
- Port:
5432/tcp
You define an egress point within the context of a space.
Step 3: Define the pre-provisioned service
No it’s time to define the pre-provisioned service, in this case a JDBC URL that allows the app to interact with the external PostgreSQL database.
In the Tanzu Platform (within the context of the space being used), follow these steps:
- Navigate to the Services section.
- Select Create Service and then Attach Service.
- Give the service a descriptive name (e.g.,
psql
).
Under Connector apply the following settings:
You can also choose to only define the JDBC URL, this will look like:
jdbc:postgresql://10.100.184.42:5432/postgres_viktorious?user=viktorious&password=VMware1
Assign this value to the jdbc-url field.
Step 4: Bind the Service to the Application
In the this step we need to bind the connector to the our application:
Click “Create Service” to update the configuration of the application. This action will trigger the automatic redeployment of the application pods. To monitor this process, run:
k get pod -n <space-name-with-id>
You will also see that the interface reports that the pre-provisioned service is configured and bound to you application:
The interface will also report the updated configuration:
Step 5: Examining the configuration using command line commands
Let’s go through a series of CLI commands to examine the configuration.Of course you first need to login and select your project and space:
tanzu logintanzu project usetanzu space use
Now use
tanzu services list
To get on an overview of available services in the space. Now use
tanzu services get PreProvisionedService/psql
to get additional details on the PreProvisioned service. The tanzu services command also allows you to define new services and create/bind/unbind these service through the command line.
kubectl get preprovisionedservices.services.tanzu.vmware.com psql -o yaml
Will give you the YAML description of the PreProvisioned service.
Other commands of interest are:
kubectl get servicebindings.servicebinding.io psql-61j4b -o yaml
You will see that the Deployment of our app is linked to a secret that is containing the DB credentials.
kubectl get servicebindings.services.tanzu.vmware.com psql-61j4b -o yaml
Defines the connection between the Application (using the Containerapp interface) and the PreProvisoned service (note that actual names for these object may be different in your environment).
The DB credentials are contained in the secret that was referenced in the ServiceBinding yaml:
kubectl get secret psql-yxhjs -o yaml
That’s it! You’ve successfully attached a pre-provisioned PostgreSQL database to your app on the Tanzu Platform for Kubernetes. I hope this guide helps you get started with managing external services in your K8s-based applications.