2.8. 使用 devfile 创建应用程序

重要

使用带有 'odo' 的 devfile 创建应用程序只是一个技术预览功能。技术预览功能不被红帽产品服务等级协议 (SLA) 支持,且可能在功能方面有缺陷。红帽不推荐在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。

有关红帽技术预览功能支持范围的详情,请参阅 https://access.redhat.com/support/offerings/techpreview/

2.8.1. 关于 odo 中的 devfile

devfile 是一个可移植的文件,它描述了您的开发环境。使用 devfile,您可以定义一个可移植的开发环境而无需重新配置。

使用 devfile,您可以描述开发环境,如源代码、IDE 工具、应用程序运行时和预定义的命令。要了解更多关于 devfile 的信息,请参阅 devfile 文档

使用 odo,您可以从 devfiles 创建组件。当使用 devfile 创建组件时,odo 会将 devfile 转换为一个由 OpenShift Container Platform、Kubernetes 或 Docker 上运行的多个容器组成的工作区。odo 自动使用默认的 devfile registry,但用户可以添加自己的 registry。

2.8.2. 使用 devfile 创建 Java 应用程序

2.8.3. 先决条件

  • 已安装了 odo
  • 必须知道您的 ingress 域集群名称。如果不知道,请联络您的集群管理员。例如, apps-crc.testingRed Hat CodeReady Containers 的集群域名。
  • 已在 odo 中启用了实验模式。

    • 要在 odo 中启用实验模式,运行 odo preference set Experimental true 或使用环境变量 odo config set --env ODO_EXPERIMENTAL=true

2.8.3.1. 创建一个项目

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

流程

  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.8.3.2. 列出可用的 devfile 组件

使用 odo,可以显示集群中的所有可用组件。可用的组件取决于集群的配置。

流程

  1. 要列出集群中可用的 devfile 组件,请运行:

    $ odo catalog list components was

    输出列出了可用的 odo 组件:

    Odo OpenShift Components:
     NAME              PROJECT       TAGS                        SUPPORTED
     java              openshift     11,8,latest                 YES
     nodejs            openshift     10-SCL,8,8-RHOAR,latest     YES
     dotnet            openshift     2.1,2.2,3.0,latest          NO
     golang            openshift     1.11.5,latest               NO
     httpd             openshift     2.4,latest                  NO
     modern-webapp     openshift     10.x,latest                 NO
     nginx             openshift     1.10,1.12,latest            NO
     perl              openshift     5.24,5.26,latest            NO
     php               openshift     7.0,7.1,7.2,latest          NO
     python            openshift     2.7,3.6,latest              NO
     ruby              openshift     2.4,2.5,latest              NO
    
     Odo Devfile Components:
     NAME                 DESCRIPTION                           SUPPORTED
     maven                Upstream Maven and OpenJDK 11         YES
     nodejs               Stack with NodeJS 10                  YES
     openLiberty          Open Liberty microservice in Java     YES
     java-spring-boot     Spring Boot® using Java               YES

2.8.3.3. 使用 devfile 部署 Java 应用程序

在这一部分,您将了解如何使用 devfile 部署使用 Maven 和 Java 8 JDK 的 Java 示例项目。

流程

  1. 创建一个目录来存储组件的源代码:

     $ mkdir <directory-name>
  2. 创建名为 myspring 的 Spring Boot 组件类型的组件配置并下载其示例项目:

       $ odo create java-spring-boot myspring --downloadSource

    上面的命令会产生以下输出:

       Experimental mode is enabled, use at your own risk
    
       Validation
        ✓  Checking devfile compatibility [195728ns]
        ✓  Creating a devfile component from registry: DefaultDevfileRegistry [170275ns]
        ✓  Validating devfile component [281940ns]
    
        Please use `odo push` command to create the component with source deployed

    odo create 命令从记录的 devfile registry 中下载相关的 devfile.yaml 文件。

  3. 列出目录的内容以确认下载了 devfile 和示例 Java 应用程序:

      $ ls

    上面的命令会产生以下输出:

      README.md    devfile.yaml    pom.xml        src
  4. 创建一个 URL 以访问部署的组件:

     $ odo url create --host apps-crc.testing

    上面的命令会产生以下输出:

      ✓  URL myspring-8080.apps-crc.testing created for component: myspring
    
     To apply the URL configuration changes, please use odo push
    注意

    创建 URL 时必须使用集群主机名。

  5. 将组件推送到集群:

      $ odo push

    上面的命令会产生以下输出:

      Validation
       ✓  Validating the devfile [81808ns]
    
      Creating Kubernetes resources for component myspring
       ✓  Waiting for component to start [5s]
    
      Applying URL changes
       ✓  URL myspring-8080: http://myspring-8080.apps-crc.testing created
    
      Syncing to component myspring
       ✓  Checking files for pushing [2ms]
       ✓  Syncing files to the component [1s]
    
      Executing devfile commands for component myspring
       ✓  Executing devbuild command "/artifacts/bin/build-container-full.sh" [1m]
       ✓  Executing devrun command "/artifacts/bin/start-server.sh" [2s]
    
      Pushing devfile component myspring
       ✓  Changes successfully pushed to component
  6. 列出组件的 URL 以验证组件已被成功推送:

     $ odo url list

    上面的命令会产生以下输出:

     Found the following URLs for component myspring
     NAME              URL                                       PORT     SECURE
     myspring-8080     http://myspring-8080.apps-crc.testing     8080     false
  7. 使用生成的 URL 查看部署的应用程序。

      $ curl http://myspring-8080.apps-crc.testing