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中已有新創建的鏡像。

這裏寫圖片描述

這裏寫圖片描述

這裏寫圖片描述

構建成功。

發佈了67 篇原創文章 · 獲贊 23 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章