Red Hat Training

A Red Hat training course is available for Red Hat JBoss Web Server

Red Hat JBoss Web Server for OpenShift

Red Hat JBoss Web Server 5.0

安装并使用 Red Hat JBoss Web Server for OpenShift

Red Hat Customer Content Services

摘要

使用 Red Hat JBoss Web Server for OpenShift 指南

第 1 章 简介

1.1. 什么是 Red Hat JBoss Web Server for OpenShift?

Red Hat JBoss Web Server (JWS) 5.0 的 Apache Tomcat 7 和 Apache Tomcat 8 组件作为为 OpenShift 设计的容器化镜像提供。开发人员可以使用这些镜像来构建、扩展和测试 Java Web 应用程序以便在混合云环境中部署。

第 2 章 开始前

2.1. Red Hat JBoss Web Server 和 JWS for OpenShift 的不同

JWS for OpenShift 镜像与常规发行的 JWS 的不同包括:

  • JWS for OpenShift 镜像内的 JWS_HOME/tomcat<version>/ 的位置是:/opt/webserver/
  • JWS for OpenShift 镜像 JWS 不包含 Apache HTTP 服务器。所有负载平衡都由 OpenShift 路由器处理,而不是 Apache HTTP 服务器 mod_cluster 或 mod_jk 连接器。

有关 JWS 功能的文档,请参阅 Red Hat JBoss Web Server 文档

2.2. 版本兼容性和支持

如需了解有关 OpenShift 镜像版本兼容性的详细信息,请参阅 OpenShift Container Platform Tested Integrations 页面中的 xPaaS 表。

重要

用于 OpenShift 镜像和应用程序模板的 JWS 的 5.0 版本应该用于部署新应用。

OpenShift 镜像和应用程序模板的 JWS 的 3.1 版本已弃用,不再接收更新。

第 3 章 开始使用

3.1. 初始设置

本指南中的说明遵循 OpenShift Primer,假设受支持的 OpenShift 配置或非生产环境的 OpenShift 实例,如 OpenShift Primer 所述。

JWS for OpenShift 在安装 OpenShift 期间自动创建,以及其他默认镜像流和模板。

注意

OpenShift 应用程序模板的 JWS 为 Tomcat 7 和 Tomcat 8 发布。

3.2. 使用 JWS for OpenShift Source-to-Image (S2I) 过程

要运行并配置 JWS for OpenShift 镜像,使用带有应用程序参数和环境变量的 OpenShift S2I 流程。

用于 JWS for OpenShift 的 S2I 流程可以正常工作:

  • 如果 configuration/ 源目录中有一个 Maven settings.xml 文件,则会将其移到新镜像的 $HOME/.m2/ 中。

    有关 Maven 和 Maven settings.xml 文件的更多信息,请参阅 Apache Maven 项目网站

  • 如果源存储库中有一个 pom.xml 文件,则使用 $MAVEN_ARGS 环境变量的内容触发 Maven 构建。

    默认情况下,package 目标与 openshift 配置集一起使用,包括跳过测试的参数 (-DskipTests),并启用 Red Hat GA 存储库 (-Dcom.redhat.xpaas.repo.redhatga)。

  • 成功 Maven 构建的结果被复制到 /opt/webserver/webapps/ 中。这包括 $ARTIFACT_DIR 环境变量指定的源目录中的所有 WAR 文件。$ARTIFACT_DIR 的默认值为 target/ 目录。

    使用 MAVEN_ARGS_APPEND 环境变量来修改 Maven 参数。

  • deployments/ 源目录中的所有 WAR 文件都复制到 /opt/webserver/webapps/ 中。
  • configuration/ 源目录中的所有文件都复制到 /opt/webserver/conf/ (不包括 Maven settings.xml 文件)。
  • lib/ 源目录中的所有文件都复制到 /opt/webserver/lib/ 中。

    注意

    如果要使用自定义 Tomcat 配置文件,文件名应与普通的 Tomcat 安装相同。例如:context.xmlserver.xml

有关配置 S2I 进程以使用自定义 Maven 工件存储库镜像的指南,请参阅 Artifact Repository Mirrors 部分。

3.2.1. 使用现有的 maven 二进制文件为 OpenShift 应用创建一个 JWS

使用 oc start-build 命令在 OpenShift 上部署现有的应用程序。

先决条件:要在 JWS for OpenShift 上部署的应用程序的 .war.ear.jar

  1. 在本地文件系统上准备目录结构。

    创建包含未包含在二进制文件中的任何内容的源目录(如果需要,请参阅 使用 JWS for OpenShift Source-to-Image (S2I)进程),然后创建一个子目录 deployments/

    $ mkdir -p <build_dir>/deployments
  2. 将二进制文件(.war,.ear,.jar)复制到 deployments/:

    $ cp /path/to/binary/<filenames_with_extensions> <build_dir>/deployments/
    注意

    源目录的 deployments/ 子目录中的应用程序存档被复制到 OpenShift 上构建的镜像的 $JWS_HOME/webapps/ 目录中。要部署应用程序,包含 Web 应用程序数据的目录层次结构必须被正确构建(请参阅 第 3.2 节 “使用 JWS for OpenShift Source-to-Image (S2I) 过程”)。

  3. 登录到 OpenShift 实例:

    $ oc login <url>
  4. 根据需要创建新项目:

    $ oc new-project <project-name>
  5. 使用 oc get 识别用于应用程序的 OpenShift 镜像流的 JWS 是 -n openshift

    $ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' '
    
    jboss-webserver30-tomcat7-openshift
    jboss-webserver30-tomcat8-openshift
    jboss-webserver31-tomcat7-openshift
    jboss-webserver31-tomcat8-openshift
    注意

    选项 -n openshift 指定要使用的项目。oc get 是 -n openshiftopenshift 项目检索(get)镜像流资源()。

  6. 创建新构建配置,指定镜像流和应用程序名称:

    $ oc new-build --binary=true \
     --image-stream=jboss-webserver31-tomcat8-openshift \
     --name=<my-jws-on-openshift-app>
  7. 指示 OpenShift 使用之前创建的 源目录进行 OpenShift 镜像构建的二进制输入:

    $ oc start-build <my-jws-on-openshift-app> --from-dir=./<build_dir> --follow
  8. 根据镜像创建新 OpenShift 应用程序:

    $ oc new-app <my-jws-on-openshift-app>
  9. 公开该服务以使应用程序可以被用户访问:

    # to check the name of the service to expose
    $ oc get svc -o name
    
    service/<my-jws-on-openshift-app>
    
    # to expose the service
    $ oc expose svc/my-jws-on-openshift-app
    
    route "my-jws-on-openshift-app" exposed
  10. 检索公开路由的地址:

    oc get routes --no-headers -o custom-columns='host:spec.host' my-jws-on-openshift-app
  11. 要在浏览器中访问应用程序: http:// <address_of_exposed_route> / & lt ;my-war-ear-jar-filename-without-extension>

3.2.2. 示例:使用现有 maven 二进制文件为 OpenShift 应用程序创建 JWS

以下示例使用 第 3.2.1 节 “使用现有的 maven 二进制文件为 OpenShift 应用创建一个 JWS” 中的步骤使用 tomcat-websocket-chat Quickstart。

3.2.2.1. 先决条件

  1. 获取 WAR 应用程序存档或在本地构建应用程序。

    • 克隆源代码:

      $ git clone https://github.com/jboss-openshift/openshift-quickstarts.git
    • 配置 Red Hat JBoss Middleware Maven 存储库
    • 构建应用程序:

      $ cd openshift-quickstarts/tomcat-websocket-chat/
      $ mvn clean package
      
      [INFO] Scanning for projects...
      [INFO]
      [INFO] ------------------------------------------------------------------------
      [INFO] Building Tomcat websocket example 1.2.0.Final
      [INFO] ------------------------------------------------------------------------
      ...
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD SUCCESS
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time: 01:28 min
      [INFO] Finished at: 2018-01-16T15:59:16+10:00
      [INFO] Final Memory: 19M/271M
      [INFO] ------------------------------------------------------------------------
  1. 在本地文件系统上准备目录结构。

    在本地文件系统和 deployments/ 子目录上为二进制构建创建源目录。将 WAR 归档复制到 deployments/ 中:

    [tomcat-websocket-chat]$ ls
    
    pom.xml  README.md  src/  target/
    $ mkdir -p ocp/deployments
    $ cp target/websocket-chat.war ocp/deployments/

3.2.2.2. 在 OpenShift 中设置示例应用程序

  1. 登录到 OpenShift 实例:

    $ oc login <url>
  2. 根据需要创建新项目:

    $ oc new-project jws-bin-demo
  3. 使用 oc get 识别用于应用程序的 OpenShift 镜像流的 JWS 是 -n openshift

    $ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' '
    
    jboss-webserver30-tomcat7-openshift
    jboss-webserver30-tomcat8-openshift
    jboss-webserver31-tomcat7-openshift
    jboss-webserver31-tomcat8-openshift
  4. 创建新构建配置,指定镜像流和应用程序名称:

    $ oc new-build --binary=true \
     --image-stream=jboss-webserver31-tomcat8-openshift \
     --name=jws-wsch-app
    
    --> Found image 8c3b85b (4 weeks old) in image stream "openshift/jboss-webserver31-tomcat8-openshift" under tag "latest" for "jboss-webserver31-tomcat8-openshift"
    
        JBoss Web Server 3.1
        --------------------
        Platform for building and running web applications on JBoss Web Server 3.1 - Tomcat v8
    
        Tags: builder, java, tomcat8
    
        * A source build using binary input will be created
          * The resulting image will be pushed to image stream "jws-wsch-app:latest"
          * A binary build was created, use 'start-build --from-dir' to trigger a new build
    
    --> Creating resources with label build=jws-wsch-app ...
        imagestream "jws-wsch-app" created
        buildconfig "jws-wsch-app" created
    --> Success
  5. 启动二进制构建。指示 OpenShift 将源目录用于 OpenShift 镜像构建的二进制输入:

    $ oc start-build jws-wsch-app --from-dir=./ocp --follow
    
    Uploading directory "ocp" as binary input for the build ...
    build "jws-wsch-app-1" started
    Receiving source from STDIN as archive ...
    
    Copying all deployments war artifacts from /home/jboss/source/deployments directory into /opt/webserver/webapps for later deployment...
    '/home/jboss/source/deployments/websocket-chat.war' -> '/opt/webserver/webapps/websocket-chat.war'
    
    
    Pushing image 172.30.202.111:5000/jws-bin-demo/jws-wsch-app:latest ...
    Pushed 0/7 layers, 7% complete
    Pushed 1/7 layers, 14% complete
    Pushed 2/7 layers, 29% complete
    Pushed 3/7 layers, 49% complete
    Pushed 4/7 layers, 62% complete
    Pushed 5/7 layers, 92% complete
    Pushed 6/7 layers, 100% complete
    Pushed 7/7 layers, 100% complete
    Push successful
  6. 根据镜像创建新 OpenShift 应用程序:

    $ oc new-app jws-wsch-app
    
    --> Found image e5f3a6b (About a minute old) in image stream "jws-bin-demo/jws-wsch-app" under tag "latest" for "jws-wsch-app"
    
        JBoss Web Server 3.1
        --------------------
        Platform for building and running web applications on JBoss Web Server 3.1 - Tomcat v8
    
        Tags: builder, java, tomcat8
    
        * This image will be deployed in deployment config "jws-wsch-app"
        * Ports 8080/tcp, 8443/tcp, 8778/tcp will be load balanced by service "jws-wsch-app"
          * Other containers can access this service through the hostname "jws-wsch-app"
    
    --> Creating resources ...
        deploymentconfig "jws-wsch-app" created
        service "jws-wsch-app" created
    --> Success
        Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
         'oc expose svc/jws-wsch-app'
        Run 'oc status' to view your app.
  7. 公开该服务以使应用程序可以被用户访问:

    # to check the name of the service to expose
    $ oc get svc -o name
    
    service/jws-wsch-app
    
    # to expose the service
    $ oc expose svc/jws-wsch-app
    
    route "jws-wsch-app" exposed
  8. 检索公开路由的地址:

    oc get routes --no-headers -o custom-columns='host:spec.host' jws-wsch-app
  9. 在浏览器中访问应用程序: http:// <address_of_exposed_route>/websocket-chat

3.2.3. 从源代码为 OpenShift 应用创建一个 JWS

有关从源代码创建新 OpenShift 应用程序的详细信息,请参阅 OpenShift.com - 从源代码创建应用程序

注意

在继续操作前,请确保应用程序的数据被正确结构化(请参阅 第 3.2 节 “使用 JWS for OpenShift Source-to-Image (S2I) 过程”)。

  1. 登录到 OpenShift 实例:

    $ oc login <url>
  2. 根据需要创建新项目:

    $ oc new-project <project-name>
  3. 使用 oc get 识别用于应用程序的 OpenShift 镜像流的 JWS 是 -n openshift

    $ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' '
    
    jboss-webserver30-tomcat7-openshift
    jboss-webserver30-tomcat8-openshift
    jboss-webserver31-tomcat7-openshift
    jboss-webserver31-tomcat8-openshift
  4. 使用 Red Hat JBoss Web Server for OpenShift 镜像从源代码创建新的 OpenShift 应用程序,使用 --image-stream 选项:

    $ oc new-app \
     <source_code_location> \
     --image-stream=jboss-webserver31-tomcat8-openshift \
     --name=<openshift_application_name>

    例如:

    $ oc new-app \
     https://github.com/jboss-openshift/openshift-quickstarts.git#master \
     --image-stream=jboss-webserver31-tomcat8-openshift \
     --context-dir='tomcat-websocket-chat' \
     --name=jws-wsch-app

    源代码添加到镜像中,并编译源代码。也创建了构建配置和服务。

  5. 公开应用程序:

    # to check the name of the service to expose
    $ oc get svc -o name
    
    service/<openshift_application_name>
    
    # to expose the service
    $ oc expose svc/<openshift_application_name>
    
    route "<openshift_application_name>" exposed
  6. 检索公开路由的地址:

    oc get routes --no-headers -o custom-columns='host:spec.host' <openshift_application_name>
  7. 要在浏览器中访问应用程序: http:// <address_of_exposed_route> / &lt;java_application_name>

第 4 章 参考

4.1. Source-to-Image(S2I)

Red Hat JBoss Web Server for OpenShift 镜像包括 S2I 脚本和 Maven。

4.1.1. 将 maven 工件存储库镜像与 JWS 用于 OpenShift

Maven 存储库包含构建工件和依赖项,如项目 jar、库 jar、插件或任何其他项目特定工件。它还定义了在执行 S2I 构建时从中下载工件的位置。除了使用 Maven Central 存储库 外,一些组织还会部署本地自定义存储库(mirror)。

使用本地镜像的好处是:

  • 同步镜像的可用性,这是更接近且更快地进行的。
  • 更好地控制存储库内容。
  • 有可能在不同团队(开发者、CI)之间共享工件,而无需依赖公共服务器和存储库。
  • 改进了构建时间。

Maven 存储库管理器 可以充当镜像的本地缓存。假设存储库管理器已在 http://10.0.0.1:8080/repository/internal/ 外部部署并可访问,S2I 构建可以使用此存储库。要使用内部 Maven 存储库,请将 MAVEN_MIRROR_URL 环境变量添加到应用的构建配置中。

对于新的构建配置,在 oc new-appoc new-build 中使用 --build-env 选项:

$ oc new-app \
 https://github.com/jboss-openshift/openshift-quickstarts.git#master \
 --image-stream=jboss-webserver31-tomcat8-openshift \
 --context-dir='tomcat-websocket-chat' \
 --build-env MAVEN_MIRROR_URL=http://10.0.0.1:8080/repository/internal/ \
 --name=jws-wsch-app

对于现有的构建配置:

  1. 识别需要 MAVEN_MIRROR_URL 变量的构建配置:

    $ oc get bc -o name
    
    buildconfig/jws
  2. MAVEN_MIRROR_URL 环境变量添加到 buildconfig/jws

    $ oc env bc/jws MAVEN_MIRROR_URL="http://10.0.0.1:8080/repository/internal/"
    
    buildconfig "jws" updated
  3. 验证构建配置是否已更新:

    $ oc env bc/jws --list
    
    # buildconfigs jws
    MAVEN_MIRROR_URL=http://10.0.0.1:8080/repository/internal/
  4. 使用 oc start-build调度应用程序的新构建
注意

在应用构建期间,Maven 依赖项从存储库管理器下载,而不是从默认的公共存储库下载。构建完成后,镜像包含构建期间检索和使用的所有依赖项。

4.1.2. Red Hat JBoss Web Server for OpenShift 镜像中包含的脚本

run
运行 Catalina (Tomcat)
assemble
使用 Maven 构建源,创建软件包(.war),并将它移到 $JWS_HOME/webapps 目录。

4.1.3. 用于 OpenShift 兼容环境变量的 JWS

可通过将环境变量包含 Source-to-Image build 命令(请参阅 第 4.1.1 节 “将 maven 工件存储库镜像与 JWS 用于 OpenShift”)来修改构建配置。Red Hat JBoss Web Server for OpenShift 镜像的有效环境变量是:

变量名称

描述

值示例

:leveloffset: +3

ARTIFACT_DIR

.war.ear、和 .jar 文件将复制到 deployments 目录中。

target

HTTP_PROXY_HOST

用于 Maven 的 HTTP 代理的主机名或 IP 地址。

192.168.1.1

HTTP_PROXY_PORT

用于 Maven 的 HTTP 代理的 TCP 端口。

8080

HTTP_PROXY_USERNAME

如果提供给 HTTP_PROXY_PASSWORD,请为 HTTP 代理使用凭证。

myusername

HTTP_PROXY_PASSWORD

如果由 HTTP_PROXY_USERNAME 提供,请为 HTTP 代理使用凭证。

mypassword

HTTP_PROXY_NONPROXYHOSTS

如果提供,配置的 HTTP 代理将忽略这些主机(以逗号分隔的主机、IP 地址或域列表)。

*.example.net,some.example.org

MAVEN_ARGS

覆盖在构建期间提供给 Maven 的参数。

-e -Popenshift -DskipTests -Dcom.redhat.xpaas.repo.redhatga package

MAVEN_ARGS_APPEND

在构建期间附加提供给 Maven 的用户参数。

-Dfoo=bar

MAVEN_MIRROR_URL

用于配置的 Maven 镜像/存储库管理器的 URL。

http://10.0.0.1:8080/repository/internal/

MAVEN_CLEAR_REPO

(可选)在构建后清除本地 Maven 存储库。

true

:leveloffset: 3

4.2. 用于 OpenShift 的 JWS 上的 valves

4.2.1. 用于 OpenShift 兼容环境变量的 JWS (评估组件)

您可以定义以下环境变量,将 valve 组件插入到关联的 Catalina 容器的请求处理管道中。

变量名称描述值示例默认值

ENABLE_ACCESS_LOG

启用 Access Log Valve 将访问信息记录到标准输出频道。

true

false

4.3. 检查日志

查看 OpenShift 日志或由正在运行的容器提供的日志:

$ oc logs -f <pod_name> <container_name>

访问日志存储在 /opt/webserver/logs/ 中。

法律通告

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.