本篇博客所用到的代码已上传至github。https://github.com/hu12081/openshift-s2i-tomcat-svn.git
不建议直接clone git,否则,注意修改文件权限。
s2i源码研究(能力不够,修改失败)
周一的时候在github上粗略阅读了source-to-image的源码,代码地址为https://github.com/openshift/source-to-image。整体代码采用go语言实现,https://github.com/openshift/source-to-image/tree/master/pkg/scm/downloaders目录下的代码应该是实现代码下载功能的,包括从本地文件路径拷贝代码、通过git克隆代码。
修改builder镜像,定制部署模板(成功实现)
周二在看《开源容器云openshift》一书时,无意在书中发现作者的github,上面就有svn的demo,地址为https://github.com/nichochen/openshift-tomcat-svn。着实尴尬。
该项目的最后提交时间为两年前,在尝试部署的过程中发现存在以下问题:
1.ose-json文件夹下的openshift-tomcat7-svn-is.json存在语法错误。
2.该部署模板虽然集成了svn,但是在web界面上必须填写可用的git地址(该地址只用于跳过s2i验证),作者在README.md指出了该缺陷,缺没有修复。
3.没有提供svn的账号、密码输入功能。
4.直接clone该项目,在builder镜像的使用过程中,会报文件夹权限的问题。
优化已有的开源项目
参考我的博客openshift/origin学习记录(9)——S2I镜像定制(基于Git)
以下内容有做了一些定制化开发(基本都有表明)不具有通用性,不推荐直接复制粘贴使用!!!请自行修改参数、指令等内容。
创建S2I Builder镜像工作目录
通过s2i create命令创建一个名为tomcat-s2i的S2I Builder镜像。第二个参数tomcat-svn为S2I Builder镜像名称。第三个参数tomcat-svn-catalog定义了工作目录的名称。
s2i create tomcat-svn tomcat-svn-catalog
编写Dockerfile
修改tomcat-svn-catalog目录下的Dockerfile文件。
# openshift-tomcat8-svn
FROM docker.io/centos
# TODO: Put the maintainer name in the image metadata
MAINTAINER huliaoliao
# TODO: Rename the builder environment variable to inform users about application you provide them
ENV BUILDER_VERSION 1.0
# TODO: Set labels used in OpenShift to describe the builder image
LABEL io.openshift.s2i.scripts-url=image:///usr/libexec/s2i \
io.k8s.description="Platform for building tomcat" \
io.k8s.display-name="builder tomcat" \
io.openshift.expose-services="8080:http" \
io.openshift.tags="builder,tomcat,java,etc."
# TODO: Install required packages here:
COPY ./CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum makecache && yum install -y java-1.8.0-openjdk subversion maven && yum clean all -y
COPY ./s2i/bin/ /usr/libexec/s2i
# TODO (optional): Copy the builder files into /opt/app-root
COPY ./tomcat8/ /opt/app-root/tomcat8
# TODO: Copy the S2I scripts to /usr/local/s2i, since openshift/base-centos7 image sets io.openshift.s2i.scripts-url label that way, or update that label
#COPY ./s2i/bin/ /usr/libexec/s2i
# TODO: Drop the root user and make the content of /opt/app-root owned by user 1001
RUN useradd -m tomcat -u 1002 && \
chmod -R a+rw /opt && \
chmod -R a+rw /opt/app-root && \
chmod a+rwx /opt/app-root/tomcat8/* && \
chmod +x /opt/app-root/tomcat8/bin/*.sh && \
rm -rf /opt/app-root/tomcat8/webapps/* && \
rm -rf /usr/share/maven/conf/settings.xml
ADD ./settings.xml /usr/share/maven/conf/
# This default user is created in the openshift/base-centos7 image
USER 1002
# TODO: Set the default port for applications built using this image
EXPOSE 8080
ENTRYPOINT []
# TODO: Set the default CMD for the image
CMD ["/usr/libexec/s2i/usage"]
其中COPY ./CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
是为了换源。rm -rf /usr/share/maven/conf/settings.xml
和ADD ./settings.xml /usr/share/maven/conf/
是修改镜像中maven的配置,指向自己的maven库。
下载tomcat
wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz
解压到tomcat-svn-catalog目录下的tomcat8文件夹下。
修改s2i/bin/assemble脚本(负责源代码的编译、构建以及构建产出物的部署)
#!/bin/bash -e
#
# S2I assemble script for the 'nico-tomcat' image.
# The 'assemble' script builds your application source ready to run.
#
# For more information refer to the documentation:
# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
# Restore artifacts from the previous build (if they exist).
#
if [ "$1" = "-h" ]; then
# If the 'nico-tomcat' assemble script is