环境:OCP 3.4
从 OpenShift 3.2 开始支持 binary 方式部署,即支持直接部署 war 文件。
1. 首先确认 jboss-webserver30-tomcat8-openshift Image Stream 导入成功
oc get is jboss-webserver30-tomcat8-openshift -n openshift
输出如下:
NAME DOCKER REPO TAGS UPDATED
jboss-webserver30-tomcat8-openshift registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift
导入后的 Image Stream 有个问题,就是没有 Tags,还需要导入相应的 Image。
docker tag registry.access.redhat.com/jboss-webserver-3/webserver30-tomcat8-openshift:latest registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift:latest (之前已做)
docker push registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift (之前已做)
oc import-image jboss-webserver30-tomcat8-openshift --insecure -n openshift
再次检查,这次有了 tag。
oc get is jboss-webserver30-tomcat8-openshift -n openshift
输出如下:
NAME DOCKER REPO TAGS UPDATED
jboss-webserver30-tomcat8-openshift registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift latest 7 seconds ago
2. 根据 image stream 创建一个新 build,并指定 binary=true
oc new-build --image-stream=jboss-webserver30-tomcat8-openshift --binary=true --name=myapp
参数 --binary=true 表示 build 将会使用 binary 而不是源代码。
输出如下:
--> Found image d8f7cc1 (6 weeks old) in image stream "openshift/jboss-webserver30-tomcat8-openshift" under tag "latest" for "jboss-webserver30-tomcat8-openshift"
JBoss Web Server 3.0
--------------------
Platform for building and running web applications on JBoss Web Server 3.0 - Tomcat v8
Tags: builder, java, tomcat8
* 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
3. 开始 build
可以直接指定 war 文件,也可以指定存放 .war 文件的目录,两种方式选择一种即可。
3.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
Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 89% complete
Pushed 7/7 layers, 100% complete
Push successful
注意,这样做不会成功,原因是 .war 文件没有复制到 /opt/webserver/webapps 目录下。
这一点从输出日志也可以看出来,没有进行复制操作。应该是个 BUG。
经过摸索,可以这样做
oc start-build myapp --from-file=. --follow=true --wait=true
Uploading directory "." as binary input for the build ...
build "myapp-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/cluster_test_repl.war' -> '/opt/webserver/webapps/cluster_test_repl.war'
Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 86% complete
Pushed 7/7 layers, 100% complete
Push successful
3.2 指定存放 .war 文件目录
这种方式可以部署多个 war 文件,这里在当前目录下的 deployments 目录下放置 cluster_test.war 文件。
注意,指定的目录下面必须有一个 deployments 目录,deployments 目录下面放置 .war 文件。
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 deployments war artifacts from /home/jboss/source/deployments directory into /opt/webserver/webapps for later deployment...
'/home/jboss/source/deployments/cluster_test_repl.war' -> '/opt/webserver/webapps/cluster_test_repl.war'
Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 86% complete
Pushed 7/7 layers, 100% complete
Push successful
4. 创建应用
oc new-app myapp --insecure-registry=true
输出如下:
--> Found image 6304e7c (42 seconds old) in image stream "applications/myapp" under tag "latest" for "myapp"
applications/myapp-1:5c9b5f29
-----------------------------
Platform for building and running web applications on JBoss Web Server 3.0 - Tomcat v8
Tags: builder, java, tomcat8
* 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.
5. 创建 route
oc expose svc myapp
6. 访问 http://myapp-applications.apps.example.com/cluster_test_repl/index.jsp
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://playbooks-rhtconsulting.rhcloud.com/playbooks/app_dev/binary_deployment_howto.html
从 OpenShift 3.2 开始支持 binary 方式部署,即支持直接部署 war 文件。
1. 首先确认 jboss-webserver30-tomcat8-openshift Image Stream 导入成功
oc get is jboss-webserver30-tomcat8-openshift -n openshift
输出如下:
NAME DOCKER REPO TAGS UPDATED
jboss-webserver30-tomcat8-openshift registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift
导入后的 Image Stream 有个问题,就是没有 Tags,还需要导入相应的 Image。
docker tag registry.access.redhat.com/jboss-webserver-3/webserver30-tomcat8-openshift:latest registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift:latest (之前已做)
docker push registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift (之前已做)
oc import-image jboss-webserver30-tomcat8-openshift --insecure -n openshift
再次检查,这次有了 tag。
oc get is jboss-webserver30-tomcat8-openshift -n openshift
输出如下:
NAME DOCKER REPO TAGS UPDATED
jboss-webserver30-tomcat8-openshift registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift latest 7 seconds ago
2. 根据 image stream 创建一个新 build,并指定 binary=true
oc new-build --image-stream=jboss-webserver30-tomcat8-openshift --binary=true --name=myapp
参数 --binary=true 表示 build 将会使用 binary 而不是源代码。
输出如下:
--> Found image d8f7cc1 (6 weeks old) in image stream "openshift/jboss-webserver30-tomcat8-openshift" under tag "latest" for "jboss-webserver30-tomcat8-openshift"
JBoss Web Server 3.0
--------------------
Platform for building and running web applications on JBoss Web Server 3.0 - Tomcat v8
Tags: builder, java, tomcat8
* 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
3. 开始 build
可以直接指定 war 文件,也可以指定存放 .war 文件的目录,两种方式选择一种即可。
3.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
Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 89% complete
Pushed 7/7 layers, 100% complete
Push successful
注意,这样做不会成功,原因是 .war 文件没有复制到 /opt/webserver/webapps 目录下。
这一点从输出日志也可以看出来,没有进行复制操作。应该是个 BUG。
经过摸索,可以这样做
oc start-build myapp --from-file=. --follow=true --wait=true
注意,此种方式指定的目录下面必须有一个 deployments 目录,deployments 目录下面放置 .war 文件。也就是说,此时 --from-file 和 --from-dir 功能是一样的。
输出如下:Uploading directory "." as binary input for the build ...
build "myapp-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/cluster_test_repl.war' -> '/opt/webserver/webapps/cluster_test_repl.war'
Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 86% complete
Pushed 7/7 layers, 100% complete
Push successful
这种方式可以部署多个 war 文件,这里在当前目录下的 deployments 目录下放置 cluster_test.war 文件。
注意,指定的目录下面必须有一个 deployments 目录,deployments 目录下面放置 .war 文件。
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 deployments war artifacts from /home/jboss/source/deployments directory into /opt/webserver/webapps for later deployment...
'/home/jboss/source/deployments/cluster_test_repl.war' -> '/opt/webserver/webapps/cluster_test_repl.war'
Pushing image 172.30.98.122:5000/applications/myapp:latest ...
Pushed 6/7 layers, 86% complete
Pushed 7/7 layers, 100% complete
Push successful
4. 创建应用
oc new-app myapp --insecure-registry=true
输出如下:
--> Found image 6304e7c (42 seconds old) in image stream "applications/myapp" under tag "latest" for "myapp"
applications/myapp-1:5c9b5f29
-----------------------------
Platform for building and running web applications on JBoss Web Server 3.0 - Tomcat v8
Tags: builder, java, tomcat8
* 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.
5. 创建 route
oc expose svc myapp
6. 访问 http://myapp-applications.apps.example.com/cluster_test_repl/index.jsp
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://playbooks-rhtconsulting.rhcloud.com/playbooks/app_dev/binary_deployment_howto.html