設定無 Proxy gRPC 服務
本指南說明如何使用 Mesh
和 GRPCRoute
資源設定無 Proxy 的 gRPC 服務網格。
GRPCRoute
和 Mesh
資源 (按一下即可放大)事前準備
請務必詳閱「準備透過 Envoy 和無 Proxy 工作負載進行設定」,並完成該文件中說明的必要條件。
設定 Mesh
資源
無 Proxy gRPC 應用程式連線至 xds:///hostname
時,gRPC 用戶端程式庫會建立與 Cloud Service Mesh 的連線。用戶端程式庫會使用連線取得路由設定,以便為主機名稱的請求設定路由。
請務必記下 Mesh
資源的名稱,這是無 Proxy gRPC 應用程式用來要求與這個網格相關聯設定的金鑰。
建立
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
上公開的 gRPC 服務:helloworld
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
使用
grpcurl
工具,並將xds:///helloworld-gce
設為服務 URI,helloworld.Greeter/SayHello
設為要叫用的服務名稱和方法。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
資源」。