LoginSignup
10
5

More than 5 years have passed since last update.

Google Cloud Runを触ってみた

Last updated at Posted at 2019-04-21

Google Cloud Next '19 で発表されたCloud Run。報告会やブログなどで良い印象でしたので早速触ってみました。

Cloud Run の特徴

詳細は公式ドキュメントを見ていただくとして、ポイントは以下。

  • サーバーレス
  • コンテナを実行できる
  • オートスケール

触ってみる

こちらのチュートリアルに沿って触ってみます。手順の流れは以下のようになります。

  1. 下準備
  2. アプリ作成
  3. docker イメージのビルド & コンテナレジストリへアップロード
  4. Cloud Run へデプロイ
  5. お掃除

1. 下準備

下準備では以下を行います。

  1. プロジェクトを作成(既存のプロジェクトを選択も可)
  2. プロジェクトの課金を有効にする
  3. Cloud Run APIを有効にする
  4. Cloud SDK のインストールとイニシャライズ
  5. gcloud beta component のインストール
  6. gcloud components のアップデート

チュートリアルに沿っていけば問題ないです。
APIを有効にする際は今回作業するプロジェクトを選択していることを確認しておきましょう。自分は他のプロジェクトを選択した状態だったので、後の手順の Cloud Run へのデプロイで 「image にアクセスする権限がないよ」と怒られました。
Cloud SDK のインストールとイニシャライズはこちらの「対話型インストーラ」の手順で行いました。

2. アプリ作成

hello world アプリを作ります。作業ディレクトリを作ってそこにソースコードを作りましょう。今回は Go で作りました。

helloworld.go
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))
}

3. コンテナイメージ作成 & コンテナレジストリへアップロード

作業ディレクトリに 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"]

Cloud Build を使ってコンテナイメージをビルド & コンテナレジストリへアップロードします。作業ディレクトリで以下のコマンドを打ちます。([PROJECT-ID] の部分には今回作業しているプロジェクトIDを入れてください。以下同様。)

gcloud builds submit --tag gcr.io/[PROJECT-ID]/helloworld

4. Cloud Run へのデプロイ

コンテナイメージを Cloud Run へデプロイします。

gcloud beta run deploy --image gcr.io/[PROJECT-ID]/helloworld

途中でリージョンを訊かれます。

Please specify a region:
 [1] us-central1
 [2] cancel
Please enter your numeric choice:

「us-central1」を選択しましょう。その後「unauthenticated invocations」を許可するか聞かれます。

Allow unauthenticated invocations to new service [helloworld]? (y/N)?

こちらは「y」を選択します。
数秒待つとデプロイできた旨のメッセージが表示されます。URL があるのでブラウザからアクセスすると無事「Hello World!!」が表示されました。

5. お掃除

お試しが終わったら作成したプロジェクトは削除しておきましょう。

所感

サーバーレスでコンテナを実行できるので、AWS Lambda などの FaaS と違い、言語やフレームワークなどが自由に選択できるのはいいですね。
課金対象となる時間はコンテナインスタンスの起動時間ではなくリクエストを処理している時間であることや、同時実行数がデフォルトで 80 あるのもポイントですね。

10
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5