Compute Engine VM とプロキシレス gRPC サービスを設定する
このガイドでは、Compute Engine VM ホスト、gRPC アプリケーション、負荷分散コンポーネントなど、Traffic Director に必要な要素を構成する方法について説明します。
このガイドに従って作業を行う前に、プロキシレス gRPC サービスで Traffic Director を設定する準備をご覧ください。
概要
Compute Engine 仮想マシン(VM)とプロキシレス gRPC サービスで Traffic Director を設定するには、次の作業を行います。
- バックエンドをホストするマネージド インスタンス グループを設定する。
- クライアントからのリクエストに応じて
hello world
を返す gRPC サーバーを実行するようにバックエンドを構成する。 - マネージド インスタンス グループとその他の Google Cloud Load Balaning コンポーネントを使用して Traffic Director を構成する。
- プロキシレス gRPC クライアント アプリケーションを使用してトラフィックを gRPC サーバー アプリケーションに送信することで、デプロイが正しく機能していることを確認する。
プロキシレス gRPC クライアントは xDS を使用して Traffic Director に接続します。クライアントが Traffic Director に接続されると、Traffic Director は hello world
サービスに関連付けられているバックエンドの情報をクライアントに送信します。プロキシレス gRPC クライアントは、この情報を使用して hello world
gRPC サーバーにリクエストを送信します。
Traffic Director 用のマネージド インスタンス グループの構成
マネージド インスタンス グループは、自動スケーリングを使用して、デプロイで必要な新しいバックエンド VM を作成します。この例では、次の方法について説明します。
- gRPC プロトコルで
hello world
サービスを提供するサンプル サービスを使用して、インスタンス テンプレートを作成します。 - テンプレートを使用してマネージド インスタンス グループを構成します。
インスタンス テンプレートの作成
このセクションでは、インスタンス テンプレートの作成手順について説明します。この例では、ポート 50051 で公開されている helloworld
gRPC サービスをデプロイします。
コンソール
- Google Cloud コンソールで、[インスタンス テンプレート] ページに移動します。
- [インスタンス テンプレートを作成] をクリックします。
各フィールドを次のように指定します。
- 名前: grpc-td-vm-template
- ブートディスクのバージョン: Debian / Linux 10
- サービス アカウント: Compute Engine default service account
- アクセス スコープ: すべての Google Cloud APIs への完全アクセス権を許可
[ファイアウォール] で、[HTTP トラフィックを許可する] と [HTTPS トラフィックを許可する] の横にあるボックスを選択します。
[管理、セキュリティ、ディスク、ネットワーク、単一テナンシー] をクリックします。
[管理] タブで、次のスクリプトを [起動スクリプト] フィールドにコピーします。
#! /bin/bash set -e cd /root sudo apt-get update -y sudo apt-get install -y openjdk-11-jdk-headless curl -L https://github.com/grpc/grpc-java/archive/v1.37.0.tar.gz | tar -xz cd grpc-java-1.37.0/examples/example-hostname ../gradlew --no-daemon installDist # Server listens on 50051 sudo systemd-run ./build/install/hostname-server/bin/hostname-server
[作成] をクリックします。
gcloud
インスタンス テンプレートを作成します。
gcloud compute instance-templates create grpc-td-vm-template \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --tags=allow-health-checks \ --image-family=debian-10 \ --image-project=debian-cloud \ --metadata-from-file=startup-script=<(echo '#! /bin/bash set -e cd /root sudo apt-get update -y sudo apt-get install -y openjdk-11-jdk-headless curl -L https://github.com/grpc/grpc-java/archive/v1.37.0.tar.gz | tar -xz cd grpc-java-1.37.0/examples/example-hostname ../gradlew --no-daemon installDist # Server listens on 50051 sudo systemd-run ./build/install/hostname-server/bin/hostname-server')
マネージド インスタンス グループの作成
このセクションでは、前のセクションで作成したインスタンス テンプレートを使用してマネージド インスタンス グループを作成します。
コンソール
- Google Cloud コンソールの [インスタンス グループ] ページに移動します。
- [インスタンス グループを作成] をクリックします。デフォルトでは、マネージド インスタンス グループの作成に関するページが表示されます。
- マネージド インスタンス グループの名前として「
grpc-td-mig-us-central1
」を入力し、us-central1-a
ゾーンを選択します。 - [インスタンス テンプレート] で、作成したインスタンス テンプレート(
grpc-td-vm-template
)を選択します。 - グループ内に作成するインスタンスの最小数と最大数に 2 を指定します。
- [作成] をクリックします。
gcloud
マネージド インスタンス グループを作成します。
gcloud compute instance-groups managed create grpc-td-mig-us-central1 \ --zone us-central1-a \ --size=2 \ --template=grpc-td-vm-template
名前付きポートの構成
このセクションでは、gRPC サービスの名前付きポートを構成します。名前付きポートとは、gRPC サービスがリクエストをリッスンするポートです。この例では、名前付きポートはポート 50051 です。
コンソール
- Google Cloud コンソールの [インスタンス グループ] ページに移動します。
- 作成したインスタンス グループ
grpc-td-mig-us-central1
の名前をクリックします。 - [グループを編集] をクリックします。
- [ポート名] フィールドに「
grpc-helloworld-port
」と入力します。 - [ポート番号] フィールドに「
50051
」と入力します。 - [保存] をクリックします。
gcloud
名前付きポートを構成します。
gcloud compute instance-groups set-named-ports grpc-td-mig-us-central1 \ --named-ports=grpc-helloworld-port:50051 \ --zone us-central1-a
Google Cloud 負荷分散コンポーネントを使用して Traffic Director を構成する
このセクションでは、サービスの Google Cloud load balancing コンポーネントを使用して Traffic Director を構成する方法について説明します。
ヘルスチェック、ファイアウォール ルール、バックエンド サービスの作成
このセクションでは、INTERNAL_SELF_MANAGED
とプロトコル GRPC
の負荷分散方式でグローバル バックエンド サービスを作成し、バックエンド サービスにヘルスチェックとインスタンス グループを関連付けます。この例では、マネージド インスタンス グループの作成で作成したマネージド インスタンス グループを使用します。このマネージド インスタンス グループは、サンプルの gRPC サービスを実行します。--port-name
フラグのポートは、名前付きポートの構成で作成した名前付きポートです。
gcloud
ヘルスチェックを作成します。
gcloud compute health-checks create grpc grpc-helloworld-health-check \ --use-serving-port
ヘルスチェックのファイアウォール ルールを作成します。
gcloud compute firewall-rules create grpc-vm-allow-health-checks \ --network default --action allow --direction INGRESS \ --source-ranges=35.191.0.0/16,130.211.0.0/22 \ --target-tags allow-health-checks \ --rules tcp:50051
バックエンド サービスを作成します。
gcloud compute backend-services create grpc-helloworld-service \ --global \ --load-balancing-scheme=INTERNAL_SELF_MANAGED \ --protocol=GRPC \ --port-name=grpc-helloworld-port \ --health-checks=grpc-helloworld-health-check
マネージド インスタンス グループをバックエンド サービスに追加します。
gcloud compute backend-services add-backend grpc-helloworld-service \ --instance-group grpc-td-mig-us-central1 \ --instance-group-zone us-central1-a \ --global
ルーティング ルール マップ、ターゲット プロキシ、転送ルールの作成
このセクションでは、URL マップ、パスマッチャー、ホストルールを作成して、ホスト名とパスに基づいてサービスのトラフィックをルーティングします。次の例では、サービス名として helloworld-gce
を使用しています。gRPC アプリケーションは、このサービスに接続するときに、ターゲット URI でこのサービス名を使用します。デフォルトでは、パスマッチャーは、このホストのすべてのパスリクエスト(/*
)を照合します。また、ターゲット gRPC プロキシと転送ルールを作成します。
詳細については、ルーティング ルールマップをご覧ください。
次の例では、ポート 80 が指定のポートです。
gcloud
URL マップを作成します。
gcloud compute url-maps create grpc-vm-url-map \ --default-service grpc-helloworld-service
パスマッチャーを作成します。
gcloud compute url-maps add-path-matcher grpc-vm-url-map \ --default-service grpc-helloworld-service \ --path-matcher-name grpc-vm-path-matcher \ --new-hosts helloworld-gce
ターゲット gRPC プロキシを作成します。
gcloud compute target-grpc-proxies create grpc-vm-proxy \ --url-map grpc-vm-url-map \ --validate-for-proxyless
転送ルールを作成します。
gcloud compute forwarding-rules create grpc-vm-forwarding-rule \ --global \ --load-balancing-scheme=INTERNAL_SELF_MANAGED \ --address=0.0.0.0 --address-region=us-central1 \ --target-grpc-proxy=grpc-vm-proxy \ --ports 80 \ --network default
Traffic Director は、URL マップで指定されたサービスに対してマネージド インスタンス グループのバックエンド全体でトラフィックを負荷分散するように構成されています。
構成の確認
構成プロセスが完了したら、以前に作成した Traffic Director の負荷分散サービスへの gRPC 接続を確立できることを確認します。
サービスが利用可能かどうかを確認するには、次のいずれかを行います。
- VM ホストの 1 つ(gRPC サービスのバックエンド)にログインし、リスニング ポートで gRPC サービスが実行されていることを確認します。この例のポートは 50051 です。
- Console の Traffic Director ページで、構成されたサービス
helloworld-gce
を確認します。また、バックエンドが正常であると報告されていることを確認します。 - 次の手順で、Compute Engine gRPC クライアントの確認を行います。
gRPC クライアント アプリケーションによるサービスの確認
次の例では、選択した言語で gRPC クライアントを使用するか、grpcurl
ツールで gRPC サービスのテストを行います。
まず、サービスのテストを行う gRPC クライアントを実行するクライアント VM を作成します。
gcloud compute instances create grpc-client \ --zone us-central1-a \ --scopes=https://www.googleapis.com/auth/cloud-platform \ --image-family=debian-10 \ --image-project=debian-cloud \ --metadata-from-file=startup-script=<(echo '#! /bin/bash set -e export GRPC_XDS_BOOTSTRAP=/run/td-grpc-bootstrap.json # Expose bootstrap variable to SSH connections echo export GRPC_XDS_BOOTSTRAP=$GRPC_XDS_BOOTSTRAP | sudo tee /etc/profile.d/grpc-xds-bootstrap.sh # Create the bootstrap file curl -L https://storage.googleapis.com/traffic-director/td-grpc-bootstrap-0.11.0.tar.gz | tar -xz ./td-grpc-bootstrap-0.11.0/td-grpc-bootstrap | tee $GRPC_XDS_BOOTSTRAP')
環境変数とブートストラップ ファイルの設定
クライアント アプリケーションには、ブートストラップ構成ファイルが必要です。前のセクションの起動スクリプトで GRPC_XDS_BOOTSTRAP
環境変数を設定し、ヘルパー スクリプトを使用してブートストラップ ファイルを生成します。生成されたブートストラップ ファイル内の TRAFFICDIRECTOR_GCP_PROJECT_NUMBER
、TRAFFICDIRECTOR_NETWORK_NAME
、zone
の値は、Compute Engine VM インスタンスの詳細を把握しているメタデータ サーバーから取得されます。これらの値は、-gcp-project-number
オプションと -vpc-network-name
オプションを使用してヘルパー スクリプトに手動で指定できます。
構成を確認するには、クライアント VM にログインして次の例を実行します。
Java
gRPC Java クライアントでサービスを確認するには:
gRPC Java の最新バージョンをダウンロードし、最新のパッチを適用して
xds-hello-world
クライアント アプリケーションをビルドします。sudo apt-get update -y sudo apt-get install -y openjdk-11-jdk-headless curl -L https://github.com/grpc/grpc-java/archive/v1.37.0.tar.gz | tar -xz cd grpc-java-1.37.0/examples/example-xds ../gradlew --no-daemon installDist
名前として
world
を、サービス URI としてxds:///helloworld-gce
を使用してクライアントを実行します。./build/install/example-xds/bin/xds-hello-world-client "world" \ xds:///helloworld-gce
Go
gRPC Go クライアントでサービスを確認するには:
gRPC Go の最新バージョンをダウンロードし、最新のパッチを適用して
xds-hello-world
クライアント アプリケーションをビルドします。sudo apt-get update -y sudo apt-get install -y golang git curl -L https://github.com/grpc/grpc-go/archive/v1.37.0.tar.gz | tar -xz cd grpc-go-1.37.0/examples/features/xds/client go get google.golang.org/grpc@v1.37.0 go build .
名前として
world
を、サービス URI としてxds:///helloworld-gce
を使用してクライアントを実行します。./client "world" xds:///helloworld-gce
C++
gRPC C++ クライアントでサービスを確認するには:
gRPC C++ の最新バージョンをダウンロードし、最新のパッチを適用して
helloworld
クライアントのサンプルをビルドします。sudo apt-get update -y sudo apt-get install -y build-essential cmake git git clone --recurse-submodules -b v1.37.1 https://github.com/grpc/grpc cd grpc mkdir -p cmake/build pushd cmake/build cmake ../.. make sudo make install popd mkdir -p third_party/abseil-cpp/cmake/build pushd third_party/abseil-cpp/cmake/build cmake ../.. make sudo make install popd cd examples/cpp/helloworld mkdir -p cmake/build cd cmake/build/ cmake ../.. make
サービス URI として「xds:///helloworld-gce」を指定してクライアントを実行します。
./greeter_client --target=xds:///helloworld-gce
grpcurl
grpcurl
ツールを使用してサービスを確認するには:
grpcurl
ツールをダウンロードしてインストールします。curl -L https://github.com/fullstorydev/grpcurl/releases/download/v1.8.1/grpcurl_1.8.1_linux_x86_64.tar.gz | tar -xz
サービス URI として「xds:///helloworld-gce」を指定し、サービス名とメソッドとして
helloworld.Greeter/SayHello
を指定して、grpcurl
ツールを実行します。SayHello
メソッドのパラメータは-d
オプションで渡します。./grpcurl --plaintext \ -d '{"name": "world"}' \ xds:///helloworld-gce helloworld.Greeter/SayHello
Python
gRPC Python クライアントでサービスを確認するには、次のコマンドを実行します。最新バージョンの gRPC を使用して最新のパッチを適用します。
sudo apt-get update sudo apt-get -y install python3-pip sudo pip3 install virtualenv curl -L https://github.com/grpc/grpc/archive/v1.37.1.tar.gz | tar -xz cd grpc-1.37.1/examples/python/xds virtualenv venv -p python3 source venv/bin/activate pip install -r requirements.txt python client.py xds:///helloworld-gce
Ruby
gRPC Ruby クライアントでサービスを確認するには、次のコマンドを実行します。最新バージョンの gRPC を使用して最新のパッチを適用します。
sudo apt-get update sudo apt-get install -y ruby-full sudo gem install grpc curl -L https://github.com/grpc/grpc/archive/v1.37.1.tar.gz | tar -xz cd grpc-1.37.1/examples/ruby ruby greeter_client.rb john xds:///helloworld-gce
PHP
gRPC PHP クライアントでサービスを確認するには、次のコマンドを実行します。最新バージョンの gRPC を使用して最新のパッチを適用します。
sudo apt-get update sudo apt-get install -y php7.3 php7.3-dev php-pear phpunit python-all zlib1g-dev git sudo pecl install grpc curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer curl -L https://github.com/grpc/grpc/archive/v1.37.1.tar.gz | tar -xz cd grpc-1.37.1 export CC=/usr/bin/gcc ./tools/bazel build @com_google_protobuf//:protoc ./tools/bazel build src/compiler:grpc_php_plugin cd examples/php composer install ./../../bazel-bin/external/com_google_protobuf/protoc --proto_path=../protos \ --php_out=. --grpc_out=. \ --plugin=protoc-gen-grpc=../../bazel-bin/src/compiler/grpc_php_plugin \ ../protos/helloworld.proto php -d extension=grpc.so greeter_client.php john xds:///helloworld-gce
Node.js
gRPC Node.js クライアントでサービスを確認するには、次のコマンドを実行します。最新バージョンの gRPC を使用して最新のパッチを適用します。
sudo apt-get update sudo apt-get install -y nodejs npm curl -L https://github.com/grpc/grpc/archive/v1.34.0.tar.gz | tar -xz cd grpc-1.34.0/examples/node/xds npm install node ./greeter_client.js --target=xds:///helloworld-gce
次のような出力が表示されます。INSTANCE_NAME
は VM インスタンスの名前です。
Greeting: Hello world, from INSTANCE_HOSTNAME
これにより、プロキシレス gRPC クライアントが Traffic Director に接続され、helloworld-gce
のバックエンドが xds
の名前解決機能を使用して確認されていることが検証されます。クライアントは、IP アドレスの把握や DNS 解決の実行なしに、サービスのバックエンドの 1 つに対してリクエストを送信しています。
次のステップ
- 高度なトラフィック管理について確認する。
- オブザーバビリティの設定方法について確認する。
- プロキシレスの Traffic Director のデプロイをトラブルシューティングする方法について確認する。