2017年1月29日星期日

OpenShift_048:使用 binary 方式部署 .war 到 JBoss EAP 7 集群

环境:OCP 3.4

从 OpenShift 3.2 开始支持 binary 方式部署,即支持直接部署 war 文件。

1. 根据 image stream 创建一个新 build,并指定 binary=true
oc new-build --image-stream=jboss-eap70-openshift --binary=true --name=myapp

参数 --binary=true 表示 build 将会使用 binary 而不是源代码。
输出如下:
--> Found image 92138ab (6 weeks old) in image stream "openshift/jboss-eap70-openshift" under tag "latest" for "jboss-eap70-openshift"

    JBoss EAP 7.0
    -------------
    Platform for building and running JavaEE applications on JBoss EAP 7.0

    Tags: builder, javaee, eap, eap7

    * A source build using binary input will be created
      * The resulting image will be pushed to image stream "myapp:latest"
      * A binary build was created, use 'start-build --from-dir' to trigger a new build

--> Creating resources with label build=myapp ...
    imagestream "myapp" created
    buildconfig "myapp" created
--> Success

2. 开始 build 
可以直接指定 war 文件,也可以指定存放 .war 文件的目录,两种方式选择一种即可。

2.1 指定 war 文件
oc start-build myapp --from-file=cluster_test_repl.war --follow=true --wait=true
输出如下:
Uploading file "cluster_test_repl.war" as binary input for the build ...
build "myapp-1" started
Receiving source from STDIN as file cluster_test_repl.war

Copying all war artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
'/home/jboss/source/./cluster_test_repl.war' -> '/opt/eap/standalone/deployments/cluster_test_repl.war'
Copying all ear artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
Copying all rar artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
Copying all jar artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
Copying all war artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...
Copying all ear artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...
Copying all rar artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...
Copying all jar artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...


Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 87% complete
Pushed 7/7 layers, 100% complete
Push successful

2.2 指定存放 .war 文件目录 
这种方式可以部署多个 war 文件,这里在当前目录下放置 cluster_test_repl.war 文件。
注意,指定的目录下可以直接放 .war 文件,不必须有一个 deployments 目录,这一点和 tomcat 8 不一样。
oc start-build myapp --from-dir=. --follow=true --wait=true
输出如下:
Uploading directory "." as binary input for the build ...
build "myapp-1" started
Receiving source from STDIN as archive ...

Copying all war artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
'/home/jboss/source/./cluster_test_repl.war' -> '/opt/eap/standalone/deployments/cluster_test_repl.war'
Copying all ear artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
Copying all rar artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
Copying all jar artifacts from /home/jboss/source/. directory into /opt/eap/standalone/deployments for later deployment...
Copying all war artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...
Copying all ear artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...
Copying all rar artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...
Copying all jar artifacts from /home/jboss/source/deployments directory into /opt/eap/standalone/deployments for later deployment...


Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 87% complete
Pushed 7/7 layers, 100% complete
Push successful

3. 创建应用
oc new-app myapp
输出如下:
--> Found image c3032f8 (About a minute old) in image stream "applications/myapp" under tag "latest" for "myapp"

    applications/myapp-1:4f873094
    -----------------------------
    Platform for building and running JavaEE applications on JBoss EAP 7.0

    Tags: builder, javaee, eap, eap7

    * This image will be deployed in deployment config "myapp"
    * Ports 8080/tcp, 8443/tcp, 8778/tcp will be load balanced by service "myapp"
      * Other containers can access this service through the hostname "myapp"

--> Creating resources ...
    deploymentconfig "myapp" created
    service "myapp" created
--> Success
    Run 'oc status' to view your app.

4. 创建 route
oc expose svc myapp

5. 访问 http://myapp-applications.apps.example.com/cluster_test_repl/index.jsp

6. 配置集群
弹性扩展 myapp
oc scale dc/myapp --replicas=2

给 default sa 授权 view
oc policy add-role-to-user view system:serviceaccount:$(oc project -q):default -n $(oc project -q)

创建 eap7-service-account sa
oc create serviceaccount eap7-service-account -n $(oc project -q)

给 eap7-service-account sa 授权 view
oc policy add-role-to-user view system:serviceaccount:$(oc project -q):eap7-service-account -n $(oc project -q)

为 myapp DeploymentConfig 增加环境变量
oc env dc/myapp -e OPENSHIFT_KUBE_PING_NAMESPACE=$(oc project -q) OPENSHIFT_KUBE_PING_LABELS=app=myapp

查看 app pod 日志
oc logs -f < app pod1 name > 
oc logs -f < app pod2 name >

刷新页面,删除接受请求的 pod,继续刷新页面,发现数字继续增长。
// TODO 数字没有继续增长,而是重新增长

7. 清理
如果有错,执行以下命令清理,然后重做:
oc delete bc/myapp is/myapp dc/myapp routes/myapp svc/myapp

参考文献:
1. https://blog.openshift.com/binary-input-sources-openshift-3-2/
2. http://labs.openshift3roadshow.com/roadshow/clustering
3. https://access.redhat.com/documentation/en/red-hat-jboss-middleware-for-openshift/3/single/red-hat-jboss-enterprise-application-platform-for-openshift/#clustering

没有评论: