5.4. 服务绑定入门

Service Binding Operator 管理工作负载和后备服务的数据平面。本指南提供了一些示例,可帮助您创建数据库实例、部署应用程序,以及使用 Service Binding Operator 在应用程序和数据库服务间创建绑定连接。

先决条件

  • 可以使用具有 cluster-admin 权限的账户访问 OpenShift Container Platform 集群。
  • 已安装 oc CLI。
  • 已从 OperatorHub 安装了 Service Binding Operator。
  • 已使用 v5 Update 频道从 OperatorHub 安装了 Crunchy Postgres for Kubernetes Operator。安装的 Operator 在适当的命名空间中可用,如 my-petclinic 命名空间。

    注意

    您可以使用 oc create namespace my-petclinic 命令创建命名空间。

5.4.1. 创建 PostgreSQL 数据库实例

要创建 PostgreSQL 数据库实例,您必须创建一个 PostgresCluster 自定义资源 (CR) 并配置数据库。

流程

  1. 在 shell 中运行以下命令来在 my-petclinic 命名空间中创建 PostgresCluster CR:

    $ oc apply -n my-petclinic -f - << EOD
    ---
    apiVersion: postgres-operator.crunchydata.com/v1beta1
    kind: PostgresCluster
    metadata:
      name: hippo
    spec:
      image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres-ha:centos8-13.4-0
      postgresVersion: 13
      instances:
        - name: instance1
          dataVolumeClaimSpec:
            accessModes:
            - "ReadWriteOnce"
            resources:
              requests:
                storage: 1Gi
      backups:
        pgbackrest:
          image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.33-2
          repos:
          - name: repo1
            volume:
              volumeClaimSpec:
                accessModes:
                - "ReadWriteOnce"
                resources:
                  requests:
                    storage: 1Gi
          - name: repo2
            volume:
              volumeClaimSpec:
                accessModes:
                - "ReadWriteOnce"
                resources:
                  requests:
                    storage: 1Gi
      proxy:
        pgBouncer:
          image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbouncer:centos8-1.15-2
    EOD

    PostgresCluster CR 中的注解启用服务绑定连接并触发 Operator 协调。

    输出会验证数据库实例是否已创建:

    输出示例

    postgrescluster.postgres-operator.crunchydata.com/hippo created

  2. 创建数据库实例后,确保 my-petclinic 命名空间中的所有 pod 都在运行:

    $ oc get pods -n my-petclinic

    输出(需要几分钟)验证是否创建并配置了数据库:

    输出示例

    NAME                               READY   STATUS    RESTARTS   AGE
    hippo-backup-nqjg-2rq94            1/1     Running   0          35s
    hippo-instance1-nw92-0             3/3     Running   0          112s
    hippo-pgbouncer-57b98f4476-znsk5   2/2     Running   0          112s
    hippo-repo-host-0                  1/1     Running   0          112s

    配置了数据库后,您可以部署示例应用程序并将其连接到数据库服务。

5.4.2. 部署 Spring PetClinic 示例应用程序

要在 OpenShift Container Platform 集群上部署 Spring PetClinic 示例应用程序,您必须使用部署配置并配置本地环境才能测试应用程序。

流程

  1. 在 shell 中运行以下命令,使用 PostgresCluster 自定义资源(CR)部署 spring-petclinic 应用程序:

    $ oc apply -n my-petclinic -f - << EOD
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: spring-petclinic
      labels:
        app: spring-petclinic
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: spring-petclinic
      template:
        metadata:
          labels:
            app: spring-petclinic
        spec:
          containers:
            - name: app
              image: quay.io/service-binding/spring-petclinic:latest
              imagePullPolicy: Always
              env:
              - name: SPRING_PROFILES_ACTIVE
                value: postgres
              ports:
              - name: http
                containerPort: 8080
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: spring-petclinic
      name: spring-petclinic
    spec:
      type: NodePort
      ports:
        - port: 80
          protocol: TCP
          targetPort: 8080
      selector:
        app: spring-petclinic
    EOD

    输出会验证 Spring PetClinic 示例应用是否已创建并部署:

    输出示例

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

    注意

    如果要在 web 控制台的 Developer 视角中使用 容器镜像 部署应用程序,则必须在高级选项Deployment 部分输入以下环境变量:

    • 名称:SPRING_PROFILES_ACTIVE
    • 值: postgres
  2. 运行以下命令,验证应用程序是否还没有连接到数据库服务:

    $ oc get pods -n my-petclinic

    显示 CrashLoopBackOff 状态需要几分钟时间 :

    输出示例

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

    在这个阶段,pod 无法启动。如果您尝试与应用交互,它会返回错误。

现在,您可以使用 Service Binding Operator 将应用程序连接到数据库服务。

5.4.3. 将 Spring PetClinic 示例应用程序连接到 PostgreSQL 数据库服务

要将示例应用程序连接到数据库服务,您必须创建一个 ServiceBinding 自定义资源 (CR),该资源会触发 Service Binding Operator 将绑定数据项目到应用程序中。

流程

  1. 创建 ServiceBinding CR 以项目绑定数据:

    $ 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
    指定服务资源列表。
    2
    数据库的 CR。
    3
    示例应用程序,指向带有嵌入式 PodSpec 的 Deployment 或任何其他类似资源。

    输出会验证是否已创建 ServiceBinding CR 以将绑定数据项目到示例应用程序中。

    输出示例

    servicebinding.binding.operators.coreos.com/spring-petclinic created

  2. 验证服务绑定的请求是否成功:

    $ oc get servicebindings -n my-petclinic

    输出示例

    NAME                         READY   REASON              AGE
    spring-petclinic-pgcluster   True    ApplicationsBound   7s

    默认情况下,来自数据库服务的绑定数据的值作为运行示例应用程序的工作负载容器中的文件进行投射。例如,Secret 资源中的所有值都投射到 bindings/spring-petclinic-pgcluster 目录中。

    注意

    另外,您还可以通过打印出目录内容来验证应用程序中的文件是否包含投射绑定数据:

    $ 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

    输出示例:使用来自 secret 资源的所有值

    /bindings/spring-petclinic-pgcluster/username: hippo
    /bindings/spring-petclinic-pgcluster/password: KXKF{nAI,I-J6zLt:W+FKnze
    /bindings/spring-petclinic-pgcluster/host: hippo-primary.my-petclinic.svc
    /bindings/spring-petclinic-pgcluster/port: 5432
    /bindings/spring-petclinic-pgcluster/type: postgresql

  3. 设置应用程序端口的端口转发,以便从本地环境访问示例应用程序:

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

    输出示例

    Forwarding from 0.0.0.0:8080 -> 8080
    Handling connection for 8080

  4. 访问 http://localhost:8080/petclinic

    现在,您可以在 localhost:8080 远程访问 Spring PetClinic 示例应用程序,并查看应用程序现在连接到数据库服务。

5.4.4. 其它资源