日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/3/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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腳本。

其中:

  • assemble:負責源代碼的編譯、構建以及構建產出物的部署。
  • run:S2I流程生成的最終鏡像將以這個腳本作為容器的啟動命令。
  • usage:打印幫助信息,一般作為S2I Builder鏡像的啟動命令。
  • 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" ]]; thenexec /usr/libexec/s2i/usage fi # Restore artifacts from the previous build (if they exist). # if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; thenecho "---> 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中已有新創建的鏡像。

    構建成功。

    總結

    以上是生活随笔為你收集整理的openshift/origin学习记录(9)——S2I镜像定制(基于Git)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。