Getting started with service binding on IBM Power, IBM Z, and IBM® LinuxONE

The Service Binding Operator manages the data plane for workloads and backing services. This guide provides instructions with examples to help you create a database instance, deploy an application, and use the Service Binding Operator to create a binding connection between the application and the database service.

Prerequisites

  • You have access to an OKD cluster using an account with cluster-admin permissions.

  • You have installed the oc CLI.

  • You have installed the Service Binding Operator from OperatorHub.

Deploying a PostgreSQL Operator

Procedure

  1. To deploy the Dev4Devs PostgreSQL Operator in the my-petclinic namespace run the following command in shell:
  1. $ oc apply -f - << EOD
  2. ---
  3. apiVersion: v1
  4. kind: Namespace
  5. metadata:
  6. name: my-petclinic
  7. ---
  8. apiVersion: operators.coreos.com/v1
  9. kind: OperatorGroup
  10. metadata:
  11. name: postgres-operator-group
  12. namespace: my-petclinic
  13. ---
  14. apiVersion: operators.coreos.com/v1alpha1
  15. kind: CatalogSource
  16. metadata:
  17. name: ibm-multiarch-catalog
  18. namespace: openshift-marketplace
  19. spec:
  20. sourceType: grpc
  21. image: quay.io/ibm/operator-registry-<architecture> (1)
  22. imagePullPolicy: IfNotPresent
  23. displayName: ibm-multiarch-catalog
  24. updateStrategy:
  25. registryPoll:
  26. interval: 30m
  27. ---
  28. apiVersion: operators.coreos.com/v1alpha1
  29. kind: Subscription
  30. metadata:
  31. name: postgresql-operator-dev4devs-com
  32. namespace: openshift-operators
  33. spec:
  34. channel: alpha
  35. installPlanApproval: Automatic
  36. name: postgresql-operator-dev4devs-com
  37. source: ibm-multiarch-catalog
  38. sourceNamespace: openshift-marketplace
  39. ---
  40. apiVersion: rbac.authorization.k8s.io/v1
  41. kind: ClusterRole
  42. metadata:
  43. name: database-view
  44. labels:
  45. servicebinding.io/controller: "true"
  46. rules:
  47. - apiGroups:
  48. - postgresql.dev4devs.com
  49. resources:
  50. - databases
  51. verbs:
  52. - get
  53. - list
  54. EOD
1The Operator image.
  • For IBM Power: quay.io/ibm/operator-registry-ppc64le:release-4.9

  • For IBM Z and IBM® LinuxONE: quay.io/ibm/operator-registry-s390x:release-4.8

Verification

  1. After the operator is installed, list the operator subscriptions in the openshift-operators namespace:

    1. $ oc get subs -n openshift-operators

    Example output

    1. NAME PACKAGE SOURCE CHANNEL
    2. postgresql-operator-dev4devs-com postgresql-operator-dev4devs-com ibm-multiarch-catalog alpha
    3. rh-service-binding-operator rh-service-binding-operator redhat-operators stable

Creating a PostgreSQL database instance

To create a PostgreSQL database instance, you must create a Database custom resource (CR) and configure the database.

Procedure

  1. Create the Database CR in the my-petclinic namespace by running the following command in shell:

    1. $ oc apply -f - << EOD
    2. apiVersion: postgresql.dev4devs.com/v1alpha1
    3. kind: Database
    4. metadata:
    5. name: sampledatabase
    6. namespace: my-petclinic
    7. annotations:
    8. host: sampledatabase
    9. type: postgresql
    10. port: "5432"
    11. service.binding/database: 'path={.spec.databaseName}'
    12. service.binding/port: 'path={.metadata.annotations.port}'
    13. service.binding/password: 'path={.spec.databasePassword}'
    14. service.binding/username: 'path={.spec.databaseUser}'
    15. service.binding/type: 'path={.metadata.annotations.type}'
    16. service.binding/host: 'path={.metadata.annotations.host}'
    17. spec:
    18. databaseCpu: 30m
    19. databaseCpuLimit: 60m
    20. databaseMemoryLimit: 512Mi
    21. databaseMemoryRequest: 128Mi
    22. databaseName: "sampledb"
    23. databaseNameKeyEnvVar: POSTGRESQL_DATABASE
    24. databasePassword: "samplepwd"
    25. databasePasswordKeyEnvVar: POSTGRESQL_PASSWORD
    26. databaseStorageRequest: 1Gi
    27. databaseUser: "sampleuser"
    28. databaseUserKeyEnvVar: POSTGRESQL_USER
    29. image: registry.redhat.io/rhel8/postgresql-13:latest
    30. databaseStorageClassName: nfs-storage-provisioner
    31. size: 1
    32. EOD

    The annotations added in this Database CR enable the service binding connection and trigger the Operator reconciliation.

    The output verifies that the database instance is created:

    Example output

    1. database.postgresql.dev4devs.com/sampledatabase created
  2. After you have created the database instance, ensure that all the pods in the my-petclinic namespace are running:

    1. $ oc get pods -n my-petclinic

    The output, which takes a few minutes to display, verifies that the database is created and configured:

    Example output

    1. NAME READY STATUS RESTARTS AGE
    2. sampledatabase-cbc655488-74kss 0/1 Running 0 32s

After the database is configured, you can deploy the sample application and connect it to the database service.

Deploying the Spring PetClinic sample application

To deploy the Spring PetClinic sample application on an OKD cluster, you must use a deployment configuration and configure your local environment to be able to test the application.

Procedure

  1. Deploy the spring-petclinic application with the PostgresCluster custom resource (CR) by running the following command in shell:

    1. $ oc apply -n my-petclinic -f - << EOD
    2. ---
    3. apiVersion: apps/v1
    4. kind: Deployment
    5. metadata:
    6. name: spring-petclinic
    7. labels:
    8. app: spring-petclinic
    9. spec:
    10. replicas: 1
    11. selector:
    12. matchLabels:
    13. app: spring-petclinic
    14. template:
    15. metadata:
    16. labels:
    17. app: spring-petclinic
    18. spec:
    19. containers:
    20. - name: app
    21. image: quay.io/service-binding/spring-petclinic:latest
    22. imagePullPolicy: Always
    23. env:
    24. - name: SPRING_PROFILES_ACTIVE
    25. value: postgres
    26. - name: org.springframework.cloud.bindings.boot.enable
    27. value: "true"
    28. ports:
    29. - name: http
    30. containerPort: 8080
    31. ---
    32. apiVersion: v1
    33. kind: Service
    34. metadata:
    35. labels:
    36. app: spring-petclinic
    37. name: spring-petclinic
    38. spec:
    39. type: NodePort
    40. ports:
    41. - port: 80
    42. protocol: TCP
    43. targetPort: 8080
    44. selector:
    45. app: spring-petclinic
    46. EOD

    The output verifies that the Spring PetClinic sample application is created and deployed:

    Example output

    1. deployment.apps/spring-petclinic created
    2. service/spring-petclinic created

    If you are deploying the application using Container images in the Developer perspective of the web console, you must enter the following environment variables under the Deployment section of the Advanced options:

    • Name: SPRING_PROFILES_ACTIVE

    • Value: postgres

  2. Verify that the application is not yet connected to the database service by running the following command:

    1. $ oc get pods -n my-petclinic

    It takes take a few minutes until the CrashLoopBackOff status is displayed:

    Example output

    1. NAME READY STATUS RESTARTS AGE
    2. spring-petclinic-5b4c7999d4-wzdtz 0/1 CrashLoopBackOff 4 (13s ago) 2m25s

    At this stage, the pod fails to start. If you try to interact with the application, it returns errors.

You can now use the Service Binding Operator to connect the application to the database service.

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

  1. Create a ServiceBinding CR to project the binding data:

    1. $ oc apply -n my-petclinic -f - << EOD
    2. ---
    3. apiVersion: binding.operators.coreos.com/v1alpha1
    4. kind: ServiceBinding
    5. metadata:
    6. name: spring-petclinic-pgcluster
    7. spec:
    8. services: (1)
    9. - group: postgresql.dev4devs.com
    10. kind: Database (2)
    11. name: sampledatabase
    12. version: v1alpha1
    13. application: (3)
    14. name: spring-petclinic
    15. group: apps
    16. version: v1
    17. resource: deployments
    18. EOD
    1Specifies a list of service resources.
    2The CR of the database.
    3The 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

    1. servicebinding.binding.operators.coreos.com/spring-petclinic created
  2. Verify that the request for service binding is successful:

    1. $ oc get servicebindings -n my-petclinic

    Example output

    1. NAME READY REASON AGE
    2. spring-petclinic-postgresql True ApplicationsBound 47m

    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.

  3. Once this is created, you can go to the topology to see the visual connection.

    img power

    Figure 1. Connecting spring-petclinic to a sample database

  4. Set up the port forwarding from the application port to access the sample application from your local environment:

    1. $ oc port-forward --address 0.0.0.0 svc/spring-petclinic 8080:80 -n my-petclinic

    Example output

    1. Forwarding from 0.0.0.0:8080 -> 8080
    2. Handling connection for 8080
  5. Access 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.

Additional resources