Connecting PetClinic application to an Operator-backed PostgreSQL database
In this configuration, we leverage the Service Binding Operator to collect the binding data from the PostgreSQL database and to project them into the Spring PetClinic application.
The PostgreSQL database in this scenario is deployed using the
This scenario involves the following procedures:
This scenario uses a PostgreSQL database service, which you must install using the Operator Lifecycle Manager (OLM).
You have installed the Crunchy Postgres Operator from Crunchy Data available at OperatorHub.io and the Operator is available in the
The installation of the Crunchy PostgreSQL Operator does not create a database instance. To create a database service instance, you must create the following custom resource (CR), which will be processed by the Operator:
PostgresCluster resource to run the actual database instance
- name: instance1
- name: repo1
To create a database instance, create a
PostgresClusterCR by running the following command:
kubectl apply -f https://redhat-developer.github.io/service-binding-operator/userguide/getting-started/_attachments/pgcluster-deployment.yaml -n my-petclinic
After the database is created, verify that all the pods in the
my-petclinicnamespace are running (it will take a few minutes):
kubectl get pods -n my-petclinicExample output:
NAME READY STATUS RESTARTS AGE hippo-backup-bthn-p6xmn 0/1 Completed 0 16s hippo-instance1-jqpz-0 4/4 Running 0 99s hippo-repo-host-0 2/2 Running 0 99s
The previous output verifies that the database service is created and configured.
Now, after the database is configured for the application, you can deploy the application and connect it to the database service.
To deploy the Spring PetClinic application on our Kubernetes cluster, use a deployment configuration consisting of the following resources:
Deployment resource to run the actual application instance
- name: app
- name: SPRING_PROFILES_ACTIVE
- name: http
Service resource to provide a way to access the application UI
- port: 80
Create the application resources by running the following command:
kubectl apply -f https://redhat-developer.github.io/service-binding-operator/userguide/getting-started/_attachments/petclinic-deployment.yaml -n my-petclinic
At this stage, the application is not yet connected to the database service. Hence the pod fails to start. To verify run the following command:
kubectl get pods -n my-petclinicExample output:
NAME READY STATUS RESTARTS AGE spring-petclinic-5d47b7dbcd-7zd8v 0/1 CrashLoopBackOff 1 (7s ago) 28s
You can now use the Service Binding Operator to connect the application to the database service.
In the absence of the Service Binding Operator, as an administrator of the application, you must perform the following steps manually to extract all the configuration details, create a
To leverage the Service Binding Operator as a way to easily and safely connect the sample application to the database service, you must create a
ServiceBinding custom resource (CR) that triggers the Service Binding Operator to project the binding data into the application:
ServiceBinding resource to project the binding data
- group: postgres-operator.crunchydata.com
.spec field of the
ServiceBinding CR has two sections:
The first section is a list of service resources (
.spec.services). The services resources point to the database service resources. For more information on how the values are exposed from the service resources, see the Exposing binding data section.
The second section is the application (
.spec.application). The application points to a
Deploymentor any resource that is compliant with PodSpec.
ServiceBindingCR by running the following command in shell:
kubectl apply -f https://redhat-developer.github.io/service-binding-operator/userguide/getting-started/_attachments/petclinic-pgcluster-binding.yaml -n my-petclinic
Verify that the request for service binding is successful by running the following command:
kubectl get servicebindings -n my-petclinicExample output:
NAME READY REASON AGE spring-petclinic-pgcluster True ApplicationsBound 28s
By creating this
ServiceBindingresource, we now have the binding data values from the database that is to be projected into the application container as files, by default. Alternatively, you can also choose to project the binding data values as environment variables if you prefer. If you check under the
/bindings/spring-petclinic-pgclusterdirectory, you can see all the values from the
Secretresource projected there.
In the case of the previous example, you can find
passwordas the projected values. The values pointed out through the annotation are also projected, such as the
portvalues. For connectivity, the
typevalue is projected as the binding data.
The application looks for the
SERVICE_BINDING_ROOTenvironment variable to find the location of the
/bindingsdirectory. The Spring Boot application used here uses the Spring Cloud Bindings library and it looks for the
SERVICE_BINDING_ROOTenvironment variable to get the projected binding data. For more information on how an application uses these values, see the Projecting binding data section.
Verify that the binding is successful by setting up the port forwarding from the application port to access the sample application from your local environment:
kubectl port-forward --address 0.0.0.0 svc/spring-petclinic 8080:80 -n my-petclinicExample output:
Forwarding from 0.0.0.0:8080 -> 8080
You can now remotely access web UI of the application and see that the application is now connected to the database service.
For more information on creating requests for service binding, see the Creating service binding section.
In this scenario, we set up an Operator-backed PostgreSQL database and connected it to the Spring PetClinic application using the Service Binding Operator to collect the binding data and expose them to the application.
By using service bindings, developers are able to more easily leverage the services available to them on a Kubernetes cluster. This method provides consistency across different services and is repeatable for the developers. Service binding provides a unified way to create a binding connection between the application and service and eliminates the need for the usual manual and error-prone configuration.
For more information, see the following sections: