6.5.4. Connecting the Spring PetClinic sample application to the PostgreSQL database service
To 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.
Procedure
Create a
ServiceBindingCR to project the binding data:$ oc apply -n my-petclinic -f - << EOD --- apiVersion: binding.operators.coreos.com/v1alpha1 kind: ServiceBinding metadata: name: spring-petclinic-pgcluster spec: services:1 - group: postgresql.dev4devs.com kind: Database2 name: sampledatabase version: v1alpha1 application:3 name: spring-petclinic group: apps version: v1 resource: deployments EOD- 1
- Specifies a list of service resources.
- 2
- The CR of the database.
- 3
- The sample application that points to a Deployment or any other similar resource with an embedded PodSpec.
The output verifies that the
ServiceBindingCR is created to project the binding data into the sample application.Example output
servicebinding.binding.operators.coreos.com/spring-petclinic created
Verify that the request for service binding is successful:
$ oc get servicebindings -n my-petclinicExample output
NAME READY REASON AGE spring-petclinic-postgresql True ApplicationsBound 47mBy default, the values from the binding data of the database service are projected as files into the workload container that runs the sample application. For example, all the values from the Secret resource are projected into the
bindings/spring-petclinic-pgclusterdirectory.Once this is created, you can go to the topology to see the visual connection.
그림 6.1. Connecting spring-petclinic to a sample database
Set up the port forwarding from the application port to access the sample application from your local environment:
$ oc port-forward --address 0.0.0.0 svc/spring-petclinic 8080:80 -n my-petclinicExample output
Forwarding from 0.0.0.0:8080 -> 8080 Handling connection for 8080Access http://localhost:8080.
You can now remotely access the Spring PetClinic sample application at localhost:8080 and see that the application is now connected to the database service.