Cloud Runをたった3ステップでデプロイしてみた (golang)
Cloud Run とは?
Cloud Run is a managed compute platform that enables you to run stateless containers that are invocable via HTTP requests. Cloud Run is serverless
※ https://cloud.google.com/run/
詳しくは割愛するが、Cloud Functions や App Engine と同じようなサーバーレスで動作するもの。 コンテナを deploy するため、GKE から制御することもできる。

デプロイしてみた
https://cloud.google.com/run/docs/quickstarts/build-and-deploy
を参考に進めていく。
ちなみに、動作環境は下記コンテナ内に行う。
https://hub.docker.com/r/google/cloud-sdk
step1. gcloud の各種設定
$ gcloud components update
$ gcloud components install beta
$ gcloud config set run/region us-central1
※ 2019/04/11 時点では、Cloud Run は beta.
step2. アプリケーションコードの作成
$ mkdir helloworld-go && cd helloworld-go
$ touch helloworld.go Dockerfile
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Print("Hello world received a request.")
target := os.Getenv("TARGET")
if target == "" {
target = "World"
}
fmt.Fprintf(w, "Hello %s!\n", target)
}
func main() {
log.Print("Hello world sample started.")
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
# Use the offical Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.12 as builder
# Copy local code to the container image.
WORKDIR /go/src/github.com/knative/docs/helloworld
COPY . .
# Build the command inside the container.
# (You may fetch or manage dependencies here,
# either manually or with a tool like "godep".)
RUN CGO_ENABLED=0 GOOS=linux go build -v -o helloworld
# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine
# Copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/github.com/knative/docs/helloworld/helloworld /helloworld
# Run the web service on container startup.
CMD ["/helloworld"]
step3. 登録&デプロイ
$ gcloud builds submit --tag gcr.io/[PROJECT-ID]/helloworld
$ gcloud beta run deploy --image gcr.io/[PROJECT-ID]/helloworld

感想
普段私は、個人開発をしているときによくつかっている now.shという Serverless Deployments を使っている。こちらは、v1 のときは docker コンテナを使えていたのだが、v2 になると使えなくなってしまった。ただ、無料で簡単にデプロイできるものを選んでいると、こちらのサービスが最善だと感じていた。
しかし、今回の GoogleCloudNext19 の発表で、CloudRun というものを Beta 版でリリースされたことを知り、早速使ってみた。 何事もなく、今回の手順を進めて一切失敗することなく、3 分以内にデプロイまで進めることができた。 これは、なんて楽で便利なんだと感心してしまった。 また、価格テーブルを見ると、CloudFunctions のようなリクエストによる従量課金制で、月 2 百万リクエストまで無料だ。個人開発においては、AppEngine のようなインスタンス起動時間による料金設定よりも、こちらの方が断然オトク。 これはもう now.sh をやめて、こっちに乗り換えるっきゃない!!