openshift 中部署springboot項目並增量更新

 最新公司準備應用openshift,於是安排我學習openshift,通過好幾天的學習,嘗試,踩過無數個坑,終於搭建起了一個Mater,一個Node的openshift環境:

  公司後端項目是基於springboot,openshift沒有提供現有的is,所以需要通過s2i自己製作is,現記錄過程如下:

 1. 下載s2i,過程略過...

 2. 通過s2i創建鏡像:

     s2i create springboot-s2i springboot-s2i

   

   

s2i目錄下爲S2I腳本。

其中:

assemble:負責源代碼的編譯、構建以及構建產出物的部署。
run:S2I流程生成的最終鏡像將以這個腳本作爲容器的啓動命令。
usage:打印幫助信息,一般作爲S2I Builder鏡像的啓動命令。
save-artifacts:爲了實現增量構建。稍後對針對springboot的增量更新做詳細說明

3. 編輯Dockfile文件:

#springboot-s2i
FROM maven:3.6-jdk-8

# TODO: Put the maintainer name in the image metadata
LABEL maintainer="huabing.li"

# TODO: Rename the builder environment variable to inform users about application you provide them
ENV MAVEN_CONFIG=.m2

# TODO: Set labels used in OpenShift to describe the builder image
LABEL io.openshift.s2i.scripts-url="image:///usr/libexec/s2i" \
      io.openshift.s2i.destination="/opt/s2i/destination"
#LABEL io.k8s.description="Platform for building xyz" \
#      io.k8s.display-name="builder x.y.z" \
#      io.openshift.expose-services="8080:http" \
#      io.openshift.tags="builder,x.y.z,etc."

# TODO: Install required packages here:
WORKDIR /opt

RUN useradd -m deploy -u 1001 && \
    chmod -R 777 /opt && \
    mkdir -p /opt/s2i/destination && \
    chmod -R 777 /opt/s2i/destination

# TODO: Copy the S2I scripts to /usr/libexec/s2i, since openshift/base-centos7 image
# sets io.openshift.s2i.scripts-url label that way, or update that label
COPY ./s2i/bin/ /usr/libexec/s2i

# TODO: Drop the root user and make the content of /opt/app-root owned by user 1001
RUN chown -R 1001:1001 /opt && \
    chown -R 1001:1001 /var

# This default user is created in the openshift/base-centos7 image
USER 1001

# TODO: Set the default port for applications built using this image
# EXPOSE 8080

# TODO: Set the default CMD for the image
# CMD ["/usr/libexec/s2i/usage"]

  4. 編輯s2i/bin/assemble文件:

#!/bin/bash -e
#
# S2I assemble script for the 'springboot-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 'springboot-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 -A /opt/s2i/destination/artifacts/ 2>/dev/null)" ]; then
  echo "---> Restoring build artifacts..."
  # ls -la /opt/s2i/destination/artifacts/home/deploy/.m2
  mv /opt/s2i/destination/artifacts/home/deploy/.m2 $HOME/.m2
fi

echo "---> Installing application source..."

cp -Rf /opt/s2i/destination/src/. ./

mvn compile -Dmaven.test.skip=true package

find . -type f -name '*.tar.gz'|xargs -i tar zxvf {}

echo "---> Building application from source..."
# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc.

5. 編輯s2i/bin/run文件

 

#!/bin/bash -e
#
# S2I run script for the 'springboot-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 "java -jar financial-assistant.jar"

bash -c "bin/single.sh start ${SPRING_PROFILES_ACTIVE}"
tail -f logs/stdout.log

  bin/single.sh腳本爲項目定製啓動腳本,此處可以換成具體的啓動命令;${SPRING_PROFILES_ACTIVE}是環境變量,表示項目啓動時應用那個profile。

6. 編輯增量文件

 

#!/bin/sh -e

tar cf - $HOME/.m2

7.編譯,並把創建的鏡像推到私有倉庫

 

1. make
2. docker tag springboot-s2i master.openshift.com:5000/springboot-s2i
3. docker push master.openshift.com:5000/springboot-s2i
4. oc import-image master.openshift.com:5000/springboot-s2i -n openshift --confirm --insecure

 執行第4步需要登陸openshift並擁有管理員權限。

 8. 編輯is,使得openshift能夠識別剛導入的is

 

 9.操作完成後,就能在openshfit console中看到剛創建的is了,如圖:

10.根據剛生成的springboot-s2i,我們嘗試部署一下系統:

 

中間綠色表示的當次構建成功,所以剛纔構建的springboot-s2i是能夠使用的。

11. 現在講解一下增量更新

   a. 首先說一下增量更新的原理:

     1. 當標記Builds 需要增量更新時,openshift會已上一次成功構建的鏡像啓動,然後執行s2i/bin/save-artifacts腳本,打包需要保存的內容至標準輸出流,上第<6>步驟只保存了.m2下的本地倉庫;

     2. 執行s2i/bin/assemble腳本,判斷是否是增量構建:第<4>步中判斷目標目錄中是否有從上一個容器中複製的文件來判斷是否需要執行復制/移動操作:

 

 圖中圈中部分腳本就是增量更新時執行邏輯,/opt/s2i/destination目錄爲Dockerfile 中定義的Label,大概意思是增量更新時,從save-artifacts腳本打包的內容解壓到定義的artifacts目錄下。

3. 需要增量更新,還需要配置Build config:

   

點擊保存之後,重新構建,查看maven倉庫是否重新下載,判斷增量更新是否起作用:

通過查看日誌,可以看到,再進行新一次構建過程中並沒有重新下載依賴,所以增量更新成功。

查看構建歷史,可以看到最後配置了增量更新後的構建耗時明顯由於沒有配置增量構建之前的構建的。

 在搭建環境和操作過程中,遇到了很多坑,就不一一列出來了,有問題的可以留言討論。

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