2.7. 创建带有数据库的应用程序

这个示例描述了如何部署和连接数据库到前端应用程序。

2.7.1. 先决条件

  • 已安装了 odo
  • 已安装oc 客户端。
  • 有一个正在运行的 OpenShift Container Platform 集群。开发人员可以使用 CodeReady Containers (CRC) 来快速部署一个本地的 OpenShift Container Platform 集群。
  • 服务目录被启用。

2.7.2. 创建一个项目

创建一个项目来在一个独立的空间内保存源代码、测试并对库进行管理。

流程

  1. 登陆到一个OpenShift Container Platform集群。

    $ odo login -u developer -p developer
  2. 创建一个项目:

    $ odo project create myproject
     ✓  Project 'myproject' is ready for use
     ✓  New project created and now using project : myproject

2.7.3. 部署前端组件

要创建并部署前端组件,请下载 Node.js 应用程序并使用 odo将源代码推送到集群中。

流程

  1. 下载前端应用程序示例:

    $ git clone https://github.com/openshift/nodejs-ex
  2. 将当前目录改为前端目录:

    $ cd <directory-name>
  3. 列出目录的内容,以查看前端是一个 Node.js 应用程序。

    $ ls
    assets  bin  index.html  kwww-frontend.iml  package.json  package-lock.json  playfield.png  README.md  server.js
    注意

    前端组件使用解释语言 (Node.js) 编写,不需要构建它。

  4. 创建名为 frontend的 Node.js 组件类型组件配置:

    $ odo create nodejs frontend
     ✓  Validating component [5ms]
    Please use `odo push` command to create the component with source deployed
  5. 创建一个 URL 来访问前台接口。

    $ odo url create myurl
     ✓  URL myurl created for component: nodejs-nodejs-ex-pmdp
  6. 将组件推送到 OpenShift Container Platform 集群。

    $ odo push
    Validation
     ✓  Checking component [7ms]
    
     Configuration changes
     ✓  Initializing component
     ✓  Creating component [134ms]
    
     Applying URL changes
     ✓  URL myurl: http://myurl-app-myproject.192.168.42.79.nip.io created
    
     Pushing to component nodejs-nodejs-ex-mhbb of type local
     ✓  Checking files for pushing [657850ns]
     ✓  Waiting for component to start [6s]
     ✓  Syncing files to the component [408ms]
     ✓  Building component [7s]
     ✓  Changes successfully pushed to component

2.7.4. 以互动模式部署数据库

odo 提供了一个简化部署的命令行互动模式。

流程

  • 运行互动模式并回答提示:

    $ odo service create
    ? Which kind of service do you wish to create database
    ? Which database service class should we use mongodb-persistent
    ? Enter a value for string property DATABASE_SERVICE_NAME (Database Service Name): mongodb
    ? Enter a value for string property MEMORY_LIMIT (Memory Limit): 512Mi
    ? Enter a value for string property MONGODB_DATABASE (MongoDB Database Name): sampledb
    ? Enter a value for string property MONGODB_VERSION (Version of MongoDB Image): 3.2
    ? Enter a value for string property VOLUME_CAPACITY (Volume Capacity): 1Gi
    ? Provide values for non-required properties No
    ? How should we name your service  mongodb-persistent
    ? Output the non-interactive version of the selected options No
    ? Wait for the service to be ready No
     ✓  Creating service [32ms]
     ✓  Service 'mongodb-persistent' was created
    Progress of the provisioning will not be reported and might take a long time.
    You can see the current status by executing 'odo service list'
注意

您的密码或用户名将作为环境变量传递给前端的应用程序。

2.7.5. 手动部署数据库

  1. 列出可用服务:

    $ odo catalog list services
    NAME                         PLANS
    django-psql-persistent       default
    jenkins-ephemeral            default
    jenkins-pipeline-example     default
    mariadb-persistent           default
    mongodb-persistent           default
    mysql-persistent             default
    nodejs-mongo-persistent      default
    postgresql-persistent        default
    rails-pgsql-persistent       default
  2. 选择 mongodb-persistent 服务类型,并查看所需参数:

    $ odo catalog describe service mongodb-persistent
      ***********************        | *****************************************************
      Name                           | default
      -----------------              | -----------------
      Display Name                   |
      -----------------              | -----------------
      Short Description              | Default plan
      -----------------              | -----------------
      Required Params without a      |
      default value                  |
      -----------------              | -----------------
      Required Params with a default | DATABASE_SERVICE_NAME
      value                          | (default: 'mongodb'),
                                     | MEMORY_LIMIT (default:
                                     | '512Mi'), MONGODB_VERSION
                                     | (default: '3.2'),
                                     | MONGODB_DATABASE (default:
                                     | 'sampledb'), VOLUME_CAPACITY
                                     | (default: '1Gi')
      -----------------              | -----------------
      Optional Params                | MONGODB_ADMIN_PASSWORD,
                                     | NAMESPACE, MONGODB_PASSWORD,
                                     | MONGODB_USER
  3. 指定所需参数,并等待数据库部署:

    $ odo service create mongodb-persistent --plan default --wait -p DATABASE_SERVICE_NAME=mongodb -p MEMORY_LIMIT=512Mi -p MONGODB_DATABASE=sampledb -p VOLUME_CAPACITY=1Gi

2.7.6. 将数据库连接到前端应用程序

  1. 将数据库连接到前端服务中:

    $ odo link mongodb-persistent
     ✓  Service mongodb-persistent has been successfully linked from the component nodejs-nodejs-ex-mhbb
    
    Following environment variables were added to nodejs-nodejs-ex-mhbb component:
    - database_name
    - password
    - uri
    - username
    - admin_password
  2. 查看 Pod 中的应用程序和数据库的环境变量:

    $ oc get pods
    NAME                                READY     STATUS    RESTARTS   AGE
    mongodb-1-gsznc                     1/1       Running   0          28m
    nodejs-nodejs-ex-mhbb-app-4-vkn9l   1/1       Running   0          1m
    
    $ oc rsh nodejs-nodejs-ex-mhbb-app-4-vkn9l
    sh-4.3$ env
    uri=mongodb://172.30.126.3:27017
    password=dHIOpYneSkX3rTLn
    database_name=sampledb
    username=user43U
    admin_password=NCn41tqmx7RIqmfv
    sh-4.3$
  3. 在浏览器中打开 URL ,并在右下角记录数据库配置:

    $ odo url list
    Request information
    Page view count: 24
    
    DB Connection Info:
    Type:	MongoDB
    URL:	mongodb://172.30.126.3:27017/sampledb

2.7.7. 删除应用程序

重要

删除应用程序将删除与应用程序关联的所有组件。

流程

  1. 列出当前项目中的应用程序:

    $ odo app list
        The project '<project_name>' has the following applications:
        NAME
        app
  2. 列出与应用程序关联的组件。这些组件将随应用程序一起删除:

    $ odo component list
        APP     NAME                      TYPE       SOURCE        STATE
        app     nodejs-nodejs-ex-elyf     nodejs     file://./     Pushed
  3. 删除应用程序:

    $ odo app delete <application_name>
        ? Are you sure you want to delete the application: <application_name> from project: <project_name>
  4. 使用 Y 确认删除。您可以使用 -f 标记来阻止确认提示。