Cloud Runとは
GCPが提供するフルマネージドなコンテナ実行環境です。
11/15にGAになりました。
(11/20時点では日本語のドキュメントは対応が遅れていてベータ版という記載があります。)
AWSではFargateという似たような?サービスがあります。
2019/12/6追記
Cloud RunとCloud Run on GKEの違いが開発者のツイッターで記載されていました。
やはりツイッターは時代の最先端を取得するためのツール
https://twitter.com/ahmetb/status/1116041166359654400
Cloud RunはGoogle社内で使われているインフラが使われているのでBorg(現Omega?)がバックグラウンドだそうです。
対してCloud Run on GKEはKnativeで動いているようです。
公式
Cloud Run(日本語)
Cloud Run(英語)
サーバーレスでコンテナを実行できる
GCPがインフラについては提供してくれるのでコンテナの作成に注力できます。Knativeから作成されている
knativeについてはこちら(knative)
Cloud Runを実行してみる
公式のチュートリアルに従って実行してみました。
チュートリアル
作成したソース
$ export PROJECT_ID=hogehoge
$ make deploy_cloud
$ make run_cloud
サンプルなのにマルチステージビルドしてて意識高い
公式からの引用 Quickstart: Build and Deploy
# Use the official 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.13 as builder
# Create and change to the app directory.
WORKDIR /app
# Retrieve application dependencies.
# This allows the container build to reuse cached dependencies.
COPY go.* ./
RUN go mod download
# Copy local code to the container image.
COPY . ./
# Build the binary.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server
# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates
# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server /server
# Run the web service on container startup.
CMD ["/server"]
デプロイが完了すると自動でリビジョンが切られたり、トラフィックの切り替わりが行われます。
デプロイ
コンテナのビルドは行なってあります。
コマンドラインからの実行
gcloud run deploy --region=asia-northeast1 --image gcr.io/$PROJECT_ID/go-server --platform=managed
Service name (go-server): go-server #go-serverと入力
Allow unauthenticated invocations to [go-server] (y/N)? y #yと入力
Deploying container to Cloud Run service [go-server] in project [$PROJECT_ID] region [asia-northeast1]
✓ Deploying... Done.
✓ Creating Revision...
✓ Routing traffic...
Done.
Service [go-server] revision [go-server-00002-dag] has been deployed and is serving 100 percent of traffic at https://$URL
コンソールからの実行
と非常に簡単にコンテナがデプロイできます。
現状GCRからのpullしか対応していないようで、Dockerhubなど外部リポジトリからは取得できないようです。
リポジトリの指定がgcr.ioが必要なようです。
gcloud run deploy --region=asia-northeast1 --image nginx:latest --platform=managed
〜中略〜
ERROR: (gcloud.run.deploy) Invalid image provided in the revision template. Expected [region.]gcr.io/repo-path[:tag or @digest], obtained nginx:latest
今後に期待すること
Dockerhubなど外部のリポジトリからも取得できるようにして欲しい
バージョンを手軽に戻せるようにして欲しい
と思いました。