自前のS2Iスクリプトを使ったイメージビルドをプライベートネットワーク上のGitLab CEのリポジトリ上のソースコードからビルド&デプロイする手順と、更にそれをWebhookを使ってpush
をトリガーに自動でビルド&デプロイする設定について。
扱っているソース・やっている内容は前回の記事の続きです。
リポジトリからのS2Iビルド自体は以下の記事と同じです。
リポジトリにソース一式をpushする
GitLab CEはこの子です。
まずGitLab CEでリポジトリを作成。
(以下、普通のGit/GitLab操作です)
とりあえずサンプルなのでpublic
設定で。
ローカルのソースでS2Iした時のファイルをpush
(*.org
は削除した)。
[zaki@codeready s2isample]$ echo "# S2I sample script" > README.md [zaki@codeready s2isample]$ find . ./sockserv.pl ./.s2i ./.s2i/bin ./.s2i/bin/assemble ./.s2i/bin/run ./README.md [zaki@codeready s2isample]$ git init Initialized empty Git repository in /home/zaki/src/s2isample/.git/ [zaki@codeready s2isample]$ git remote add origin https://gitlab-ce.example.org:8443/zaki/s2i-sample.git [zaki@codeready s2isample]$ git add . [zaki@codeready s2isample]$ git commit -m "Initial commit" [master (root-commit) af197a1] Initial commit 4 files changed, 35 insertions(+) create mode 100644 .s2i/bin/assemble create mode 100644 .s2i/bin/run create mode 100644 README.md create mode 100644 sockserv.pl [zaki@codeready s2isample]$ git push -u origin master Username for 'https://gitlab-ce.example.org:8443': zaki Password for 'https://zaki@gitlab-ce.example.org:8443': Counting objects: 8, done. Delta compression using up to 4 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (8/8), 873 bytes | 0 bytes/s, done. Total 8 (delta 0), reused 0 (delta 0) To https://gitlab-ce.example.org:8443/zaki/s2i-sample.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
ここまでは普通です。
これから、このリポジトリのソースを使ってS2Iします。
ビルド&デプロイ
以下、環境はOKD4.4です。
[zaki@okd4-manager ~]$ oc version Client Version: 4.4.0-0.okd-2020-01-28-022517 Server Version: 4.4.0-0.okd-2020-01-28-022517 Kubernetes Version: v1.17.1
作業用プロジェクト
[zaki@okd4-manager ~]$ oc login -u zaki Logged into "https://api.okd4.naru.jp-z.jp:6443" as "zaki" using existing credentials. You don't have any projects. You can try to create a new project, by running oc new-project <projectname> [zaki@okd4-manager ~]$ oc new-project s2i-git-perl Now using project "s2i-git-perl" on server "https://api.okd4.naru.jp-z.jp:6443". You can add applications to this project with the 'new-app' command. For example, try: oc new-app ruby~https://github.com/sclorg/ruby-ex.git to build a new example application in Python. Or use kubectl to deploy a simple Kubernetes application: kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
Gitリポジトリの証明書secretを作成
内容はここと一緒です。
[zaki@okd4-manager ~]$ oc create secret generic gitlabcert --from-file=ca.crt=gitlab-cert/gitlab-ce.example.org.crt secret/gitlabcert created [zaki@okd4-manager ~]$ oc describe secret gitlabcert Name: gitlabcert Namespace: s2i-git-perl Labels: <none> Annotations: <none> Type: Opaque Data ==== ca.crt: 1822 bytes
new-app
これは足りない例
$ oc new-app https://gitlab-ce.example.org:8443/zaki/s2i-sample.git --source-secret=gitlabcert
カスタムスクリプトで実装している関係で、これだとS2Iによる言語の検知が動作しない(Perlならこのファイルがあるはず、という条件を満たしていない)ので、イメージを指定。
言語の検出については以下のドキュメント参照。
- ver 3.11: 言語検出 / 第2章 アプリケーションライフサイクル管理 OpenShift Container Platform 3.11 | Red Hat Customer Portal
- ver 4.3: 2.3.1.4. 言語の検出 / 2.3. CLI を使用したアプリケーションの作成 4.3 | Red Hat Customer Portal
イメージを指定するには、リポジトリの前にイメージ名:tag~
を付与。
[zaki@okd4-manager ~]$ oc new-app perl:5.26~https://gitlab-ce.example.org:8443/zaki/s2i-sample.git --source-secret=gitlabcert --> Found image 57aa6a9 (3 weeks old) in image stream "openshift/perl" under tag "5.26" for "perl:5.26" Apache 2.4 with mod_perl/5.26 ----------------------------- Perl 5.26 available as container is a base platform for building and running various Perl 5.26 applications and frameworks. Perl is a high-level programming language with roots in C, sed, awk and shell scripting. Perl is good at handling processes and files, and is especially good at handling text. Perl's hallmarks are practicality and efficiency. While it is used to do a lot of different things, Perl's most common applications are system administration utilities and web programming. Tags: builder, perl, perl526 * A source build using source code from https://gitlab-ce.example.org:8443/zaki/s2i-sample.git will be created * The resulting image will be pushed to image stream tag "s2i-sample:latest" * Use 'oc start-build' to trigger a new build * This image will be deployed in deployment config "s2i-sample" * Port 8080/tcp will be load balanced by service "s2i-sample" * Other containers can access this service through the hostname "s2i-sample" --> Creating resources ... imagestream.image.openshift.io "s2i-sample" created buildconfig.build.openshift.io "s2i-sample" created deploymentconfig.apps.openshift.io "s2i-sample" created service "s2i-sample" created --> Success Build scheduled, use 'oc logs -f bc/s2i-sample' to track its progress. Application is not exposed. You can expose services to the outside world by executing one or more of the commands below: 'oc expose svc/s2i-sample' Run 'oc status' to view your app.
各種リソースが作成される。
[zaki@okd4-manager ~]$ oc get all NAME READY STATUS RESTARTS AGE pod/s2i-sample-1-build 0/1 Init:0/2 0 6s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/s2i-sample ClusterIP 172.30.154.146 <none> 8080/TCP 6s NAME REVISION DESIRED CURRENT TRIGGERED BY deploymentconfig.apps.openshift.io/s2i-sample 0 1 0 config,image(s2i-sample:latest) NAME TYPE FROM LATEST buildconfig.build.openshift.io/s2i-sample Source Git 1 NAME TYPE FROM STATUS STARTED DURATION build.build.openshift.io/s2i-sample-1 Source Git Pending NAME IMAGE REPOSITORY TAGS UPDATED imagestream.image.openshift.io/s2i-sample image-registry.openshift-image-registry.svc:5000/s2i-git-perl/s2i-sample
Gitからソースをpullし、イメージのビルドもnew-app
の実行の流れで行われる。
[zaki@okd4-manager ~]$ oc get buildconfig NAME TYPE FROM LATEST s2i-sample Source Git 1 [zaki@okd4-manager ~]$ oc get build NAME TYPE FROM STATUS STARTED DURATION s2i-sample-1 Source Git Pending [zaki@okd4-manager ~]$
しばらくまてば、、
[zaki@okd4-manager ~]$ oc logs build/s2i-sample-1 Cloning "https://gitlab-ce.example.org:8443/zaki/s2i-sample.git" ... Commit: af197a1d5eff7a46e46cf227d2e1797642d520c7 (Initial commit) Author: zaki <zaki.hmkc@gmail.com> Date: Fri Feb 21 15:32:09 2020 +0900 Caching blobs under "/var/cache/blobs". Getting image source signatures Copying blob sha256:455ea8ab06218495bbbcb14b750a0d644897b24f8c5dcf9e8698e27882583412 Copying blob sha256:01ace4f1c1164bbc4513b616c5cff62585fde1dc198b7469c5bc5bc5355941da Copying blob sha256:4e466ee901fb8e61b18a0b1c172c781c8cb3609060c441a3217b9ebd2cb971cd Copying blob sha256:bb13d92caffa705f32b8a7f9f661e07ddede310c6ccfa78fb53a49539740e29b Copying blob sha256:84e620d0abe585d05a7bed55144af0bc5efe083aed05eac1e88922034ddf1ed2 Copying config sha256:57aa6a9e95d366288608f9e224d70922eb6803d78a2e807338cb954d6d1d0629 Writing manifest to image destination Storing signatures Generating dockerfile with builder image image-registry.openshift-image-registry.svc:5000/openshift/perl@sha256:4dde5621a54dc0ef542d015b42a9e30d6ed45a185ceec7ae81be5b0da43c9e2c STEP 1: FROM image-registry.openshift-image-registry.svc:5000/openshift/perl@sha256:4dde5621a54dc0ef542d015b42a9e30d6ed45a185ceec7ae81be5b0da43c9e2c STEP 2: LABEL "io.openshift.build.commit.ref"="master" "io.openshift.build.commit.message"="Initial commit" "io.openshift.build.image"="image-registry.openshift-image-registry.svc:5000/openshift/perl@sha256:4dde5621a54dc0ef542d015b42a9e30d6ed45a185ceec7ae81be5b0da43c9e2c" "io.openshift.build.commit.author"="zaki <zaki.hmkc@gmail.com>" "io.openshift.build.commit.date"="Fri Feb 21 15:32:09 2020 +0900" "io.openshift.build.commit.id"="af197a1d5eff7a46e46cf227d2e1797642d520c7" STEP 3: ENV OPENSHIFT_BUILD_NAME="s2i-sample-1" OPENSHIFT_BUILD_NAMESPACE="s2i-git-perl" OPENSHIFT_BUILD_SOURCE="https://gitlab-ce.example.org:8443/zaki/s2i-sample.git" OPENSHIFT_BUILD_COMMIT="af197a1d5eff7a46e46cf227d2e1797642d520c7" STEP 4: USER root STEP 5: COPY upload/scripts /tmp/scripts STEP 6: COPY upload/src /tmp/src STEP 7: RUN chown -R 1001:0 /tmp/scripts /tmp/src STEP 8: USER 1001 STEP 9: RUN /tmp/scripts/assemble STEP 10: CMD /tmp/scripts/run STEP 11: COMMIT temp.builder.openshift.io/s2i-git-perl/s2i-sample-1:96c53677 Getting image source signatures Copying blob sha256:35817540a17b5b90cb4426078d53813b16e70c75e1cce3db116d2fbbca7fbf10 Copying blob sha256:c7fbe90ae90e9c6452e5d809f069907907e6f32532565104921f600fb956306c Copying blob sha256:74d760a83a4b8bcb3d01e7726e06c11469f0bb2ce4645474cb91a607c27bf482 Copying blob sha256:f1cc3f0b20053e48dc0f6505734d2f476e4fcd975df9791164bd749af9332401 Copying blob sha256:8995923ae10d45d1b79c0876141c62b891d258a68c8fc2ed53743acbc076ed5d Copying blob sha256:bd6b846d7f3b84f9d8e9de2dd380e34a2baff06916e19fbf71394afb66c43736 Copying config sha256:f37c27b818ce7f115147e306f00c8e751af29e9202b92a621f5c6d393e2c2d4a Writing manifest to image destination Storing signatures f37c27b818ce7f115147e306f00c8e751af29e9202b92a621f5c6d393e2c2d4a f37c27b818ce7f115147e306f00c8e751af29e9202b92a621f5c6d393e2c2d4a Pushing image image-registry.openshift-image-registry.svc:5000/s2i-git-perl/s2i-sample:latest ... Getting image source signatures Copying blob sha256:bd6b846d7f3b84f9d8e9de2dd380e34a2baff06916e19fbf71394afb66c43736 Copying blob sha256:01ace4f1c1164bbc4513b616c5cff62585fde1dc198b7469c5bc5bc5355941da Copying blob sha256:455ea8ab06218495bbbcb14b750a0d644897b24f8c5dcf9e8698e27882583412 Copying blob sha256:bb13d92caffa705f32b8a7f9f661e07ddede310c6ccfa78fb53a49539740e29b Copying blob sha256:4e466ee901fb8e61b18a0b1c172c781c8cb3609060c441a3217b9ebd2cb971cd Copying blob sha256:84e620d0abe585d05a7bed55144af0bc5efe083aed05eac1e88922034ddf1ed2 Copying config sha256:f37c27b818ce7f115147e306f00c8e751af29e9202b92a621f5c6d393e2c2d4a Writing manifest to image destination Storing signatures Successfully pushed image-registry.openshift-image-registry.svc:5000/s2i-git-perl/s2i-sample@sha256:c0cb8460b2dad54b76607978a1267313f22ad3383f325e56a13a7f764ffab4ab Push successful
イメージのビルドが完了し、内部イメージレジストリへpushされる。
[zaki@okd4-manager ~]$ oc get build NAME TYPE FROM STATUS STARTED DURATION s2i-sample-1 Source Git@af197a1 Complete About a minute ago 1m50s [zaki@okd4-manager ~]$ [zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-build 0/1 Completed 0 2m s2i-sample-1-deploy 0/1 ContainerCreating 0 9s [zaki@okd4-manager ~]$ [zaki@okd4-manager ~]$ oc get is NAME IMAGE REPOSITORY TAGS UPDATED s2i-sample image-registry.openshift-image-registry.svc:5000/s2i-git-perl/s2i-sample latest 13 seconds ago
さらに続けてdeploy podも動き出す。
[zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-585bz 0/1 ContainerCreating 0 13s s2i-sample-1-build 0/1 Completed 0 2m21s s2i-sample-1-deploy 1/1 Running 0 30s
アプリケーションpodがRunningになる。
[zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-585bz 1/1 Running 0 30s s2i-sample-1-build 0/1 Completed 0 2m38s s2i-sample-1-deploy 0/1 Completed 0 47s
アプリケーションpodが動作したので、exposeする。
[zaki@okd4-manager ~]$ oc get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE s2i-sample ClusterIP 172.30.154.146 <none> 8080/TCP 3m16s [zaki@okd4-manager ~]$ [zaki@okd4-manager ~]$ oc expose svc/s2i-sample route.route.openshift.io/s2i-sample exposed [zaki@okd4-manager ~]$ oc get route NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD s2i-sample s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp s2i-sample 8080-tcp None [zaki@okd4-manager ~]$
アクセス
[zaki@okd4-manager ~]$ curl http://s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp curl: (52) Empty reply from server [zaki@okd4-manager ~]$ curl -v http://s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp * About to connect() to s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp port 80 (#0) * Trying 172.16.0.50... * Connected to s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp (172.16.0.50) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp > Accept: */* > * Empty reply from server * Connection #0 to host s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp left intact curl: (52) Empty reply from server
ん…?
[zaki@okd4-manager s2isample]$ oc rsh s2i-sample-1-585bz bash bash-4.2$ curl http://localhost:8080 カレーは汗をかくのでスポーツ curl: (56) Recv failure: Connection reset by peer bash-4.2$
接続まではできてるし、中のアプリは動いてはいるね。
[zaki@okd4-manager ~]$ curl -v http://s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp * About to connect() to s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp port 80 (#0) * Trying 172.16.0.50... * Connected to s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp (172.16.0.50) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp > Accept: */* > < HTTP/1.1 502 Bad Gateway < content-length: 107 < cache-control: no-cache < content-type: text/html < connection: close < <html><body><h1>502 Bad Gateway</h1> The server returned an invalid or incomplete response. </body></html> * Closing connection 0
たまにこういうレスポンスになる。
なんでや…?
そう。ソースコードを見直して思い出したけど、これ「TCPソケットサーバー」であって、別にHTTP喋ってないんですよね。
クライアントから接続があったら、文字列を接続元にただprintしてるだけで、HTTP/1.1 200 OK
とかContent-type
とか何も言わずにぶっきらぼうに要件だけ喋っているコミュ障なので、routerで動作しているHAProxyさんがさばいてない状態。
(OpenShiftのrouteは、HTTP/HTTPSを処理する)
ということでバージョン1は「OpenShift的にpodのビルド&デプロイはできてる」けど、「アプリケーションは動作していない」ので、修正します。
※ アプリケーションとしては動いてないけど、oc new-app
のみでpodのビルド&デプロイが完了してる。
リビルド
コード修正・push
というわけで、ソケットサーバーにHTTPを喋らせる処理を追加。
[zaki@codeready s2isample]$ git diff diff --git a/sockserv.pl b/sockserv.pl index bb81d2b..0843941 100644 --- a/sockserv.pl +++ b/sockserv.pl @@ -16,6 +16,8 @@ die "IO::Socket $!" unless $server; for (;;) { if (my $client = $server->accept()) { print "connected from " . $client->peerhost() . "\n"; + print $client "HTTP/1.1 200 OK\n"; + print $client "content-type: text/plain\n\n"; print $client "カレーは汗をかくのでスポーツ\n"; close $client; print "disconected...\n";
commit
/push
する。
イメージのリビルド
BuildConfigにリポジトリの情報は含まれている。
[zaki@okd4-manager ~]$ oc describe bc s2i-sample Name: s2i-sample Namespace: s2i-git-perl Created: 2 hours ago Labels: app=s2i-sample app.kubernetes.io/component=s2i-sample app.kubernetes.io/instance=s2i-sample Annotations: openshift.io/generated-by=OpenShiftNewApp Latest Version: 1 Strategy: Source URL: https://gitlab-ce.example.org:8443/zaki/s2i-sample.git Source Secret: gitlabcert From Image: ImageStreamTag openshift/perl:5.26 Output to: ImageStreamTag s2i-sample:latest : :
なので、リビルドするには、start-build
でBuildConfigを指定すればOK
[zaki@okd4-manager ~]$ oc start-build s2i-sample build.build.openshift.io/s2i-sample-2 started [zaki@okd4-manager ~]$ [zaki@okd4-manager ~]$ [zaki@okd4-manager ~]$ oc get build NAME TYPE FROM STATUS STARTED DURATION s2i-sample-1 Source Git@af197a1 Complete 2 hours ago 1m50s s2i-sample-2 Source Git Pending [zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-585bz 1/1 Running 0 98m s2i-sample-1-build 0/1 Completed 0 100m s2i-sample-1-deploy 0/1 Completed 0 98m s2i-sample-2-build 0/1 Init:0/2 0 5s
リポジトリからソースをpull
し、イメージをビルド完了すれば、deploy podが起動し、アプリケーションpodを新しいイメージからデプロイする。
[zaki@okd4-manager ~]$ oc get build NAME TYPE FROM STATUS STARTED DURATION s2i-sample-1 Source Git@af197a1 Complete 2 hours ago 1m50s s2i-sample-2 Source Git@ae51ef0 Complete About a minute ago 40s [zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-585bz 1/1 Terminating 0 99m s2i-sample-1-build 0/1 Completed 0 101m s2i-sample-1-deploy 0/1 Completed 0 99m s2i-sample-2-build 0/1 Completed 0 61s s2i-sample-2-deploy 1/1 Running 0 21s s2i-sample-2-dzmwb 1/1 Running 0 12s
アプリケーションpodがデプロイまで完了し、Running状態になる。
さて、、、
[zaki@okd4-manager ~]$ curl http://s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp カレーは汗をかくのでスポーツ [zaki@okd4-manager ~]$ curl -v http://s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp * About to connect() to s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp port 80 (#0) * Trying 172.16.0.50... * Connected to s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp (172.16.0.50) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.29.0 > Host: s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp > Accept: */* > < HTTP/1.1 200 OK < content-type: text/plain < connection: close < カレーは汗をかくのでスポーツ * Closing connection 0 [zaki@okd4-manager ~]$
ちゃんと動きました(・∀・)
Webhook
最後にWebhookの設定。
これは、BuildConfigのトリガーにWebhookを設定することで、リポジトリにpushすることでpodのリビルド&デプロイを行うというもの。
- ver 3.11: Webhook のトリガー / 第8章 ビルド OpenShift Container Platform 3.11 | Red Hat Customer Portal
- ver 4.3: 第8章 ビルドのトリガーおよび変更 4.3 | Red Hat Customer Portal
なお、トリガーの設定はoc set trigger
でできる。
初期状態
[zaki@okd4-manager ~]$ oc set triggers bc s2i-sample NAME TYPE VALUE AUTO buildconfigs/s2i-sample config true buildconfigs/s2i-sample image openshift/perl:5.26 true buildconfigs/s2i-sample webhook <secret> buildconfigs/s2i-sample github <secret>
BuildConfigに設定を追加する
[zaki@okd4-manager ~]$ oc set triggers bc s2i-sample --from-gitlab buildconfig.build.openshift.io/s2i-sample triggers updated
BuildConfigの設定はこれだけ。
[zaki@okd4-manager ~]$ oc set triggers bc s2i-sample NAME TYPE VALUE AUTO buildconfigs/s2i-sample config true buildconfigs/s2i-sample image openshift/perl:5.26 true buildconfigs/s2i-sample webhook <secret> buildconfigs/s2i-sample github <secret> buildconfigs/s2i-sample gitlab <secret>
gitlab
の設定が追加されている。
BuildConfigの内容は-o yaml
で確認すれば以下の部分が追加されている。
triggers: - github: secret: ******************** type: GitHub - generic: secret: ******************** type: Generic - type: ConfigChange - imageChange: lastTriggeredImageID: image-registry.openshift-image-registry.svc:5000/openshift/perl@sha256:4dde5621a54dc0ef542d015b42a9e30d6ed45a185ceec7ae81be5b0da43c9e2c type: ImageChange + - gitlab: + secret: ******************** + type: GitLab
このsecret
の値をGitLabの設定で使用する。
また、BuildConfigをoc describe
で確認すると、以下のように出力される。
Build Run Policy: Serial Triggered by: Config, ImageChange Webhook GitLab: URL: https://api.okd4.naru.jp-z.jp:6443/apis/build.openshift.io/v1/namespaces/s2i-git-perl/buildconfigs/s2i-sample/webhooks/<secret>/gitlab Webhook GitHub: URL: https://api.okd4.naru.jp-z.jp:6443/apis/build.openshift.io/v1/namespaces/s2i-git-perl/buildconfigs/s2i-sample/webhooks/<secret>/github Webhook Generic: URL: https://api.okd4.naru.jp-z.jp:6443/apis/build.openshift.io/v1/namespaces/s2i-git-perl/buildconfigs/s2i-sample/webhooks/<secret>/generic AllowEnv: false Builds History Limit: Successful: 5 Failed: 5
この中の、Webhook GitLab
のURLの<secret>
となっている個所に、前述-o yaml
で確認したsecret
の値に置き換えたURLをGitLab CEに設定する。
GitLabにWebhookの設定追加
初めに作成したs2i-sampleプロジェクトをGitLab CEで開き、メニューの"Settings"->"Integrations"を開く。
設定画面のURLに、前述のsecret込みURLを入力する。
(字が似てるので、間違えてGitHubの値を混ぜないように注意。私はこれで時間を溶かしました)
それ以外にTriggerのチェックは設定変更無し・Enable SSL verification
は(OpenShiftクラスタの証明書をGitLab CEリポジトリ側に設定していなければ)チェックを外し、ページの中央付近にある"Add webhook"を押下。
※ GitLab CEでは、ローカルネットワークでのWebhookはデフォルト禁止になっているため、以下のエラーが出る場合は、admin設定で制限を外す必要がある。
Url is blocked: Requests to the local network are not allowed
これにチェックしておく。
Webhookが追加されたら、Push events
のTestを行う。
設定がうまくいっていれば、200 OKになる。
これで準備OK
というかこのTestでダミーのpush eventが発行されているので、OpenShift側ではBuildConfigの設定によってこのイベントを検知し、リポジトリからソースをpull
してイメージのリビルド・デプロイが行われている。
[zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-build 0/1 Completed 0 3h16m s2i-sample-1-deploy 0/1 Completed 0 3h14m s2i-sample-2-build 0/1 Completed 0 95m s2i-sample-2-deploy 0/1 Completed 0 95m s2i-sample-3-build 0/1 Completed 0 4m27s s2i-sample-3-deploy 0/1 Completed 0 3m44s s2i-sample-3-nwsxg 1/1 Running 0 3m35s [zaki@okd4-manager ~]$ oc get dc NAME REVISION DESIRED CURRENT TRIGGERED BY s2i-sample 3 1 1 config,image(s2i-sample:latest)
バージョンが2
になっている。
コードを修正しpushする
というわけで本番。
また分かりやすいように文言を更新。
[zaki@codeready s2isample]$ git diff diff --git a/sockserv.pl b/sockserv.pl index 0843941..7961e7e 100644 --- a/sockserv.pl +++ b/sockserv.pl @@ -18,7 +18,7 @@ for (;;) { print "connected from " . $client->peerhost() . "\n"; print $client "HTTP/1.1 200 OK\n"; print $client "content-type: text/plain\n\n"; - print $client "カレーは汗をかくのでスポーツ\n"; + print $client "カレーは時に甘く時に辛いので人生\n"; close $client; print "disconected...\n"; }
commit
& push
する。
[zaki@codeready s2isample]$ git add sockserv.pl [zaki@codeready s2isample]$ git commit -m "update: 文言変更" [zaki@codeready s2isample]$ git push
これだけ。
Gitにpush
するだけで、新しいBuildが動き出す
[zaki@okd4-manager ~]$ oc get build NAME TYPE FROM STATUS STARTED DURATION s2i-sample-1 Source Git@af197a1 Complete 3 hours ago 1m50s s2i-sample-2 Source Git@ae51ef0 Complete 2 hours ago 40s s2i-sample-3 Source Git@af197a1 Complete 10 minutes ago 42s s2i-sample-4 Source Git@5c7b4f5 Pending [zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-build 0/1 Completed 0 3h22m s2i-sample-1-deploy 0/1 Completed 0 3h20m s2i-sample-2-build 0/1 Completed 0 101m s2i-sample-2-deploy 0/1 Completed 0 101m s2i-sample-3-build 0/1 Completed 0 10m s2i-sample-3-deploy 0/1 Completed 0 9m46s s2i-sample-3-nwsxg 1/1 Running 0 9m37s s2i-sample-4-build 0/1 Init:1/2 0 10s
しばらく待てば
[zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-build 0/1 Completed 0 3h22m s2i-sample-1-deploy 0/1 Completed 0 3h20m s2i-sample-2-build 0/1 Completed 0 102m s2i-sample-2-deploy 0/1 Completed 0 101m s2i-sample-3-build 0/1 Completed 0 11m s2i-sample-3-deploy 0/1 Completed 0 10m s2i-sample-3-nwsxg 1/1 Running 0 10m s2i-sample-4-build 0/1 Completed 0 45s s2i-sample-4-deploy 0/1 ContainerCreating 0 3s [zaki@okd4-manager ~]$ [zaki@okd4-manager ~]$ oc get pod NAME READY STATUS RESTARTS AGE s2i-sample-1-build 0/1 Completed 0 3h23m s2i-sample-1-deploy 0/1 Completed 0 3h21m s2i-sample-2-build 0/1 Completed 0 102m s2i-sample-2-deploy 0/1 Completed 0 102m s2i-sample-3-build 0/1 Completed 0 11m s2i-sample-3-deploy 0/1 Completed 0 10m s2i-sample-3-nwsxg 1/1 Terminating 0 10m s2i-sample-4-build 0/1 Completed 0 71s s2i-sample-4-deploy 0/1 Completed 0 29s s2i-sample-4-mk5s4 1/1 Running 0 20s
また新しいpodがRunningになった。
webアクセスすると
[zaki@okd4-manager ~]$ curl http://s2i-sample-s2i-git-perl.apps.okd4.naru.jp-z.jp カレーは時に甘く時に辛いので人生
新しいバージョンになりました。
まとめ
ということで、Webhookの設定を行ったBuildConfigとGitリポジトリを組み合わせることで、oc
コマンドを使ったオペレーション無しに任意のアプリケーションをビルド&デプロイできるようになりました。
またGitLab CEを使うことで、クローズドな環境のリポジトリでも利用できます。
これがGitOpsの入り口なんですね…