0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Google Cloud】Cloud Load Balancingを使ってみる

0
Last updated at Posted at 2026-05-25

はじめに

この記事では、Google Cloud の Global External Load Balancer を構築し、URLパスに応じて Cloud RunGCE マネージドインスタンスグループ(MIG) にトラフィックを振り分けてみます。

構成概要

インターネット
  └─ Global External Load Balancer
       ├─ /api/* → バックエンドA(Cloud Run)
       └─ /*    → バックエンドB(GCE MIG × 2台)

使用するサービス

サービス 役割
Global External Load Balancer HTTPSトラフィックの分散
Cloud Run サーバーレスバックエンドA
GCE マネージドインスタンスグループ バックエンドB(Nginx)
Serverless NEG Cloud RunをLBに接続

前提条件

  • Google Cloud プロジェクトが作成済み
  • gcloud CLI がインストール済み・認証済み
  • 必要なAPIが有効化済み

Step1:ネットワークの準備

VPCネットワーク・サブネットおよびファイアウォールルールを設定します。

# VPCネットワークを作成
gcloud compute networks create test-vpc \
  --subnet-mode custom

# サブネットを作成
gcloud compute networks subnets create test-subnet-1 \
  --network test-vpc \
  --region asia-northeast1 \
  --range 10.0.1.0/24

# ファイアウォールルール:HTTPを許可
gcloud compute firewall-rules create handson-allow-http \
  --network=test-vpc \
  --allow=tcp:80,tcp:443 \
  --source-ranges=0.0.0.0/0 \
  --target-tags=http-server

# ファイアウォールルール:ヘルスチェックを許可
gcloud compute firewall-rules create handson-allow-health-check \
  --network=test-vpc \
  --allow=tcp:80 \
  --source-ranges=130.211.0.0/22,35.191.0.0/16 \
  --target-tags=http-server

# ファイアウォールルール:IAP経由のSSHを許可
gcloud compute firewall-rules create handson-allow-ssh \
  --network=test-vpc \
  --allow=tcp:22 \
  --source-ranges=35.235.240.0/20 \
  --target-tags=http-server

Step2:バックエンドの準備

バックエンドリソースとしてCloud RunとGCE MIG(Nginx)を作成します。

バックエンドA:Cloud Run

# サンプルアプリをCloud Runにデプロイ
gcloud run deploy backend-a \
  --image=gcr.io/google-samples/hello-app:1.0 \
  --region=asia-northeast1 \
  --no-allow-unauthenticated  # LB経由のみ許可

バックエンドB:GCE MIG(Nginx)

# インスタンステンプレートの作成
gcloud compute instance-templates create web-template \
  --machine-type=e2-micro \
  --image-family=debian-11 \
  --image-project=debian-cloud \
  --tags=http-server \
  --network=test-vpc \
  --subnet=test-subnet-1 \
  --region=asia-northeast1 \
  --metadata=startup-script='#!/bin/bash
    apt-get update
    apt-get install -y nginx
    echo "Backend B - $(hostname)" > /var/www/html/index.html
    systemctl start nginx'

# マネージドインスタンスグループの作成
gcloud compute instance-groups managed create web-mig \
  --template=web-template \
  --size=2 \
  --zone=asia-northeast1-a

# 名前付きポートの設定(LBがHTTP:80でヘルスチェックするために必要)
gcloud compute instance-groups managed set-named-ports web-mig \
  --named-ports=http:80 \
  --zone=asia-northeast1-a

Step3:ヘルスチェックの作成

80番ポートに対して、10秒間隔でヘルスチェックを実行するように設定します。

gcloud compute health-checks create http web-health-check \
  --port=80 \
  --request-path=/ \
  --check-interval=10s \
  --timeout=5s \
  --healthy-threshold=2 \
  --unhealthy-threshold=3

Step4:バックエンドサービスの作成

LBのバックエンドサービスを作成します。

Cloud Run 用バックエンドサービス(Serverless NEG)

# Serverless NEG の作成
gcloud compute network-endpoint-groups create cloudrun-neg \
  --region=asia-northeast1 \
  --network-endpoint-type=SERVERLESS \
  --cloud-run-service=backend-a

# Cloud Run 用バックエンドサービスの作成
gcloud compute backend-services create api-backend \
  --protocol=HTTP \
  --global

# NEG をバックエンドに追加
gcloud compute backend-services add-backend api-backend \
  --network-endpoint-group=cloudrun-neg \
  --network-endpoint-group-region=asia-northeast1 \
  --global

GCE MIG 用バックエンドサービス

# バックエンドサービスの作成
gcloud compute backend-services create web-backend \
  --protocol=HTTP \
  --health-checks=web-health-check \
  --global

# MIG をバックエンドに追加
gcloud compute backend-services add-backend web-backend \
  --instance-group=web-mig \
  --instance-group-zone=asia-northeast1-a \
  --balancing-mode=UTILIZATION \
  --max-utilization=0.8 \
  --global

Step5:URLマップの作成(パスベースルーティング)

LBでパスが「/api」の場合はCloud Runに、それ以外の場合はGCE MIGにルーティングされるようにURLマップを作成します。

パス 転送先
/api/* バックエンドA(Cloud Run)
/*(その他すべて) バックエンドB(GCE MIG)
# /api/* はバックエンドA(Cloud Run)に転送
gcloud compute url-maps add-path-matcher web-url-map \
  --path-matcher-name=path-matcher \
  --default-service=web-backend \
  --path-rules="/api/*=api-backend"
  
# デフォルトはバックエンドB(GCE MIG)
gcloud compute url-maps create web-url-map \
  --default-service=web-backend

Step6:SSL証明書(ハンズオン・テスト向けの自己署名証明書)の作成

テスト用のドメインを使用して自己署名証明書を作成します。ブラウザに警告が表示されますが、curl -k で動作確認できます。

# 自己署名証明書の作成
openssl req -x509 \
  -newkey rsa:2048 \
  -keyout key.pem \
  -out cert.pem \
  -days 365 \
  -nodes \
  -subj "/CN=test.example.com"

# GCPに証明書を登録
gcloud compute ssl-certificates create web-ssl-cert \
  --certificate=cert.pem \
  --private-key=key.pem \
  --global

Step7:プロキシとフォワーディングルールの作成

LBのプロキシとフォワーディングルールを作成します。

# グローバル静的IPアドレスの予約
gcloud compute addresses create web-ip \
  --global \
  --ip-version=IPV4

# IPアドレスの確認
gcloud compute addresses describe web-ip \
  --global \
  --format="value(address)"

# ターゲットHTTPSプロキシの作成
gcloud compute target-https-proxies create web-https-proxy \
  --url-map=web-url-map \
  --ssl-certificates=web-ssl-cert

# ターゲットHTTPプロキシの作成
gcloud compute target-http-proxies create web-http-proxy \
  --url-map=web-url-map

# HTTPSフォワーディングルール(443)
gcloud compute forwarding-rules create web-https-rule \
  --global \
  --target-https-proxy=web-https-proxy \
  --address=web-ip \
  --ports=443

# HTTPフォワーディングルール(80)
gcloud compute forwarding-rules create web-http-rule \
  --global \
  --target-http-proxy=web-http-proxy \
  --address=web-ip \
  --ports=80

Step8:動作確認

# LBのIPアドレスを確認
LB_IP=$(gcloud compute addresses describe web-ip \
  --global \
  --format="value(address)")

echo "LB IP: $LB_IP"

# バックエンドB(GCE MIG)へのアクセス確認
curl -k https://$LB_IP/
# 実行結果(インスタンスグループのいずれかのインスタンス名が返ってくる)
# web-mig-4fcm または web-mig-bzh8

# バックエンドA(Cloud Run)へのアクセス確認
curl -k https://$LB_IP/api/
# 実行結果
# Hello, world!
# Version: 1.0.0

クリーンアップ

ハンズオン終了後は以下のコマンドでリソースを削除し、課金を停止します。

# フォワーディングルールの削除
gcloud compute forwarding-rules delete web-https-rule --global -q
gcloud compute forwarding-rules delete web-http-rule --global -q

# プロキシの削除
gcloud compute target-https-proxies delete web-https-proxy -q
gcloud compute target-http-proxies delete web-http-proxy -q

# URLマップ・証明書・IPの削除
gcloud compute url-maps delete web-url-map -q
gcloud compute ssl-certificates delete web-ssl-cert --global -q
gcloud compute addresses delete web-ip --global -q

# バックエンドサービスの削除
gcloud compute backend-services delete web-backend --global -q
gcloud compute backend-services delete api-backend --global -q

# ヘルスチェックの削除
gcloud compute health-checks delete web-health-check -q

# NEGの削除
gcloud compute network-endpoint-groups delete cloudrun-neg \
  --region=asia-northeast1 -q

# MIGとテンプレートの削除
gcloud compute instance-groups managed delete web-mig \
  --zone=asia-northeast1-a -q
gcloud compute instance-templates delete web-template -q

# Cloud Runの削除
gcloud run services delete backend-a \
  --region=asia-northeast1 -q

# ファイアウォールルールとネットワークの削除
gcloud compute firewall-rules delete handson-allow-http -q
gcloud compute firewall-rules delete handson-allow-health-check -q
gcloud compute firewall-rules delete handson-allow-ssh -q
gcloud compute networks subnets delete test-subnet-1 \
  --region=asia-northeast1 -q
gcloud compute networks delete test-vpc -q

echo "クリーンアップ完了"

Google Cloud の Load Balancer は構成要素が多いですが、各コンポーネントの役割を理解することで、柔軟なトラフィック制御が可能になります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?