openshift/origin学习记录(9)——S2I镜像定制(基于Git)

参考《开源容器云Openshift》一书,制作一个Tomcat的S2I镜像(从Git下载代码,Maven打包,部署到Tomcat上。)

从Svn下载代码的S2I镜像可以参考https://github.com/nichochen/openshift-tomcat-svn,这个貌似是《开源容器云Openshift》作者的github项目。

准备环境

  • 在Master上下载S2I的二进制执行文件。
# cd /opt 
# wget https://github.com/openshift/source-to-image/releases/download/v1.1.7/source-to-image-v1.1.7-226afa1-linux-386.tar.gz
  • 解压到/usr/bin目录下。
# tar zxvf source-to-image-v1.1.7-226afa1-linux-386.tar.gz -C /usr/bin
  • 通过s2i create命令创建一个名为tomcat-s2i的S2I Builder镜像。第二个参数tomcat-s2i为S2I Builder镜像名称。第三个参数tomcat-s2i-catalog定义了工作目录的名称。
# s2i create tomcat-s2i tomcat-s2i-catalog

执行find tomcat-s2i-catalog查看目录。

这里写图片描述

s2i目录下为S2I脚本。

其中:

  1. assemble:负责源代码的编译、构建以及构建产出物的部署。
  2. run:S2I流程生成的最终镜像将以这个脚本作为容器的启动命令。
  3. usage:打印帮助信息,一般作为S2I Builder镜像的启动命令。
  4. save-artifacts:为了实现增量构建,在构建过程中会执行此脚本保存中间构建产物。此脚本并不是必需的。

编写Dockerfile

编写一个制作Tomcat的S2I镜像。Dockerfile的内容如下:

# tomcat-s2i
FROM maven:3.3-jdk-7
# 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="Tomcat S2I Builder" \
      io.k8s.display-name="tomcat s2i builder 1.0" \
      io.openshift.expose-services="8080:http" \
      io.openshift.tags="builder,tomcat"
WORKDIR /opt
ADD ./apache-tomcat-8.5.5.tar.gz /opt
RUN useradd -m tomcat -u 1001 && \
chmod -R a+rw /opt && \
chmod a+rwx /opt/apache-tomcat-8.5.5/* && \
chmod +x /opt/apache-tomcat-8.5.5/bin/*.sh && \
rm -rf /opt/apache-tomcat-8.5.5/webapps/*
# TODO: Copy the S2I scripts to /usr/libexec/s2i, since maven:3.3-jdk-7 image
# sets io.openshift.s2i.scripts-url label that way, or update that label
COPY ./s2i/bin/ /usr/libexec/s2i
# This default user is created in the image
USER 1001
# 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"]

在本Dockerfile中,io.openshift.s2i.scripts-url=image:///usr/libexec/s2i标签指定了S2I依赖的脚本所在的路径。S2I执行器将到此路径中查找所需要的执行脚本。

通过USER dev定义了一个新用户,并指定该用户为容器的启动用户。以root用户作为启动用户在某些情况下存在安全风险。

编辑S2I脚本

  • 编辑s2i/bin/assemble脚本(负责源代码的编译、构建以及构建产出物的部署)。

在脚本最末尾添加如下代码:

cp -Rf /tmp/src/. ./
mvn -Dmaven.test.skip=true package
find . -type f -name '*.war'|xargs -i cp {} /opt/apache-tomcat-8.5.5/webapps/
mvn clean

这段代码会触发一次Maven构建,并将构建产生的WAR包拷贝到Tomcat服务器的webapps目录下进行部署。
完整的assemble脚本如下:

#!/bin/bash -e
#
# S2I assemble script for the 'tomcat-s2i' image.
# The 'assemble' script builds your application source so that it is ready to run.
#
# For more information refer to the documentation:
#       https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#
# If the 'tomcat-s2i' assemble script is executed with the '-h' flag, print the usage.
if [[ "$1" == "-h" ]]; then
        exec /usr/libexec/s2i/usage
fi
# Restore artifacts from the previous build (if they exist).
#
if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
  echo "---> Restoring build artifacts..."
  mv /tmp/artifacts/. ./
fi
echo "---> Installing application source..."
cp -Rf /tmp/src/. ./
echo "---> Building application from source..."
# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc.
mvn -Dmaven.test.skip=true package
find . -type f -name '*.war'|xargs -i cp {} /opt/apache-tomcat-8.5.5/webapps/
mvn clean
  • 编辑s2i/bin/run脚本(S2I流程生成的最终镜像将以这个脚本作为容器的启动命令)。

替换为以下内容:

bash -c "/opt/apache-tomcat-8.5.5/bin/catalina.sh run"

脚本内容为启动Tomcat服务器。

执行镜像构建

  • 下载对应版本的Tomcat安装包。
[root@master tomcat-s2i-catalog]# wget http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz
  • 构建镜像。s2i create命令为用户生成了一个Makefile,通过make指令可以启动Docker build。
[root@master tomcat-s2i-catalog]# make

这里写图片描述

导入镜像

  • 将tomcat-s2i镜像推送到自己的镜像仓库。

此步省略。

  • 将tomcat-s2i镜像导入Openshift中生成相应的Image Stream。
# oc import-image master.example.com:5000/tomcat-s2i -n openshift --confirm --insecure

导入openshift项目里,以便该Image Stream可以被其他项目引用。
这里写图片描述

  • 查看导入的Image Stream。
# oc get is -n openshift

这里写图片描述

为了让OpenShift识别出这个镜像是S2I的Builder镜像,需要编辑刚导入的Image Stream,添加注解“tags”

# oc edit is/tomcat-s2i -n openshift

这里写图片描述

主要是修改annotations下的内容,如红框所示,这里只是简单的添加。

修改完成后保存退出。

验证

登录web console,我的web console中已有新创建的镜像。

这里写图片描述

这里写图片描述

这里写图片描述

构建成功。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的 S2i 模板脚本示例: assemble 脚本: ``` #!/bin/bash echo "Building application..." # 编译应用程序 make # 拷贝应用程序到指定目录 cp /usr/src/app/app /tmp/src/ ``` run 脚本: ``` #!/bin/bash echo "Starting application..." # 启动应用程序 /tmp/src/app ``` save-artifacts 脚本: ``` #!/bin/bash echo "Saving artifacts..." # 保存应用程序的构建产物 cp /tmp/src/app /opt/app-root/src/ ``` Dockerfile 文件: ``` FROM centos:7 MAINTAINER Your Name <[email protected]> # 安装必要的依赖库 RUN yum -y install make gcc # 拷贝 assemble、run、save-artifacts 脚本到镜像中 COPY s2i/bin/assemble s2i/bin/run s2i/bin/save-artifacts /usr/local/bin/ # 设置应用程序的工作目录 WORKDIR /opt/app-root/src # 暴露应用程序的端口 EXPOSE 8080 # 设置应用程序的启动命令 CMD ["run"] ``` s2i 模板文件: ``` { "apiVersion": "template.openshift.io/v1", "kind": "Template", "metadata": { "name": "my-s2i-template", "annotations": { "description": "My S2I template" } }, "objects": [ { "apiVersion": "image.openshift.io/v1", "kind": "ImageStream", "metadata": { "name": "my-image-stream" } }, { "apiVersion": "build.openshift.io/v1", "kind": "BuildConfig", "metadata": { "name": "my-build-config" }, "spec": { "source": { "type": "Git", "git": { "uri": "<Git 仓库地址>", "ref": "master" } }, "strategy": { "type": "Source", "sourceStrategy": { "from": { "kind": "ImageStreamTag", "name": "centos:7" }, "scripts": { "assemble": "source /usr/local/bin/assemble", "run": "source /usr/local/bin/run", "save-artifacts": "source /usr/local/bin/save-artifacts" }, "incremental": false } }, "output": { "to": { "kind": "ImageStreamTag", "name": "my-image-stream:latest" } } } } ], "parameters": [ { "name": "GIT_URI", "required": true, "description": "Git 仓库地址" } ] } ``` 注:这只是一个简单的示例,具体的应用程序需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值