設定無 Proxy gRPC 服務
本指南說明如何使用 Mesh
和 GRPCRoute
資源設定無 Proxy gRPC 服務網格。
GRPCRoute
和 Mesh
資源的無 Proxy gRPC 服務 (按一下可放大)事前準備
請務必閱讀「準備使用 Envoy 和無 Proxy 的工作負載」,並完成該文件中所述的必要條件。
設定 Mesh
資源
當無 Proxy gRPC 應用程式連線至 xds:///hostname
時,gRPC 用戶端程式庫會建立與 Cloud Service Mesh 的連線。用戶端程式庫會使用連線取得轉送主機名稱要求所需的轉送設定。
請務必記下 Mesh
資源的名稱,這是無 Proxy gRPC 應用程式用來要求與此 Mesh 相關聯的設定的鍵。
建立
Mesh
規格,並儲存至名為 mesh.yaml 的檔案。name: grpc-mesh
使用
mesh.yaml
規格建立Mesh
資源:gcloud network-services meshes import grpc-mesh \ --source=mesh.yaml \ --location=global
建立 Mesh
資源後,Cloud Service Mesh 就會準備好提供設定,但由於尚未定義任何服務,因此設定為空白。在下一節中,您將定義服務並將其附加至 Mesh
資源。
設定 gRPC 伺服器
為示範起見,您會在代管執行個體群組中,建立含有自動調度資源的 VM 後端服務。VM 會使用通訊埠 50051
上的 gRPC 通訊協定,提供字詞 hello world
。
建立 Compute Engine VM 執行個體範本,其中包含在
50051
連接埠公開的helloworld
gRPC 服務:gcloud compute instance-templates create-with-container grpc-td-vm-template \ --container-image=grpc/java-example-hostname:1.73.0 \ --tags=allow-health-checks \ --scopes=https://www.googleapis.com/auth/cloud-platform
依據範本建立代管執行個體群組:
gcloud compute instance-groups managed create grpc-td-mig \ --zone=ZONE \ --size=2 \ --template=grpc-td-vm-template
為 gRPC 服務建立命名通訊埠。已命名的通訊埠是 gRPC 服務用來監聽要求的通訊埠。在以下範例中,命名通訊埠為
50051
:gcloud compute instance-groups set-named-ports grpc-td-mig \ --named-ports=grpc-helloworld-port:50051 \ --zone=ZONE
建立 gRPC 健康狀態檢查。服務必須實作 gRPC 健康狀態檢查通訊協定,才能讓 gRPC 健康狀態檢查正常運作。詳情請參閱「健康狀態檢查」。
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
使用
INTERNAL_SELF_MANAGED
的負載平衡機制建立全球後端服務,並將健康狀態檢查新增至後端服務。此處指定的通訊埠用於連線至代管執行個體群組中的 VM。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 \ --instance-group-zone=ZONE \ --global
已設定 Mesh
資源和服務。在下一節中,您將設定路由。
使用 GRPCRoute
設定轉送
請按照下列操作說明設定路由。
建立
GRPCRoute
規格,並儲存至名為grpc_route.yaml
的檔案。您可以使用
PROJECT_ID
或PROJECT_NUMBER
。name: helloworld-grpc-route hostnames: - helloworld-gce meshes: - projects/PROJECT_NUMBER/locations/global/meshes/grpc-mesh rules: - action: destinations: - serviceName: projects/PROJECT_NUMBER/locations/global/backendServices/grpc-helloworld-service
使用
grpc_route.yaml
規格建立GrpcRoute
資源:gcloud network-services grpc-routes import helloworld-grpc-route \ --source=grpc_route.yaml \ --location=global
Cloud Service Mesh 現已設定為在代管執行個體群組的後端間,為 GRPCRoute
資源中指定的服務負載平衡流量。
建立 gRPC 用戶端
您可以透過例項化無 Proxy gRPC 應用程式,並將其連結至 Cloud Service Mesh,驗證設定。應用程式必須在其啟動檔案中指定 Mesh 中指定的 VPC 網路。
設定完成後,應用程式就能使用 xds:///helloworld-gce
服務 URI,將要求傳送至與 helloworld-gce
相關聯的執行個體或端點。
在以下範例中,您會使用 grpcurl 工具測試 gRPC 服務。
建立用戶端 VM。
gcloud compute instances create grpc-client \ --zone=ZONE\ --scopes=https://www.googleapis.com/auth/cloud-platform \ --image-family=debian-12 \ --image-project=debian-cloud \ --metadata-from-file=startup-script=<(echo '#!/bin/bash set -ex export GRPC_XDS_BOOTSTRAP=/run/td-grpc-bootstrap.json echo export GRPC_XDS_BOOTSTRAP=$GRPC_XDS_BOOTSTRAP | sudo tee /etc/profile.d/grpc-xds-bootstrap.sh curl -L https://storage.googleapis.com/traffic-director/td-grpc-bootstrap-0.16.0.tar.gz | tar -xz ./td-grpc-bootstrap-0.16.0/td-grpc-bootstrap --config-mesh=grpc-mesh | tee $GRPC_XDS_BOOTSTRAP')
設定啟動檔案
用戶端應用程式必須有引導設定檔。上一節中的啟動指令碼會設定 GRPC_XDS_BOOTSTRAP
環境變數,並使用輔助指令碼產生引導檔案。產生的啟動檔案中 TRAFFICDIRECTOR_GCP_PROJECT_NUMBER
和區域的值,是從瞭解 VM 執行個體這些詳細資料的中繼資料伺服器取得。您可以使用 --gcp-project-number
選項,手動將這些值提供給輔助程式指令碼。您必須使用 --config-mesh
選項提供與 Mesh
資源相符的網格名稱。
如要驗證設定,請登入用戶端 VM 並執行下列指令。
透過 SSH 連線至用戶端 VM。
gcloud compute ssh grpc-client --zone=ZONE
下載並安裝
grpcurl
工具。curl -L https://github.com/fullstorydev/grpcurl/releases/download/v1.9.2/grpcurl_1.9.2_linux_x86_64.tar.gz | tar -xz
請使用
xds:///helloworld-gce
做為服務 URI,並將helloworld.Greeter/SayHello
做為要叫用的服務名稱和方法,執行grpcurl
工具。SayHello
方法的參數會透過-d
選項傳遞。./grpcurl --plaintext \ -d '{"name": "world"}' \ xds:///helloworld-gce helloworld.Greeter/SayHello
您應該會看到類似以下的輸出內容,其中 INSTANCE_HOSTNAME
是 gRPC 伺服器 VM 例項的名稱:
{ "message": "Hello world, from INSTANCE_HOSTNAME" }
輸出內容會驗證無 Proxy gRPC 用戶端是否已成功連線至 Cloud Service Mesh,並使用 xds
名稱解析器瞭解 helloworld-gce
服務的後端。用戶端向服務的其中一個後端傳送要求,而不需要知道 IP 位址或執行 DNS 解析。
後續步驟
- 如要瞭解如何列出與
Mesh
或Gateway
資源相關聯的路徑資源,請參閱「列出Route
資源」。