3.4. 将 JWS 用于 OpenShift Source-to-Image (S2I)流程
要为 OpenShift 镜像运行并配置 JWS,请将 OpenShift S2I 进程与应用模板参数和环境变量一起使用。
用于 OpenShift 镜像的 JWS 的 S2I 流程可以正常工作:
如果
configuration/source 目录中有一个 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/jws-5.5/tomcat/webapps中。这包括$ARTIFACT_DIR环境变量指定的源目录中的所有 WAR 文件。$ARTIFACT_DIR的默认值为target/目录。使用
MAVEN_ARGS_APPEND环境变量来修改 Maven 参数。-
deployments/source 目录中的所有 WAR 文件都复制到/opt/jws-5.5/tomcat/webapps中。 -
configuration/source 目录中的所有文件都复制到/opt/jws-5.5/tomcat/conf/(不包括 Maven settings.xml 文件)。 lib/source 目录中的所有文件都复制到/opt/jws-5.5/tomcat/lib/中。注意如果要使用自定义 Tomcat 配置文件,文件名应当与普通 Tomcat 安装相同。例如: context.xml 和 server.xml。
有关配置 S2I 进程以使用自定义 Maven 工件存储库镜像的指南,请参阅 Artifact Repository Mirrors 部分。
3.4.1. 使用现有的 maven 二进制文件为 OpenShift 应用创建一个 JWS
现有应用使用 oc start-build 命令在 OpenShift 上部署。
先决条件: 要在 OpenShift 的 JWS 上部署的应用程序的现有 .war、.ear 或 .jar。
在本地文件系统上准备目录结构。
创建包含未包含在二进制文件中的任何内容的源目录(如果需要,请参阅 使用 JWS for OpenShift Source-to-Image (S2I)进程),然后创建一个子目录
deployments/:$ mkdir -p <build_dir>/deployments将二进制文件(
.war,.ear,.jar)复制到deployments/:$ cp /path/to/binary/<filenames_with_extensions> <build_dir>/deployments/
注意源目录的
deployments/子目录中的应用程序存档被复制到 OpenShift 上构建的镜像的$JWS_HOME/tomcat/webapps/目录中。要部署应用程序,包含 Web 应用程序数据的目录层次结构必须被正确构建(请参阅 第 3.4 节 “将 JWS 用于 OpenShift Source-to-Image (S2I)流程”)。登录到 OpenShift 实例:
$ oc login <url>根据需要创建新项目:
$ oc new-project <project-name>使用
oc get 识别用于应用程序的 OpenShift 镜像流的 JWS 是 -n openshift:$ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshift注意选项
-n openshift指定要使用的项目。oc get 是 -n openshift从openshift项目检索(get)镜像流资源(即)。创建新构建配置,指定镜像流和应用程序名称:
$ oc new-build --binary=true \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel8:latest \ --name=<my-jws-on-openshift-app>
指示 OpenShift 使用上面创建的源目录进行 OpenShift 镜像构建的二进制输入:
$ oc start-build <my-jws-on-openshift-app> --from-dir=./<build_dir> --follow
根据镜像创建新 OpenShift 应用程序:
$ oc new-app <my-jws-on-openshift-app>公开该服务以使应用程序可以被用户访问:
# 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
检索公开路由的地址:
oc get routes --no-headers -o custom-columns='host:spec.host' my-jws-on-openshift-app
- 要在浏览器中访问应用程序: http:// <address_of_exposed_route> / & lt ;my-war-ear-jar-filename-without-extension>
3.4.2. 示例:使用现有 maven 二进制文件为 OpenShift 应用程序创建 JWS
以下示例使用 第 3.4.1 节 “使用现有的 maven 二进制文件为 OpenShift 应用创建一个 JWS” 中的步骤使用 tomcat-websocket-chat Quickstart。
3.4.2.1. 先决条件:
获取 WAR 应用程序存档或在本地构建应用程序。
克隆源代码:
$ git clone https://github.com/jboss-openshift/openshift-quickstarts.git
构建应用程序:
$ 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] ------------------------------------------------------------------------
在本地文件系统上准备目录结构。
在本地文件系统和
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.4.2.2. 在 OpenShift 中设置示例应用程序
登录到 OpenShift 实例:
$ oc login <url>根据需要创建新项目:
$ oc new-project jws-bin-demo
使用
oc get 识别用于应用程序的 OpenShift 镜像流的 JWS 是 -n openshift:$ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshift创建新构建配置,指定镜像流和应用程序名称:
$ oc new-build --binary=true \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel8:latest\ --name=jws-wsch-app --> Found image 8c3b85b (4 weeks old) in image stream "openshift/jboss-webserver<version>-tomcat9-openshift" under tag "latest" for "jboss-webserver<version>" JBoss Web Server 5.0 -------------------- Platform for building and running web applications on JBoss Web Server 5.0 - Tomcat v9 Tags: builder, java, tomcat9 * 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
启动二进制构建。指示 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/jws-5.5/tomcat/webapps` for later deployment... '/home/jboss/source/deployments/websocket-chat.war' -> '/opt/jws-5.5/tomcat/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
根据镜像创建新 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 5.0 -------------------- Platform for building and running web applications on JBoss Web Server 5.0 - Tomcat v9 Tags: builder, java, tomcat9 * 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.公开该服务以使应用程序可以被用户访问:
# 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
检索公开路由的地址:
oc get routes --no-headers -o custom-columns='host:spec.host' jws-wsch-app
- 在浏览器中访问应用程序: http:// <address_of_exposed_route>/websocket-chat
3.4.3. 从源代码为 OpenShift 应用创建一个 JWS
有关从源代码创建新 OpenShift 应用程序的详细信息,请参阅 OpenShift.com - 从源代码创建应用程序。
在继续操作前,请确保应用程序的数据被正确结构化(请参阅 第 3.4 节 “将 JWS 用于 OpenShift Source-to-Image (S2I)流程”)。
登录到 OpenShift 实例:
$ oc login <url>根据需要创建新项目:
$ oc new-project <project-name>使用
oc get 识别用于应用程序的 OpenShift 镜像流的 JWS 是 -n openshift:$ oc get is -n openshift | grep ^jboss-webserver | cut -f1 -d ' ' jboss-webserver50-tomcat9-openshift使用 Red Hat JBoss Web Server for OpenShift 镜像从源代码创建新的 OpenShift 应用程序,使用
--image-stream选项:$ oc new-app \ <source_code_location>\ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel8\ --name=<openshift_application_name>
例如:
$ oc new-app \ https://github.com/jboss-openshift/openshift-quickstarts.git#master \ --image-stream=jboss-webserver<version>-openjdk8-tomcat9-openshift-rhel8\ --context-dir='tomcat-websocket-chat' \ --name=jws-wsch-app
源代码添加到镜像中,并编译源代码。也创建了构建配置和服务。
公开应用程序:
# 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
检索公开路由的地址:
oc get routes --no-headers -o custom-columns='host:spec.host' <openshift_application_name>- 要在浏览器中访问应用程序: http:// <address_of_exposed_route> / <java_application_name>