2.6. 使用 odo 创建多组件应用程序

通过使用 odo,可以创建多组件应用程序,修改该应用程序,并以方便和自动的方式链接其组件。

这个例子描述了如何部署多组件应用程序 - 一个射击游戏。应用程序由一个前端 Node.js 组件和一个后端 Java 组件组成。

2.6.1. 先决条件

  • 已安装了 odo
  • 有一个正在运行的 OpenShift Container Platform 集群。开发人员可以使用 CodeReady Containers (CRC) 来快速部署一个本地的 OpenShift Container Platform 集群。
  • 已安装 Maven。

2.6.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.6.3. 部署后端组件

要创建一个 Java 组件,请导入 Java 构建器镜像(builder image),下载 Java 应用程序并使用 odo将源代码推送到您的集群中。

流程

  1. openjdk18 导入集群:

    $ oc import-image openjdk18 \
    --from=registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift --confirm
  2. 将镜像标记为 builder,使它可以被 odo 访问:

    $ oc annotate istag/openjdk18:latest tags=builder
  3. 运行 odo catalog list components 查看创建的镜像:

    $ odo catalog list components
    Odo Supported OpenShift Components:
    NAME          PROJECT       TAGS
    nodejs        openshift     10,8,8-RHOAR,latest
    openjdk18     myproject     latest
  4. 为您的组件创建一个新目录:

    $ mkdir my_components $$ cd my_components
  5. 下载后端应用程序示例:

    $ git clone https://github.com/openshift-evangelists/Wild-West-Backend backend
  6. 将目录改为后端源目录,检查目录中有正确的文件:

    $ cd backend
    $ ls
    debug.sh  pom.xml  src
  7. 使用 Maven 构建后端源文件以创建一个 JAR 文件:

    $ mvn package
    ...
    [INFO] --------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] --------------------------------------
    [INFO] Total time: 2.635 s
    [INFO] Finished at: 2019-09-30T16:11:11-04:00
    [INFO] Final Memory: 30M/91M
    [INFO] --------------------------------------
  8. 创建名为 backend的 Java 组件类型组件配置:

    $ odo create openjdk18 backend --binary target/wildwest-1.0.jar
     ✓  Validating component [1ms]
     Please use `odo push` command to create the component with source deployed

    现在,配置文件 config.yaml 位于后端组件的本地目录中,其中包含用于部署的组件信息。

  9. 使用以下方法检查 config.yaml 文件中后端组件的配置设置:

    $ odo config view
    COMPONENT SETTINGS
    ------------------------------------------------
    PARAMETER         CURRENT_VALUE
    Type              openjdk18
    Application       app
    Project           myproject
    SourceType        binary
    Ref
    SourceLocation    target/wildwest-1.0.jar
    Ports             8080/TCP,8443/TCP,8778/TCP
    Name              backend
    MinMemory
    MaxMemory
    DebugPort
    Ignore
    MinCPU
    MaxCPU
  10. 将组件推送到 OpenShift Container Platform 集群。

    $ odo push
    Validation
     ✓  Checking component [6ms]
    
    Configuration changes
     ✓  Initializing component
     ✓  Creating component [124ms]
    
    Pushing to component backend of type binary
     ✓  Checking files for pushing [1ms]
     ✓  Waiting for component to start [48s]
     ✓  Syncing files to the component [811ms]
     ✓  Building component [3s]

    使用 odo push,OpenShift Container Platform 创建一个容器来托管后端组件,将容器部署到运行在 OpenShift Container Platform 集群上的 Pod 中,并启动 backend 组件。

  11. 验证:

    • odo 中操作的状态:

      odo log -f
      2019-09-30 20:14:19.738  INFO 444 --- [           main] c.o.wildwest.WildWestApplication         : Starting WildWestApplication v1.0 onbackend-app-1-9tnhc with PID 444 (/deployments/wildwest-1.0.jar started by jboss in /deployments)
    • 后端组件的状态:

      $ odo list
      APP     NAME        TYPE          SOURCE                             STATE
      app     backend     openjdk18     file://target/wildwest-1.0.jar     Pushed

2.6.4. 部署前端组件

要创建并部署前端组件,请下载 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. 将组件推送到正在运行的容器中。

    $ odo push
    Validation
     ✓  Checking component [8ms]
    
    Configuration changes
     ✓  Initializing component
     ✓  Creating component [83ms]
    
    Pushing to component frontend of type local
     ✓  Checking files for pushing [2ms]
     ✓  Waiting for component to start [45s]
     ✓  Syncing files to the component [3s]
     ✓  Building component [18s]
     ✓  Changes successfully pushed to component

2.6.5. 连接两个组件

集群中运行的组件需要连接才能进行交互。OpenShift Container Platform 提供了链接机制以发布一个程序到其客户端的通信绑定。

流程

  1. 列出在集群中运行的所有组件:

    $ odo list
    APP     NAME         TYPE          SOURCE                             STATE
    app     backend      openjdk18     file://target/wildwest-1.0.jar     Pushed
    app     frontend     nodejs        file://./                          Pushed
  2. 将当前的前端组件链接到后端:

    $ odo link backend --port 8080
     ✓  Component backend has been successfully linked from the component frontend
    
    Following environment variables were added to frontend component:
    - COMPONENT_BACKEND_HOST
    - COMPONENT_BACKEND_PORT

    后端组件的配置信息被添加到前端组件中,前端组件重新启动。

2.6.6. 公开组件

流程

  1. 为应用程序创建一个外部 URL:

    $ cd frontend
    $ odo url create frontend --port 8080
     ✓  URL frontend created for component: frontend
    
    To create URL on the OpenShift  cluster, use `odo push`
  2. 应用更改:

    $ odo push
    Validation
     ✓  Checking component [21ms]
    
    Configuration changes
     ✓  Retrieving component data [35ms]
     ✓  Applying configuration [29ms]
    
    Applying URL changes
     ✓  URL frontend: http://frontend-app-myproject.192.168.42.79.nip.io created
    
    Pushing to component frontend of type local
     ✓  Checking file changes for pushing [1ms]
     ✓  No file changes detected, skipping build. Use the '-f' flag to force the build.
  3. 在一个浏览器中使用 URL 来查看应用程序。
注意

如果一个应用程序需要一个有效的服务账户( Service Account)来访问 OpenShift Container Platform 命名空间并删除活跃的 pod 时,后端组件的 odo log 中可能会出现以下错误:

Message: Forbidden!Configured service account doesn’t have access.Service account may have been revoked

要解决这个错误,请为 Service Account 角色添加权限:

$ oc policy add-role-to-group view system:serviceaccounts -n <project>
$ oc policy add-role-to-group edit system:serviceaccounts -n <project>

不要在生产环境集群中使用它。

2.6.7. 修改正在运行的应用程序

流程

  1. 将本地目录改为前端目录:

    $ cd ~/frontend
  2. 使用以下方法监控文件系统中的更改:

    $ odo watch
  3. 编辑 index.html 文件,为游戏更改显示的名称。

    注意

    在 odo 可以识别更改前可能会有一些延迟。

    odo 将更改推送到前端组件,并将其状态输出到终端:

    File /root/frontend/index.html changed
    File  changed
    Pushing files...
     ✓  Waiting for component to start
     ✓  Copying files to component
     ✓  Building component
  4. 在浏览器中刷新应用程序页面。现在会显示新名称。

2.6.8. 删除应用程序

重要

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

流程

  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 标记来阻止确认提示。