【openshift 学习笔记】第十四章 系统集成与定制

一. s2i 镜像定制

 下载S2I的二进制执行文件

wget https://github.com/openshift/source-to-image/releases/download/v1.1.9/source-to-image-v1.1.9-db2b4645-linux-amd64.tar.gz

 解压至 /usr/bin 目录下

tar -xvzf source-to-image-v1.1.9-db2b4645-linux-amd64.tar.gz -C /usr/bin

 通过 s2i 创建一个 tomcat-s2i 的 S2I Builder 镜像

s2i create tomcat-s2i tomcat-s2i-catalog

s2i目录下为S2I脚本。

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


二. 编写 Dockerfile

FROM maven:3.3-jdk-7
ENV BUILDER_VERSION 1.0
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/*
COPY ./s2i/bin/ /usr/libexec/s2i
USER 1001
EXPOSE 8080
ENTRYPOINT []
CMD ["/usr/libexec/s2i/usage"]

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


三. 编辑 S2I 脚本

 编辑s2i/bin/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
触发一次Maven构建,并将构建产生的WAR包拷贝到Tomcat服务器的webapps目录下进行部署

 编辑s2i/bin/run脚本

#!/bin/bash -e
#
# S2I run script for the 'tomcat-s2i' image.
# The run script executes the server that runs your application.
#
# For more information see the documentation:
#	https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md
#


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


四. 构建

 执行make以及将tomcat-s2i镜像推送到自己的镜像仓库,将镜像倒入到 openshift 项目

oc import-image xxxxxxx/sandbox/tomcat-s2i -n openshift --confirm --insecure

      查看导入的Image Stream: oc get is -n openshift



为了 OpenShift 识别镜像是 S2I Builder 镜像,需要编辑Image Stream,添加注解“tags”, oc edit is tomcat-s2i -n openshift

      - annotations:
           description: tomcat builder
           openshift.io/display-name: tomcat builder
           tags: builder,tomcat


四. 验证



猜你喜欢

转载自blog.csdn.net/zhonglinzhang/article/details/79927885
今日推荐