1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-05 12:46:18 +01:00
Files
openshift-docs/modules/sbo-connecting-spring-petclinic-sample-app-to-postgresql-database-service.adoc
2024-03-15 12:15:52 +00:00

102 lines
3.4 KiB
Plaintext

// Module included in the following assemblies:
//
// * /applications/connecting_applications_to_services/getting-started-with-service-binding.adoc
:_mod-docs-content-type: PROCEDURE
[id="sbo-connecting-spring-petclinic-sample-app-to-postgresql-database-service_{context}"]
= 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 {servicebinding-title} to project the binding data into the application.
[discrete]
.Procedure
. Create a `ServiceBinding` CR to project the binding data:
+
[source,terminal]
----
$ oc apply -n my-petclinic -f - << EOD
---
apiVersion: binding.operators.coreos.com/v1alpha1
kind: ServiceBinding
metadata:
name: spring-petclinic-pgcluster
spec:
services: <1>
- group: postgres-operator.crunchydata.com
version: v1beta1
kind: PostgresCluster <2>
name: hippo
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 `ServiceBinding` CR is created to project the binding data into the sample application.
+
.Example output
[source,terminal]
----
servicebinding.binding.operators.coreos.com/spring-petclinic created
----
. Verify that the request for service binding is successful:
+
[source,terminal]
----
$ oc get servicebindings -n my-petclinic
----
+
.Example output
[source,terminal]
----
NAME READY REASON AGE
spring-petclinic-pgcluster True ApplicationsBound 7s
----
+
By 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-pgcluster` directory.
+
[NOTE]
====
Optionally, you can also verify that the files in the application contain the projected binding data, by printing out the directory contents:
[source,terminal]
----
$ for i in username password host port type; do oc exec -it deploy/spring-petclinic -n my-petclinic -- /bin/bash -c 'cd /tmp; find /bindings/*/'$i' -exec echo -n {}:" " \; -exec cat {} \;'; echo; done
----
.Example output: With all the values from the secret resource
[source,text]
----
/bindings/spring-petclinic-pgcluster/username: <username>
/bindings/spring-petclinic-pgcluster/password: <password>
/bindings/spring-petclinic-pgcluster/host: hippo-primary.my-petclinic.svc
/bindings/spring-petclinic-pgcluster/port: 5432
/bindings/spring-petclinic-pgcluster/type: postgresql
----
====
. Set up the port forwarding from the application port to access the sample application from your local environment:
+
[source,terminal]
----
$ oc port-forward --address 0.0.0.0 svc/spring-petclinic 8080:80 -n my-petclinic
----
+
.Example output
[source,terminal]
----
Forwarding from 0.0.0.0:8080 -> 8080
Handling connection for 8080
----
. Access link:http://localhost:8080/petclinic[http://localhost:8080/petclinic].
+
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.