本教學課程說明如何以「服務消費者」身分,建立 Private Service Connect「端點」來存取已發布的服務。當您將要求傳送至端點時,Private Service Connect 會將這些要求轉送至已發布的服務。
本教學課程的適用對象為雲端架構師、網路架構師、網路管理員和 IT 管理員。
目標
- 為服務消費者資源設定網路
- 建立端點
- 測試存取端點
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- 完成本系列的前兩篇教學課程:
- 建立負載平衡服務。
- 讓其他虛擬私有雲網路可存取服務。您需要 服務附件 ID 才能完成本教學課程。
- 建立或選取專案,用於服務消費者資源。
這個專案稱為
CONSUMER_PROJECT -
In the Google Cloud console, go to the project selector page.
-
Select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Enable the Compute Engine API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
Make sure that you have the following role or roles on the project: Compute Engine > Compute Network Admin, Compute Engine > Compute Instance Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 選取所需專案。
- 按一下「Grant access」(授予存取權)。
-
在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是指 Google 帳戶的電子郵件地址。
- 按一下「選取角色」,然後搜尋角色。
- 如要授予其他角色,請按一下「Add another role」(新增其他角色),然後新增其他角色。
- 按一下「Save」(儲存)。
-
設定網路
以下各節說明如何建立網路和子網路來代管端點,以及如何建立防火牆規則,允許對用於測試端點的用戶端 VM 進行 SSH 存取。
建立網路和子網路
如要建立服務消費者網路和子網路,請按照下列步驟操作。
控制台
前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。
按一下「建立虛擬私有雲網路」。
在「Name」(名稱) 中輸入
consumer-network。在「子網路建立模式」部分,選取「自訂」。
在「New subnet」(新的子網路) 區段中,輸入以下資訊。
- 在「Name」(名稱) 中輸入
consumer-subnet。 - 在「Region」(區域),選取您建立的已發布服務所在的區域。
- 在「IP stack type」(IP 堆疊類型),選取「IPv4 (single-stack)」(IPv4 (單一堆疊))。
- 在「IPv4 range」(IPv4 範圍) 中,輸入
192.168.10.0/24。 - 按一下 [完成]。
- 在「Name」(名稱) 中輸入
點選「建立」。
gcloud
建立自訂模式虛擬私有雲網路:
gcloud compute networks create consumer-network --subnet-mode=custom
在
consumer-network網路中建立子網路。gcloud compute networks subnets create consumer-subnet \ --network=consumer-network \ --range=192.168.10.0/24 \ --region=REGION將
REGION替換為您建立的已發布服務所用區域。
設定防火牆規則
建立名為 fw-allow-ssh 的防火牆規則,允許來自 0.0.0.0/0 的 SSH 流量傳送到具有 allow-ssh 網路標記的 VM。
控制台
前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。
如要允許連入 SSH 連線,請按一下「建立防火牆規則」,然後使用下列設定:
- 在「Name」(名稱) 中輸入
fw-allow-ssh。 - 在「Network」(網路) 中選取
consumer-network。 - 在「Priority」(優先順序) 中輸入
1000。 - 在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
- 在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。
- 在「Targets」(目標) 中選取 Specified target tags。
- 在「Target tags」(目標標記) 中輸入
allow-ssh。 - 在「Source filter」(來源篩選器) 中選取「IPv4 ranges」(IPv4 範圍)。
- 在「Source IPv4 ranges」(來源 IPv4 範圍) 中輸入
0.0.0.0/0。 - 在「Protocols and ports」(通訊協定和通訊埠) 中選取「Specified protocols and ports」(指定的通訊協定和通訊埠),然後勾選「TCP」核取方塊。在「Ports」(通訊埠) 部分,輸入
22。
- 在「Name」(名稱) 中輸入
按一下「建立」。
gcloud
建立
fw-allow-ssh防火牆規則,允許與具有allow-ssh網路標記的 VM 建立 SSH 連線。gcloud compute firewall-rules create fw-allow-ssh \ --network=consumer-network \ --action=allow \ --direction=ingress \ --source-ranges=0.0.0.0/0 \ --target-tags=allow-ssh \ --rules=tcp:22
建立端點
建立指向您建立的已發布服務的端點。
控制台
前往 Google Cloud 控制台的「Private Service Connect」頁面。
按一下「已連線的端點」分頁標籤。
按一下「連結端點」。
在「目標」中,選取「已發布的服務」。
在「目標服務」中,輸入
projects/PRODUCER_PROJECT/regions/REGION/serviceAttachments/published-service。將
PRODUCER_PROJECT替換為已發布服務的專案 ID。將REGION改為已發布服務的區域。在「Endpoint name」(端點名稱) 中輸入
ep-1。在「Network」(網路) 中選取
consumer-network。在「Subnetwork」(子網路) 中,選取
consumer-subnet。按一下「IP address」(IP 位址) 下拉式選單,然後選取「Create IP address」(建立 IP 位址)。
- 在「Name」(名稱) 中輸入
ep-ip-1。 - 在「Static IP address」(靜態 IP 位址) 中,選取 Let me choose。
- 在「Custom IP address」(自訂 IP 位址) 中,輸入
192.168.10.5。 - 按一下「保留」。
- 在「Name」(名稱) 中輸入
按一下「新增端點」。
gcloud
保留要指派給端點的內部 IP 位址。
gcloud compute addresses create ep-ip-1 \ --region=REGION \ --subnet=consumer-subnet \ --addresses=192.168.10.5將
REGION替換為您建立的已發布服務所用區域。建立轉送規則,將端點連至服務供應商的服務連結。
gcloud compute forwarding-rules create ep-1 \ --region=REGION \ --network=consumer-network \ --address=ep-ip-1 \ --target-service-attachment=projects/PRODUCER_PROJECT/regions/REGION/serviceAttachments/published-service更改下列內容:
PRODUCER_PROJECT:服務生產者專案的專案 ID。REGION:服務連結的區域。
測試存取端點
如要測試端點和已發布的服務是否正常運作,請建立測試 VM,並從該 VM 將要求傳送至端點。
建立用戶端 VM 以進行測試
在與端點相同的區域中建立用戶端 VM。
控制台
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
點選「建立執行個體」。
在「Name」(名稱) 中輸入
consumer-test。在「Region」(區域),選取與後端 VM 相同的區域。
在「Zone」(可用區),選取該地區中的可用區。
按一下「網路」,然後設定下列欄位:
- 在「Network tags」(網路標記) 部分,輸入
allow-ssh。 - 在「網路介面」中,選取下列項目:
- 在「Network」(網路) 中選取
consumer-network。 - 在「Subnet」(子網路) 中,選取
consumer-subnet。
- 在「Network」(網路) 中選取
- 在「Network tags」(網路標記) 部分,輸入
點選「建立」。
gcloud
gcloud compute instances create consumer-test \
--zone=ZONE \
--image-family=debian-12 \
--image-project=debian-cloud \
--tags=allow-ssh \
--subnet=consumer-subnet
將 ZONE 替換為與後端 VM 位於相同區域的可用區。
測試連線能力
這項測試會從用戶端 VM 與端點聯絡。預期的行為是在負載平衡器的後端 VM 之間分配流量。
- 連線至用戶端 VM 執行個體。
將gcloud compute ssh consumer-test --zone=ZONE
ZONE替換為用戶端 VM 的可用區。 - 使用
curl聯絡端點的 IP 位址,向端點發出網頁要求。重複發出要求,系統便會顯示來自不同後端 VM 的回應。由於每個後端 VM 上的/var/www/html/index.html內容不同,因此產生回應的 VM 名稱會顯示在 HTML 回應的文字中。舉例來說,預期回覆看起來會像Page served from: vm-1和Page served from: vm-2。curl -s http://192.168.10.5
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 帳戶收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
刪除製作人專案 (PRODUCER_PROJECT) 和消費者專案 (CONSUMER_PROJECT)。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
後續步驟
- 進一步瞭解 Private Service Connect 發布的服務
- 進一步瞭解如何透過端點存取已發布的服務
- 查看 Google Cloud 的參考架構、圖表和最佳做法。 歡迎瀏覽我們的 Cloud Architecture Center。