Cloud Run を最速で触ってみる

chidakiyo
google-cloud-jp
Published in
8 min readApr 10, 2019

--

Cloud Run とは

Google Cloud Next 18では serverless containers on the Google Cloud Functions infrastructure + GKE Serverless addonと説明されたものですね。

早速、こちらのQuickStartやってみましょう

事前に必要なこと

  • プロジェクトを作成する(既存のプロジェクトを利用することもできます。終了後の後片付けを考えると新しくプロジェクトを作っても良いです)
  • プロジェクトのbillingを有効にする
  • Cloud Run APIを有効にする
  • CloudSDKのインストール済み/設定済みであること
  • componentsのupdateと、beta componentsのinstallが必要です。

コンポーネントをupdateする

gcloud components update

betaコンポーネントをinstallする

gcloud components install beta

Cloud Runのリージョンを設定する

us-central1で実行します

gcloud config set run/region us-central1

ビルドとデプロイ

試しにGoのコンテナをCloudRunで実行してみることにします。

ディレクトリの作成

ディレクトリを作成し、ディレクトリ配下に移動します

mkdir helloworld-go
cd helloworld-go

ソースコードの作成

helloworld.go というファイルを作成し、以下のソースコードを貼り付けます。

package mainimport (
"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))
}

環境変数で渡されたPORTでlistenするサーバを作成します。

こちらをGCRにアップロードしていきましょう

コンテナ化し、GCR(Google Container Repository)にアップロードする

コンテナ化するにはソースコードと同じ場所に Dockerfile を配置します。

以下の内容で Dockerfile を作成します

# 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"]

コンテナをBuildし、GCRにuploadする

Dockerfileを配置したディレクトリで以下のコマンドを実行し、コンテナをBuildします。

gcloud builds submit --project ${PROJECT_ID} --tag gcr.io/${PROJECT_ID}/helloworld

成功するとGCRにuploadされます

Cloud Runにデプロイします

gcloud beta run deploy --project ${PROJECT_ID} --image gcr.io/${PROJECT_ID}/helloworld

サービス名を入力

デプロイ時に ServiceName の入力を求められるので公開したいサービス名を入力する

リージョンの入力

us-central を選択

アプリケーションへのアクセス

デプロイが完了するとURLが画面表示されるのでアクセスしてみましょう

デプロイしたアプリケーションを確認する

Cloud Run のメニューを開く

デプロイしたサービスの一覧が表示される

サービスの詳細を確認する

サービスをクリックすることで詳細を確認できる

Web UI上からデプロイしてみる

Webからのデプロイも可能そうなのでやってみる

Create Service を押す

コンテナイメージは先程のコンテナを利用
サービス名は helloworld2
ロケーションは us-central1

認証のチェックボックスは 入力する

Optional:
詳細メニューから
- メモリ
- コンテナあたりの最大リクエスト数
- 環境変数
が入力できます。

値が入力できたら 作成 ボタンを押します。

今回ぐらいのコンテナサイズだと10秒もかからずデプロイが完了します(めちゃ早!!)

もちろん新しくデプロイしたコンテナも新たなURLが割り当てられ、そこにアクセスすることでHelloWorld!が表示されます。

おまけ : カスタムドメインの適用

上段の manage custom domains のボタンからドメイン設定ができます。

1つのサービスに対して所有しているドメインをサブドメインで当てることができるようです。
pathベースでのルーティング(GAEのdispatch的な)をできるのかはおいおい調べようと思います。

感想

めちゃくちゃ簡単にデプロイできるので、すでにコンテナで運用している人はすぐ使えそう。
デプロイがチョッ速!

--

--