Golang で Tanzu Build Service(TBS) を利用してみる

構築済のTanzu Build Service(TBS) の環境を利用して、TBS を用いてGolang アプリのコンテナイメージを作成してみます。Golang アプリはHello, World を出力するだけのWeb アプリです。


手順

Golang アプリの作成

以下の通り、Hello, World を返すWeb アプリを作成します。
$ mkdir hello-world
$ cd hello-world
$ vim hello-world.go
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World %s!", r.URL.Path[1:])
}

TBS を利用したコンテナイメージ作成

以下の通り、kp CLI を用いて、TBS を利用してコンテナイメージを作成します。
$ kp secret create demo --registry harbor2.<MYDOMAIN> --registry-user devops --namespace demo
$ kp image create demo-go --tag harbor2.<MYDOMAIN>/tanzu/demo-go --local-path ./hello-world/ -n demo --wait
Creating Image...
Uploading to 'harbor2.<MYDOMAIN>/tanzu/demo-go-source'...
	Uploading 'harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:9b968fe872b7978ceca8c5c3ef3d100f33b46f0698c6b847a57c1a506bf5d5d7'
Image "demo-go" created
===> PREPARE
Build reason(s): CONFIG
CONFIG:
	resources: {}
	- source: {}
	+ source:
	+   registry:
	+     image: harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:9b968fe872b7978ceca8c5c3ef3d100f33b46f0698c6b847a57c1a506bf5d5d7
Loading secret for "harbor2.<MYDOMAIN>" from secret "lab-harbor2" at location "/var/build-secrets/lab-harbor2"
Pulling harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:9b968fe872b7978ceca8c5c3ef3d100f33b46f0698c6b847a57c1a506bf5d5d7...
Successfully pulled harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:9b968fe872b7978ceca8c5c3ef3d100f33b46f0698c6b847a57c1a506bf5d5d7 in path "/workspace"
===> DETECT
tanzu-buildpacks/go-dist  0.1.3
tanzu-buildpacks/go-build 0.0.23
===> ANALYZE
Previous image with name "harbor2.<MYDOMAIN>/tanzu/demo-go" not found
===> RESTORE
===> BUILD
Tanzu Go Distribution Buildpack 0.1.3
  Resolving Go version
    Candidate version sources (in priority order):
      <unknown> -> ""

    Selected Go version (using <unknown>): 1.15.5

  Executing build process
    Installing Go 1.15.5
      Completed in 3.842s

Tanzu Go Build Buildpack 0.0.23
  Executing build process
    Running 'go build -o /layers/tanzu-buildpacks_go-build/targets/bin -buildmode pie .'
      Completed in 9.061s

  Assigning launch processes
    web: /layers/tanzu-buildpacks_go-build/targets/bin/workspace
===> EXPORT
  1 package main
Adding layer 'tanzu-buildpacks/go-build:targets'
Adding 1/1 app layer(s)
Adding layer 'launcher'
Adding layer 'config'
Adding layer 'process-types'
Adding label 'io.buildpacks.lifecycle.metadata'
Adding label 'io.buildpacks.build.metadata'
Adding label 'io.buildpacks.project.metadata'
Setting default process type 'web'
*** Images (sha256:727c48f0eff2681585110987f4fb4abe49c94dba69d50e981eeb4fc74ef35205):
      harbor2.<MYDOMAIN>/tanzu/demo-go
      harbor2.<MYDOMAIN>/tanzu/demo-go:b1.20210323.120429
Adding cache layer 'tanzu-buildpacks/go-dist:go'
Adding cache layer 'tanzu-buildpacks/go-build:gocache'
===> COMPLETION
Build successful


出来上がったコンテナイメージをdocker コマンドを用いて動かしてみます。Web ブラウザからlocalhost:8080 にアクセスすると、Hello, World ! と出力されます。
$ docker run --rm -e PORT=8080 -p 8080:8080 harbor2.<MYDOMAIN>/tanzu/demo-go:b1.20210323.120429
Unable to find image 'harbor2.<MYDOMAIN>/tanzu/demo-go:b1.20210323.120429' locally
b1.20210323.120429: Pulling from tanzu/demo-go
e2cf0b38ebfc: Already exists
79742e92f112: Already exists
2cc711fadbef: Already exists
e308557cee7d: Already exists
d7e97aa6a6e0: Already exists
9c70b0bb5809: Already exists
cd0e3ee2b2e0: Pull complete
9b12ccca045e: Pull complete
2877c4e0b5d1: Pull complete
194a00b8d451: Pull complete
5a44e4f7b58d: Pull complete
Digest: sha256:727c48f0eff2681585110987f4fb4abe49c94dba69d50e981eeb4fc74ef35205
Status: Downloaded newer image for harbor2.<MYDOMAIN>/tanzu/demo-go:b1.20210323.120429

Golang アプリを更新し、再度TBS を利用しコンテナイメージ作成

出力されるメッセージ部分のみを修正します。
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HelloServer)
    http.ListenAndServe(":8080", nil)
}

func HelloServer(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, Go with TBS %s!", r.URL.Path[1:])
}

出来上がっているイメージを以下のkp CLI で更新します。コンテナイメージのビルドが上と同じ様に開始されます。
$ kp image patch demo-go --local-path ./hello-world/ -n demo --wait
Patching Image...
	Uploading 'harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:00fa709ae1cd2e0344d7372547ddea26abfc9faf9443e36b28af1a2fc0b23412'
Image "demo-go" patched
===> PREPARE
Build reason(s): CONFIG
CONFIG:
	resources: {}
	source:
	  registry:
	-     image: harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:9b968fe872b7978ceca8c5c3ef3d100f33b46f0698c6b847a57c1a506bf5d5d7
	+     image: harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:00fa709ae1cd2e0344d7372547ddea26abfc9faf9443e36b28af1a2fc0b23412
Loading secret for "harbor2.<MYDOMAIN>" from secret "lab-harbor2" at location "/var/build-secrets/lab-harbor2"
Pulling harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:00fa709ae1cd2e0344d7372547ddea26abfc9faf9443e36b28af1a2fc0b23412...
Successfully pulled harbor2.<MYDOMAIN>/tanzu/demo-go-source@sha256:00fa709ae1cd2e0344d7372547ddea26abfc9faf9443e36b28af1a2fc0b23412 in path "/workspace"
===> DETECT
tanzu-buildpacks/go-dist  0.1.3
tanzu-buildpacks/go-build 0.0.23
===> ANALYZE
Restoring metadata for "tanzu-buildpacks/go-build:targets" from app image
===> RESTORE
===> BUILD
Tanzu Go Distribution Buildpack 0.1.3
  Resolving Go version
    Candidate version sources (in priority order):
      <unknown> -> ""

    Selected Go version (using <unknown>): 1.15.5

  Executing build process
    Installing Go 1.15.5
      Completed in 3.644s

Tanzu Go Build Buildpack 0.0.23
  Executing build process
    Running 'go build -o /layers/tanzu-buildpacks_go-build/targets/bin -buildmode pie .'
      Completed in 8.592s

  Assigning launch processes
    web: /layers/tanzu-buildpacks_go-build/targets/bin/workspace
===> EXPORT
Reusing layers from image 'harbor2.<MYDOMAIN>/tanzu/demo-go@sha256:727c48f0eff2681585110987f4fb4abe49c94dba69d50e981eeb4fc74ef35205'
Adding layer 'tanzu-buildpacks/go-build:targets'
Reusing 1/1 app layer(s)
Reusing layer 'launcher'
Reusing layer 'config'
Reusing layer 'process-types'
Adding label 'io.buildpacks.lifecycle.metadata'
Adding label 'io.buildpacks.build.metadata'
Adding label 'io.buildpacks.project.metadata'
Setting default process type 'web'
*** Images (sha256:8bcdd2a32f010be39bede10406b9be63be44fcd3cab0367544585459157445de):
      harbor2.<MYDOMAIN>/tanzu/demo-go
      harbor2.<MYDOMAIN>/tanzu/demo-go:b2.20210323.120731
Adding cache layer 'tanzu-buildpacks/go-dist:go'
Adding cache layer 'tanzu-buildpacks/go-build:gocache'
===> COMPLETION
Build successful


ビルドのリストでも確認出来ます。出力の下2行がこの記事でビルドしたものになります。
$ kp build list -n demo
BUILD    STATUS     IMAGE                                                                                                                    REASON
1        SUCCESS    harbor2.<MYDOMAIN>/tanzu/spring-petclinic@sha256:616474a2f212cff60233aee8dde19439840361dd314c2f29793b4159f477ae92    CONFIG
2        SUCCESS    harbor2.<MYDOMAIN>/tanzu/spring-petclinic@sha256:b2b0c7bba33f31c29e1fa5d7d02fb7eb6dd91d17fca09fe0307d4446a4c5fcbc    COMMIT
3        SUCCESS    harbor2.<MYDOMAIN>/tanzu/spring-petclinic@sha256:1446b847a4f68f480f6ea61cc8651c05d8a3cca061e7f8e5df33d177f26eafae    COMMIT
4        SUCCESS    harbor2.<MYDOMAIN>/tanzu/spring-petclinic@sha256:f9234789dc53149392278fc8b4a41ea5604e64392335141fa6e40e9ba0bf9d83    STACK
5        SUCCESS    harbor2.<MYDOMAIN>/tanzu/spring-petclinic@sha256:aaa61b9af8e50e1a17cc127a138d5a935b369dcf9bfceb6c77d7fcf7c180405c    BUILDPACK
6        SUCCESS    harbor2.<MYDOMAIN>/tanzu/spring-petclinic@sha256:8c6ad346d135aa4752f864d22501b8e4cc77a5b62a0f8f0d34b58e5f262b4891    STACK
1        SUCCESS    harbor2.<MYDOMAIN>/tanzu/demo-php@sha256:d24663245c7cfbf43c75d635b63684ae79addc3729df07155b757a7c49f53f9f            CONFIG
2        SUCCESS    harbor2.<MYDOMAIN>/tanzu/demo-php@sha256:ef6bfa7f60f1b5977428c77cbd0348468b4a8ef9854fabbfa7a9c12d053df49d            COMMIT
1        SUCCESS    harbor2.<MYDOMAIN>/tanzu/demo-go@sha256:727c48f0eff2681585110987f4fb4abe49c94dba69d50e981eeb4fc74ef35205             CONFIG
2        SUCCESS    harbor2.<MYDOMAIN>/tanzu/demo-go@sha256:8bcdd2a32f010be39bede10406b9be63be44fcd3cab0367544585459157445de             CONFIG


docker コマンドでコンテナイメージがちゃんと更新されているか確認してみます。以下の通り、ちゃんと出力内容が変わっている事が確認出来ました。
$ docker run --rm -e PORT=8080 -p 8080:8080 harbor2.<MYDOMAIN>/tanzu/demo-go:b2.20210323.120731
Unable to find image 'harbor2.<MYDOMAIN>/tanzu/demo-go:b2.20210323.120731' locally
b2.20210323.120731: Pulling from tanzu/demo-go
e2cf0b38ebfc: Already exists
79742e92f112: Already exists
2cc711fadbef: Already exists
e308557cee7d: Already exists
d7e97aa6a6e0: Already exists
9c70b0bb5809: Already exists
e713ddf96074: Pull complete
9b12ccca045e: Pull complete
2877c4e0b5d1: Pull complete
194a00b8d451: Pull complete
5a44e4f7b58d: Pull complete
Digest: sha256:8bcdd2a32f010be39bede10406b9be63be44fcd3cab0367544585459157445de
Status: Downloaded newer image for harbor2.<MYDOMAIN>/tanzu/demo-go:b2.20210323.120731




このブログの人気の投稿