gcloud run

Table of Contents

  1. 前提
  2. 基本コマンド
  3. デプロイ
    1. ソースコードから直接デプロイ
    2. コンテナイメージからデプロイ
    3. デプロイオプション
  4. サービス管理
  5. トラフィック管理
  6. ジョブ
  7. ログの確認
  8. Secret Manager との連携
  9. 注意点
  10. 参考リンク

gcloud run は Cloud Run サービスやジョブを管理するコマンドです。コンテナイメージをサーバーレス環境にデプロイし、HTTP リクエストやイベントを処理できます。

前提

1
2
# Cloud Run API を有効化
gcloud services enable run.googleapis.com

基本コマンド

コマンド 用途
gcloud run deploy サービスをデプロイ
gcloud run services list サービスの一覧を表示
gcloud run services describe サービスの詳細を表示
gcloud run services delete サービスを削除
gcloud run jobs create ジョブを作成
gcloud run jobs execute ジョブを実行
gcloud run revisions list リビジョンの一覧を表示

デプロイ

ソースコードから直接デプロイ

Dockerfile や buildpack を使ってソースコードから直接デプロイできます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# カレントディレクトリのソースからデプロイ
gcloud run deploy my-service --source=. --region=asia-northeast1

# 環境変数を設定してデプロイ
gcloud run deploy my-service \
--source=. \
--region=asia-northeast1 \
--set-env-vars="DB_HOST=10.0.0.1,APP_ENV=production"

# 未認証アクセスを許可(公開 API の場合)
gcloud run deploy my-service \
--source=. \
--region=asia-northeast1 \
--allow-unauthenticated

コンテナイメージからデプロイ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Artifact Registry のイメージからデプロイ
gcloud run deploy my-service \
--image=asia-northeast1-docker.pkg.dev/PROJECT_ID/my-repo/my-image:latest \
--region=asia-northeast1

# リソース制限を指定
gcloud run deploy my-service \
--image=asia-northeast1-docker.pkg.dev/PROJECT_ID/my-repo/my-image:latest \
--region=asia-northeast1 \
--memory=512Mi \
--cpu=1 \
--min-instances=0 \
--max-instances=10 \
--concurrency=80 \
--timeout=300

デプロイオプション

オプション 説明
--source=. ソースコードのパス(ビルドから自動で行う)
--image=IMAGE コンテナイメージの URL
--region=REGION デプロイ先リージョン
--allow-unauthenticated 未認証アクセスを許可
--set-env-vars=K=V,... 環境変数を設定(既存を上書き)
--update-env-vars=K=V,... 環境変数を追加・更新(既存を保持)
--set-secrets=K=SECRET:VERSION Secret Manager のシークレットを環境変数に
--service-account=SA 実行時のサービスアカウント
--memory=SIZE メモリ上限(例: 256Mi1Gi
--cpu=NUM CPU 数(例: 12
--min-instances=N 最小インスタンス数(コールドスタート対策)
--max-instances=N 最大インスタンス数
--concurrency=N 1 インスタンスあたりの同時リクエスト数
--timeout=SECONDS リクエストタイムアウト(最大 3600 秒)
--port=PORT コンテナがリッスンするポート(デフォルト: 8080)
--vpc-connector=CONNECTOR VPC コネクタ(内部リソースへのアクセス)
--ingress=INGRESS allinternalinternal-and-cloud-load-balancing

サービス管理

1
2
3
4
5
6
7
8
9
10
11
12
13
# サービスの一覧
gcloud run services list --region=asia-northeast1

# サービスの詳細
gcloud run services describe my-service --region=asia-northeast1

# サービスの URL を取得
gcloud run services describe my-service \
--region=asia-northeast1 \
--format="value(status.url)"

# サービスの削除
gcloud run services delete my-service --region=asia-northeast1

トラフィック管理

複数のリビジョン間でトラフィックを分割できます。カナリアデプロイに便利です。

1
2
3
4
5
6
7
8
9
10
11
12
# リビジョンの一覧
gcloud run revisions list --service=my-service --region=asia-northeast1

# トラフィックを分割(カナリアデプロイ)
gcloud run services update-traffic my-service \
--region=asia-northeast1 \
--to-revisions=my-service-00001=90,my-service-00002=10

# 最新リビジョンに 100% ロールバック
gcloud run services update-traffic my-service \
--region=asia-northeast1 \
--to-latest

ジョブ

HTTP リクエストではなくバッチ処理を行う場合はジョブを使います。

1
2
3
4
5
6
7
8
9
10
11
12
# ジョブの作成
gcloud run jobs create my-job \
--image=asia-northeast1-docker.pkg.dev/PROJECT_ID/my-repo/my-job:latest \
--region=asia-northeast1 \
--tasks=10 \
--max-retries=3

# ジョブの実行
gcloud run jobs execute my-job --region=asia-northeast1

# ジョブの実行状況を確認
gcloud run jobs executions list --job=my-job --region=asia-northeast1

ログの確認

1
2
3
4
5
# サービスのログをストリーミング
gcloud run services logs read my-service --region=asia-northeast1 --limit=50

# リアルタイムでログを追跡
gcloud run services logs tail my-service --region=asia-northeast1

Secret Manager との連携

1
2
3
4
5
6
7
8
9
10
11
# シークレットを環境変数としてマウント
gcloud run deploy my-service \
--image=IMAGE \
--region=asia-northeast1 \
--set-secrets="DB_PASSWORD=db-password:latest"

# シークレットをファイルとしてマウント
gcloud run deploy my-service \
--image=IMAGE \
--region=asia-northeast1 \
--set-secrets="/secrets/db-password=db-password:latest"

注意点

  • --source を使う場合、Cloud Build API と Artifact Registry API も有効化が必要
  • --allow-unauthenticated を付けないとデフォルトで認証が必要(IAM で roles/run.invoker を付与する)
  • --min-instances=1 以上にするとコールドスタートを回避できるが、常時課金が発生する
  • --concurrency=1 にすると 1 リクエストずつ処理するが、スケールアウトが増えてコストが上がる

参考リンク