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

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