Openshift S2I构建流程

S2I基础概念

Source-to-Image(S2I)是一个框架,可以将应用的源码做为输入的镜像,并生成一个新的镜像。其主要优点是方便开发人员使用。需要了解两个基础概念“构建过程”和“S2I scripts”。
构建过程包括下列三个基本元素,在构建过程中S2I将S2I scripts和sources放入构建镜像中

  • sources
  • S2I scripts
  • builder image

S2I构建流程如下(官方):
在这里插入图片描述

S2I实践部署

  1. 下载s2i工具
    wget https://github.com/openshift/source-to-image/releases/download/v1.1.12/source-to-image-v1.1.12-2a783420-linux-amd64.tar.gz
    解压放到/usr/bin/下

  2. 创建一个名为tomcat-s2i的S2I Builder镜像。
    第二个参数为生成的image名称(在catalog中默认显示的也是该值),第三个参数为工作目录的名称。
    s2i create registry.xx.com/tomcat-s2i tomcat-s2i-catalog

  3. 目录结构,额外准备tomcat包
    tomcat-s2i-catalog/
    ├── apache-tomcat-8.5.5.tar.gz
    ├── Dockerfile
    ├── Makefile
    ├── README.md
    ├── s2i
    │ └── bin
    │ ├── assemble
    │ ├── run
    │ ├── save-artifacts
    │ └── usage
    └── test
    ├── run
    └── test-app
    └── index.html

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

  1. 编辑dockerfile,其中io.openshift.s2i.scripts-url=image:///usr/libexec/s2i标签指定了S2I依赖的脚本所在的路径,并将tomcat拷贝至image的/opt目录下,/opt作为WORKDIR、设置启动用户、配置暴露端口号
# tomcat-s2i
FROM maven:3.3-jdk-7
# TODO: Put the maintainer name in the image metadata
MAINTAINER zhangzhidao
# 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"]
  1. 编辑s2i/bin/assemble脚本(负责源代码的编译、构建以及构建产出物的部署)默认情况下代码clone下来后放到/tmp/src目录,代码编译打包后的war拷贝至tomcat/webapps下部署,这里将mvn打包作为参数,用于在catalog自定义输入mvn参数
echo "---> Installing application source..."
cp -Rf /tmp/src/. ./
ls -al ./
# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc.
echo "---> Building application from source..."
echo ${mvn_args}
eval ${mvn_args}
find . -type f -name '*.war'|xargs -i cp {} /opt/apache-tomcat-8.5.5/webapps/

  1. 编辑s2i/bin/run脚本(S2I流程生成的最终镜像将以这个脚本作为容器的启动命令)
bash -c "/opt/apache-tomcat-8.5.5/bin/catalina.sh run"
  1. 构建镜像并上传
    cd tomcat-s2i-catalog/ && make
    docker push registry.xx.com/openshift/tomcat8

  2. 导入镜像生成image stream,放到openshift中可以被其它项目引用
    oc import-image registry.xx.com/openshift/tomcat8 -n openshift --confirm --insecure

  3. 查看image stream
    oc get is -n openshift

  4. 为了让OpenShift识别出这个镜像是S2I的Builder镜像,即在catalog中显示,需要编辑刚导入的Image Stream,添加注解"tags"

  - annotations:
      description: simple s2i tomcat;
      openshift.io/display-name: tomcat iqj
      sampleRepo: https://github.com/nichochen/mybank-demo-maven
      tags: builder,tomcat,java
      version: "8.0"
    from:
      kind: DockerImage
      name: registry.xx.com/tomcat8
    generation: 7
    importPolicy:
      insecure: true
    name: "8.0"
    referencePolicy:
      type: Source

几个关键参数说明:
description 描述
sampleRepo 示例地址,在configuration点击Try Sample Repository 可以避免手动输入测试地址
tags 会出现在Browse Catalog Java 分类下
from – name 镜像地址
name 在configuration页面Version下拉列表显示多个版本号

S2I验证及常用参数解析

刷新web console点Browse Catalog可以看到新建的目录
在这里插入图片描述
测试github
https://github.com/nichochen/mybank-demo-maven

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
成功(注:这里务必确保集群网络和DNS没有问题,我这里之前一直失败提示不能连接github.com,如果有问题请查看网络配置)

附:制作过程有问题,按照下列流程重复执行

  1. 修改脚本、dockerfile等
  2. make
  3. docker push registry.xx.com/openshift/tomcat7
  4. 删除、导入、编辑资源
    oc delete is/tomcat7 -n openshift
    oc import-image registry.xx.com/openshift/tomcat7 -n openshift --confirm --insecure
    oc edit is/tomcat7 -n openshift

参考文献:
https://docs.okd.io/3.11/creating_images/s2i.html#s2i-scripts
https://blog.csdn.net/huqigang/article/details/78110233

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章