🐼

【OpenShift】S2IビルドでProfileを指定してJavaアプリケーションをデプロイ

に公開

概要

以前、binaryビルドによるJavaアプリケーションのデプロイを試しました。

https://zenn.dev/ramenpanda/articles/8cb9e1589fcdf9

OpenShiftには複数のビルド戦略があります。

ビルド方法 説明
Dockerビルド Dockerfileをビルドすることでコンテナイメージを作成
S2I アプリケーションのソースコードのビルドからコンテナイメージの作成までを実施
Pipeline パイプラインの定義に従いコンテナイメージを作成
Custom ビルドプロセス全体を担当するビルダーイメージを指定し、指定したビルダーイメージにおいてコンテナイメージを作成

https://docs.redhat.com/ja/documentation/openshift_container_platform/4.2/html/builds/understanding-image-builds#build-strategy-s2i_understanding-image-builds

今回は、S2Iビルドを試してみようと思います。

やりたいこと

私のアプリケーションはローカル・staging環境の差異を環境設定ファイルとして持っていて、OpenShiftのstaging環境にデプロイする場合は、maven引数でProfileを指定してあげたいです。

  • pom.xml
<profile>
       <id>staging</id>
       <build>
         <plugins>
           <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-war-plugin</artifactId>
             <configuration>
               <webResources>
                 <webResource>
                   <directory>src/environment/staging/resources</directory>
                   <targetPath>WEB-INF/classes/</targetPath>
                 </webResource>
                 <webResource>
                   <directory>src/main/resources</directory>
                   <targetPath>WEB-INF/classes/</targetPath>
                 </webResource>
               </webResources>
             </configuration>
           </plugin>
         </plugins>
       </build>
     </profile>
 mvn clean package -P staging -DskipTests=true

実施手順

  • GitにJavaアプリケーションを用意します。

サンプルアプリケーションの中身については本題とはずれるため割愛しますが、api/helloにアクセスするとhelloと表示されるものにしました。

  • S2Iビルドの実行
    以下のコマンドを実行します。
oc new-app registry.redhat.io/jboss-eap-7/eap74-openjdk11-openshift-rhel8:latest~<リポジトリのURL>#<ブランチ名> --strategy=source
  • すると、以下のようにパッケージのダウンロードが始まり、ビルドが開始されます。
$ oc logs -f hoge-1-build

Defaulted container "sti-build" out of: sti-build, git-clone (init), manage-dockerfile (init)
time="2025-05-04T02:30:07Z" level=info msg="Not using native diff for overlay, this may cause degraded performance for building images: kernel has CONFIG_OVERLAY_FS_REDIRECT_DIR enabled"
I0504 02:30:07.995558       1 defaults.go:112] Defaulting to storage driver "overlay" with options [mountopt=metacopy=on].
Caching blobs under "/var/cache/blobs".
Trying to pull registry.redhat.io/jboss-eap-7/eap74-openjdk11-openshift-rhel8@sha256:9e0df1a68c1941db963d028e262664c0dff08eac16b3f0b6d9d019e3db4ef9c4...
Getting image source signatures
Copying blob sha256:63e76d1f7a4bdf8d9f6e6f489c7f5711396b58ce48fb7d1895001c4fadbe9193
Copying blob sha256:79c20e727b94ea36ae8776eb9e4065b60dc1d396564a6a91ebb6ee334dfb5cea
Copying config sha256:8d8e20666414c1cfc90a38ade6b0282f36ef614c084e6c3904227913e551b367
Writing manifest to image destination
Generating dockerfile with builder image registry.redhat.io/jboss-eap-7/eap74-openjdk11-openshift-rhel8@sha256:9e0df1a68c1941db963d028e262664c0dff08eac16b3f0b6d9d019e3db4ef9c4
Adding transient rw bind mount for /run/secrets/rhsm
STEP 1/9: FROM registry.redhat.io/jboss-eap-7/eap74-openjdk11-openshift-rhel8@sha256:9e0df1a68c1941db963d028e262664c0dff08eac16b3f0b6d9d019e3db4ef9c4
STEP 2/9: LABEL "io.openshift.build.commit.id"="08a6213d71b8589feec40996c99cf2a296deed9a"       "io.openshift.build.commit.ref"="develop"       "io.openshift.build.commit.message"="テスト"       "io.openshift.build.source-location"="http://192.168.40.217:8080/gitlab/my-repository/hoge.git"       "io.openshift.s2i.destination"="/tmp"       "io.openshift.build.image"="registry.redhat.io/jboss-eap-7/eap74-openjdk11-openshift-rhel8@sha256:9e0df1a68c1941db963d028e262664c0dff08eac16b3f0b6d9d019e3db4ef9c4"       "io.openshift.build.commit.author"="hoge <k>"       "io.openshift.build.commit.date"="Sun May 4 10:43:03 2025 +0900"
STEP 3/9: ENV OPENSHIFT_BUILD_NAME="hoge-3"     OPENSHIFT_BUILD_NAMESPACE="s2i-sample"     OPENSHIFT_BUILD_SOURCE="http://192.168.40.217:8080/gitlab/my-repository/hoge.git"     OPENSHIFT_BUILD_REFERENCE="develop"     OPENSHIFT_BUILD_COMMIT="08a6213d71b8589feec40996c99cf2a296deed9a"    
STEP 4/9: USER root
STEP 5/9: COPY upload/src /tmp/src
STEP 6/9: RUN chown -R 185:0 /tmp/src
STEP 7/9: USER 185
STEP 8/9: RUN /usr/local/s2i/assemble
INFO Performing Maven build in /tmp/src
INFO Using MAVEN_OPTS -XX:+UseParallelOldGC -XX:MinHeapFreeRatio=10 -XX:MaxHeapFreeRatio=20 -XX:GCTimeRatio=4 -XX:AdaptiveSizePolicyWeight=90 -XX:+ExitOnOutOfMemoryError
INFO Using Apache Maven 3.6.2 (Red Hat 3.6.2-9)
Maven home: /usr/share/maven
Java version: 11.0.25, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.25.0.9-2.el8.x86_64
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "5.14.0-427.57.1.el9_4.x86_64", arch: "amd64", family: "unix"
INFO Running 'mvn clean package -DskipTests=true --batch-mode -Djava.net.preferIPv4Stack=true -s /home/jboss/.m2/settings.xml -Dmaven.repo.local=/tmp/artifacts/m2  package'
[INFO] Scanning for projects...
[INFO] Downloading from local-galleon-repository: file:///opt/jboss/container/wildfly/s2i/galleon/galleon-m2-repository/org/jboss/arquillian/arquillian-bom/1.7.0.Alpha10/arquillian-bom-1.7.0.Alpha10.pom
[INFO] Downloading from central: https://repo1.maven.org/maven2/org/jboss/arquillian/arquillian-bom/1.7.0.Alpha10/arquillian-bom-1.7.0.Alpha10.pom
[INFO] Downloaded from central: https://repo1.maven.org/maven2/org/jboss/arquillian/arquillian-bom/1.7.0.Alpha10/arquillian-bom-1.7.0.Alpha10.pom (10 kB at 11 kB/s)
[INFO] Downloading from local-galleon-repository: file:///opt/jboss/container/wildfly/s2i/galleon/galleon-m2-repository/org/jboss/shrinkwrap/shrinkwrap-bom/1.2.6/shrinkwrap-bom-1.2.6.pom
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/shrinkwrap-bom/1.2.6/shrinkwrap-bom-1.2.6.pom
[INFO] Downloaded from central: https://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/shrinkwrap-bom/1.2.6/shrinkwrap-bom-1.2.6.pom (4.0 kB at 21 kB/s)
[INFO] Downloading from local-galleon-repository: file:///opt/jboss/container/wildfly/s2i/galleon/galleon-m2-repository/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-bom/3.1.4/shrinkwrap-resolver-bom-3.1.4.pom
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-bom/3.1.4/shrinkwrap-resolver-bom-3.1.4.pom
[INFO] Downloaded from central: https://repo.maven.apache.org/maven2/org/jboss/shrinkwrap/resolver/shrinkwrap-resolver-bom/3.1.4/shrinkwrap-resolver-bom-3.1.4.pom (7.0 kB at 137 kB/s)
[INFO] Downloading from local-galleon-repository: file:///opt/jboss/container/wildfly/s2i/galleon/galleon-m2-repository/org/apache/maven/maven/3.6.3/maven-3.6.3.pom
[INFO] Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/3.6.3/maven-3.6.3.pom
  • new-appで起動されたpodは、Profileが指定されていないためstaging環境の設定が効いていません。一度podを削除し、buildconfigにProfile指定の環境設定を追加します。(※oc new-appの際に -e MAVEN_ARGS="clean package -P staging -DskipTests=trueを指定する方法も試したのですがうまくいかず断念)
oc set env bc/hoge  MAVEN_ARGS="clean package -P staging -DskipTests=true"
  • buildconfigを確認すると、sourceStrategyにビルド設定が適用されています。
$ oc get bc hoge -o yaml
  ...
  strategy:
    sourceStrategy:
      env:
      - name: MAVEN_ARGS
        value: clean package -P staging -DskipTests=true
  • この状態で再度ビルドします。
oc start-build hoge
  • 再度ビルドが始まり、ビルド設定が適用されていることが確認できました。
$ oc logs -f hoge-2-build
...
OPENSHIFT_BUILD_COMMIT="08a6213d71b8589feec40996c99cf2a296deed9a"     MAVEN_ARGS="clean package -P staging -DskipTests=true"
...
STEP 4/9: USER root
  • アプリケーションへのアクセスを確認し、staging向けの設定でS2Iビルドによるデプロイができたことが確認できました。
oc expose svc hoge

感想

  • S2Iビルドは、ビルドからデプロイまでを一貫して行なってくれるのは便利に感じました。
  • しかし、今回の方法ではビルドのたびにパッケージダウンロードが走ると時間がかかってしまいます。
  • 次回はこの課題を解決する方法について考えてみようと思います。

Discussion