1. 簡介
Private Service Connect 介面可讓生產端虛擬私有雲 (VPC) 網路,為消費者虛擬私有雲網路中的多個目的地啟動連線。生產端和消費者網路可以分屬於不同的專案和機構。
如果網路連結接受來自 Private Service Connect 介面的連線,Google Cloud 會從網路連結所指定的用戶子網路中分配 IP 位址。取用端和生產端網路彼此相連,並使用內部 IP 位址進行通訊。
網路連結和 Private Service Connect 介面之間的連線類似於 Private Service Connect 端點與服務連結之間的連線,但有以下兩點主要差異:
- 網路連結可讓生產端網路啟動至消費者網路的連線 (代管服務輸出),而端點則可讓消費者網路啟動連至供應商網路 (代管服務輸入) 的連線。
- Private Service Connect 介面是遞移性質,也就是說,生產端網路可以與其他連線至消費者網路的網路通訊。
建構項目
在此教學課程中,您將會建構一個全方位的 Private Service Connect (PSC) 介面架構,運用 Cloud 防火牆規則來允許及拒絕生產者與消費者運算之間的連線,如圖 1 所示。
圖 1

您會在消費者虛擬私有雲中建立單一 psc-network-連結,進而產生下列用途:
- 建立 Cloud 防火牆規則來允許對獅子進行存取
- 建立 Cloud 防火牆規則,拒絕從熊到老虎的存取要求
- 建立 Cloud 防火牆規則,允許來自 Cosmo 的存取
課程內容
- 如何建立網路連結
- 生產端如何透過網路連結建立 PSC 介面
- 如何建立與生產端之間的通訊
- 如何允許生產端 VM (bear) 存取消費端 VM (獅子)
- 如何禁止生產端 VM (熊) 存取消費者 VM (老虎)
- 如何允許從用戶端 VM (cosmo) 存取生產端 VM (熊)
軟硬體需求
- Google Cloud 專案
- 身分與存取權管理權限
- Compute 網路管理員 (roles/compute.networkAdmin)
- Compute 執行個體管理員 (roles/compute.instanceAdmin)
- Compute 安全管理員 (roles/compute.securityAdmin)
2. 事前準備
更新專案以支援教學課程
本教學課程將使用 $variables,協助在 Cloud Shell 中實作 gcloud 設定。
在 Cloud Shell 中執行以下操作:
gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid
3. 消費者設定
建立用戶端虛擬私有雲
在 Cloud Shell 中執行以下操作:
gcloud compute networks create consumer-vpc --project=$projectid --subnet-mode=custom
建立用戶子網路
在 Cloud Shell 中執行以下操作:
gcloud compute networks subnets create lion-subnet-1 --project=$projectid --range=192.168.20.0/28 --network=consumer-vpc --region=us-central1
在 Cloud Shell 中執行以下操作:
gcloud compute networks subnets create tiger-subnet-1 --project=$projectid --range=192.168.30.0/28 --network=consumer-vpc --region=us-central1
在 Cloud Shell 中執行以下操作:
gcloud compute networks subnets create cosmo-subnet-1 --project=$projectid --range=192.168.40.0/28 --network=consumer-vpc --region=us-central1
建立 Private Service Connect 網路連結子網路
在 Cloud Shell 中執行以下操作:
gcloud compute networks subnets create intf-subnet --project=$projectid --range=192.168.10.0/28 --network=consumer-vpc --region=us-central1
Cloud Router 和 NAT 設定
Cloud NAT 會用於軟體套件安裝教學課程,因為 VM 執行個體沒有公開 IP 位址。Cloud NAT 可讓具有私人 IP 位址的 VM 存取網際網路。
在 Cloud Shell 建立 Cloud Router。
gcloud compute routers create cloud-router-for-nat --network consumer-vpc --region us-central1
在 Cloud Shell 中建立 NAT 閘道。
gcloud compute routers nats create cloud-nat-us-central1 --router=cloud-router-for-nat --auto-allocate-nat-external-ips --nat-all-subnet-ip-ranges --region us-central1
4. 啟用 IAP
如要允許 IAP 連線至您的 VM 執行個體,請建立下列防火牆規則:
- 適用於您要透過 IAP 存取的所有 VM 執行個體。
- 允許來自 IP 範圍 35.235.240.0/20 的輸入流量。這個範圍包含 IAP 用於 TCP 轉送的所有 IP 位址。
在 Cloud Shell 中建立 IAP 防火牆規則。
gcloud compute firewall-rules create ssh-iap-consumer \
--network consumer-vpc \
--allow tcp:22 \
--source-ranges=35.235.240.0/20
5. 建立用戶 VM 執行個體
在 Cloud Shell 中,建立用戶 VM 執行個體 lion。
gcloud compute instances create lion \
--project=$projectid \
--machine-type=e2-micro \
--image-family debian-11 \
--no-address \
--image-project debian-cloud \
--zone us-central1-a \
--subnet=lion-subnet-1 \
--metadata startup-script="#! /bin/bash
sudo apt-get update
sudo apt-get install tcpdump
sudo apt-get install apache2 -y
sudo service apache2 restart
echo 'Welcome to the lion app server !!' | tee /var/www/html/index.html
EOF"
在 Cloud Shell 中,建立用戶 VM 執行個體,tiger。
gcloud compute instances create tiger \
--project=$projectid \
--machine-type=e2-micro \
--image-family debian-11 \
--no-address \
--image-project debian-cloud \
--zone us-central1-a \
--subnet=tiger-subnet-1 \
--metadata startup-script="#! /bin/bash
sudo apt-get update
sudo apt-get install tcpdump
sudo apt-get install apache2 -y
sudo service apache2 restart
echo 'Welcome to the tiger app server !!' | tee /var/www/html/index.html
EOF"
在 Cloud Shell 中,建立用戶 VM 執行個體 cosmo。
gcloud compute instances create cosmo \
--project=$projectid \
--machine-type=e2-micro \
--image-family debian-11 \
--no-address \
--image-project debian-cloud \
--zone us-central1-a \
--subnet=cosmo-subnet-1 \
--metadata startup-script="#! /bin/bash
sudo apt-get update
sudo apt-get install tcpdump
sudo apt-get install apache2 -y
sudo service apache2 restart
echo 'Welcome to the cosmo app server !!' | tee /var/www/html/index.html
EOF"
取得並儲存執行個體的 IP 位址:
在 Cloud Shell 中,對獅子和老虎 VM 執行個體執行描述。
gcloud compute instances describe lion --zone=us-central1-a | grep networkIP:
gcloud compute instances describe tiger --zone=us-central1-a | grep networkIP:
gcloud compute instances describe cosmo --zone=us-central1-a | grep networkIP:
6. Private Service Connect 網路連結
網路連結是區域性資源,代表 Private Service Connect 介面使用者端的區域。您將單一子網路與網路連結建立關聯,供應商則會從該子網路將 IP 指派給 Private Service Connect 介面。子網路必須與網路連結位於相同區域。網路連結必須與生產端服務位於相同區域。
建立網路連結
在 Cloud Shell 中建立網路連結。
gcloud compute network-attachments create psc-network-attachment \
--region=us-central1 \
--connection-preference=ACCEPT_MANUAL \
--producer-accept-list=$projectid \
--subnets=intf-subnet
列出網路連結
在 Cloud Shell 中列出網路連結。
gcloud compute network-attachments list
說明網路連結
在 Cloud Shell 中說明網路連結。
gcloud compute network-attachments describe psc-network-attachment --region=us-central1
記下生產者在建立 Private Service Connect 介面時會使用的 psc-network-attachment URI。範例如下:
user@cloudshell$ gcloud compute network-attachments describe psc-network-attachment --region=us-central1
connectionPreference: ACCEPT_MANUAL
creationTimestamp: '2023-06-06T20:57:12.623-07:00'
fingerprint: 4Yq6xAfaRO0=
id: '3235195049527328503'
kind: compute#networkAttachment
name: psc-network-attachment
network: https://www.googleapis.com/compute/v1/projects/$projectid/global/networks/consumer-vpc
producerAcceptLists:
- $projectid
region: https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1
selfLink: https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1/networkAttachments/psc-network-attachment
subnetworks:
- https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1/subnetworks/intf-subnet
7. 生產端設定
建立生產端虛擬私有雲網路
在 Cloud Shell 中執行以下操作:
gcloud compute networks create producer-vpc --project=$projectid --subnet-mode=custom
建立生產端子網路
在 Cloud Shell 中,建立用於 psc 介面 vNIC0 的子網路。
gcloud compute networks subnets create prod-subnet --project=$projectid --range=10.20.1.0/28 --network=producer-vpc --region=us-central1
8. 啟用 IAP
如要允許 IAP 連線至您的 VM 執行個體,請建立下列防火牆規則:
- 適用於您要透過 IAP 存取的所有 VM 執行個體。
- 允許來自 IP 範圍 35.235.240.0/20 的輸入流量。這個範圍包含 IAP 用於 TCP 轉送的所有 IP 位址。
在 Cloud Shell 中建立 IAP 防火牆規則。
gcloud compute firewall-rules create ssh-iap-producer \
--network producer-vpc \
--allow tcp:22 \
--source-ranges=35.235.240.0/20
9. 建立 Private Service Connect 介面
Private Service Connect 介面可讓生產端虛擬私有雲 (VPC) 網路,為消費者虛擬私有雲網路中的多個目的地啟動連線。生產端和消費者網路可以分屬於不同的專案和機構。
如果網路連結接受來自 Private Service Connect 介面的連線,Google Cloud 會從網路連結所指定的用戶子網路中分配 IP 位址。取用端和生產端網路彼此相連,並使用內部 IP 位址進行通訊。
在 Cloud Shell 中,建立 Private Service Connect 介面 (bear),然後從網路連結描述輸出內容,插入先前識別的 psc-network-attachment URI。
gcloud compute instances create bear --zone us-central1-a --machine-type=f1-micro --can-ip-forward --network-interface subnet=prod-subnet,network=producer-vpc,no-address --network-interface network-attachment=https://www.googleapis.com/compute/v1/projects/$projectid/regions/us-central1/networkAttachments/psc-network-attachment
多 nic 驗證
確認 PSC 介面已設定正確的 IP 位址。vNIC0 會使用生產端 prod-subnet (10.20.1.0/28),vNIC1 會使用取用端 intf-subnet (192.168.10.0/28)。
gcloud compute instances describe bear --zone=us-central1-a | grep networkIP:
範例:
user$ gcloud compute instances describe bear --zone=us-central1-a | grep networkIP:
networkIP: 10.20.1.2
networkIP: 192.168.10.2
10. 更新用戶防火牆規則
建立 Cloud 防火牆規則來允許對獅子進行存取
在 Cloud Shell 中,建立優先順序較高的規則,允許從 link-subnet (intf-subnet) 的 IP 位址範圍輸出至 lion-subnet-1 位址範圍中的目的地。
gcloud compute firewall-rules create allow-limited-egress-to-lion \
--network=consumer-vpc \
--action=ALLOW \
--rules=ALL \
--direction=EGRESS \
--priority=1000 \
--source-ranges="192.168.10.0/28" \
--destination-ranges="192.168.20.0/28" \
--enable-logging
在 Cloud Shell 中,建立輸入允許規則,以針對來自 psc-network-attachment 子網路的流量覆寫默示拒絕輸入規則。
gcloud compute firewall-rules create allow-ingress \
--network=consumer-vpc \
--action=ALLOW \
--rules=ALL \
--direction=INGRESS \
--priority=1000 \
--source-ranges="192.168.10.0/28" \
--enable-logging
建立 Cloud 防火牆規則,拒絕從熊到任何範圍的存取 (包括老虎)
在 Cloud Shell 中建立低優先順序規則,拒絕來自網路連結子網路 intf-subnet 的 IP 位址範圍的所有輸出流量。
gcloud compute firewall-rules create deny-all-egress \
--network=consumer-vpc \
--action=DENY \
--rules=ALL \
--direction=EGRESS \
--priority=65534 \
--source-ranges="192.168.10.0/28" \
--destination-ranges="0.0.0.0/0" \
--enable-logging
建立 Cloud 防火牆規則,允許 Cosmo 存取
在 Cloud Shell 中,建立輸入允許規則,以針對來自 psc-network-attachment 子網路的流量覆寫默示拒絕輸入規則。
gcloud compute firewall-rules create vm-subnet-allow-ingress \
--network=consumer-vpc \
--action=ALLOW \
--rules=ALL \
--direction=INGRESS \
--priority=1000 \
--source-ranges="192.168.40.0/28" \
--destination-ranges="192.168.10.0/28" \
--enable-logging
11. 建立 PSC 介面的 Linux 路徑
請在 PSC 介面執行個體中設定 Linux 路徑,允許生產端與用戶子網路進行通訊。
找出 Private Service Connect 介面的訪客 OS 名稱
如要設定轉送功能,您必須知道 Private Service Connect 介面的訪客 OS 名稱,這個名稱與 Google Cloud 中的介面名稱不同。
在 Cloud Shell 中開啟新分頁,並執行下列操作:
gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid
在 Cloud Shell 中使用 IAP,登入 psc-interface vm 的熊。
gcloud compute ssh bear --project=$projectid --zone=us-central1-a --tunnel-through-iap
在 Cloud Shell 中,取得 psc-interface 執行個體的 IP 位址
ip a
範例:
user@bear:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP group default qlen 1000
link/ether 42:01:0a:14:01:02 brd ff:ff:ff:ff:ff:ff
altname enp0s4
inet 10.20.1.2/32 brd 10.20.1.2 scope global dynamic ens4
valid_lft 85991sec preferred_lft 85991sec
inet6 fe80::4001:aff:fe14:102/64 scope link
valid_lft forever preferred_lft forever
3: ens5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1460 qdisc pfifo_fast state UP group default qlen 1000
link/ether 42:01:c0:a8:0a:02 brd ff:ff:ff:ff:ff:ff
altname enp0s5
inet 192.168.10.2/32 brd 192.168.10.2 scope global dynamic ens5
valid_lft 85991sec preferred_lft 85991sec
inet6 fe80::4001:c0ff:fea8:a02/64 scope link
valid_lft forever preferred_lft forever
找出 PSC 介面的閘道 IP
在網路介面清單中,找出並儲存與 Private Service Connect 介面 IP 位址相關聯的介面名稱,例如 ens5 (vNIC1)
如要設定轉送,您必須先知道 Private Service Connect 介面預設閘道的 IP 位址
在 Cloud Shell 中,我們會使用 1,因為 PSC 介面與 vNIC1 相關聯。
curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/1/gateway -H "Metadata-Flavor: Google" && echo
範例會產生預設 gw 192.168.10.1
user@bear:~$ curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/1/gateway -H "Metadata-Flavor: Google" && echo
192.168.10.1
新增用戶子網路的路徑
您必須為連線至 Private Service Connect 介面的每個用戶子網路,新增路徑至 Private Service Connect 介面的預設閘道。這可確保來自 Private Service Connect 介面的消費者網路輸出流量。
在熊執行個體中,新增路徑至用戶子網路。
sudo ip route add 192.168.20.0/28 via 192.168.10.1 dev ens5
sudo ip route add 192.168.30.0/28 via 192.168.10.1 dev ens5
sudo ip route add 192.168.40.0/28 via 192.168.10.1 dev ens5
驗證路徑表格
在 Cloud Shell 中,驗證新增的路徑。
ip route show
Example.
user@bear:~$ ip route show
default via 10.20.1.1 dev ens4
10.20.1.0/28 via 10.20.1.1 dev ens4
10.20.1.1 dev ens4 scope link
192.168.10.0/28 via 192.168.10.1 dev ens5
192.168.10.1 dev ens5 scope link
192.168.20.0/28 via 192.168.10.1 dev ens5
192.168.30.0/28 via 192.168.10.1 dev ens5
192.168.40.0/28 via 192.168.10.1 dev ens5
12. 驗證大熊與獅子的連線
讓我們確認生產端 VM 執行個體,小熊可透過執行 curl 的方式與用戶執行個體通訊。
以熊執行個體為例,對先前在小熊執行個體教學課程中發現的獅子 IP 位址執行 curl 指令。
curl -v <lions IP Address>
範例:
user@bear:~$ curl -v 192.168.20.2
* Trying 192.168.20.2:80...
* Connected to 192.168.20.2 (192.168.20.2) port 80 (#0)
> GET / HTTP/1.1
> Host: 192.168.20.2
> User-Agent: curl/7.74.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Tue, 06 Jun 2023 03:53:08 GMT
< Server: Apache/2.4.56 (Debian)
< Last-Modified: Mon, 05 Jun 2023 19:41:26 GMT
< ETag: "1e-5fd6716a1e11b"
< Accept-Ranges: bytes
< Content-Length: 30
< Content-Type: text/html
<
Welcome to lion app server !!
* Connection #0 to host 192.168.20.2 left intact
13. 驗證大熊與老虎之間的連線能力已遭封鎖
讓我們查看防火牆記錄,確認輸出防火牆規則封鎖了熊和老虎的存取。
從新的 Cloud 控制台工作階段前往「Logging」→「記錄檔探索工具」→ 選取「顯示查詢」

將下方的查詢字串貼入搜尋欄位,然後選取「串流」
jsonPayload.rule_details.reference="network:consumer-vpc/firewall:deny-all-egress"

在熊執行個體中,對先前在熊執行個體教學課程中發現的老虎 IP 位址執行 curl 指令。curl 最終會逾時。
curl -v <tiger's IP Address>
範例:
user@bear:~$ curl -v 192.168.30.2
* Trying 192.168.30.2:80...
* connect to 192.168.30.2 port 80 failed: Connection timed out
* Failed to connect to 192.168.30.2 port 80: Connection timed out
* Closing connection 0
curl: (28) Failed to connect to 192.168.30.2 port 80: Connection timed out
驗證 Log Explorer 是否已擷取遭拒的防火牆記錄檔。選取記錄項目並展開巢狀欄位,即可查看中繼資料。

14. 驗證 Cosmo 與熊的連線成功連線
開啟新的 Cloud Shell 分頁並更新專案設定。
在 Cloud Shell 中執行以下操作:
gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
projectid=YOUR-PROJECT-NAME
echo $projectid
在 Cloud Shell 中使用 IAP 登入 Cosmo 執行個體。
gcloud compute ssh cosmo --project=$projectid --zone=us-central1-a --tunnel-through-iap
在 Cloud Shell 中,針對先前在教學課程中確認的熊的 IP vNIV1 IP 位址執行連線偵測 (ping)
ping <bears vNIC1 IP Address>
範例:
user@cosmo:~$ ping 192.168.10.2 -c 5
PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data.
64 bytes from 192.168.10.2: icmp_seq=1 ttl=64 time=0.277 ms
64 bytes from 192.168.10.2: icmp_seq=2 ttl=64 time=0.288 ms
64 bytes from 192.168.10.2: icmp_seq=3 ttl=64 time=0.265 ms
64 bytes from 192.168.10.2: icmp_seq=4 ttl=64 time=0.264 ms
64 bytes from 192.168.10.2: icmp_seq=5 ttl=64 time=0.366 ms
15. 清除所用資源
透過 Cloud Shell 刪除教學課程元件。
gcloud compute instances delete bear --zone=us-central1-a --quiet
gcloud compute instances delete lion --zone=us-central1-a --quiet
gcloud compute instances delete tiger --zone=us-central1-a --quiet
gcloud compute instances delete cosmo --zone=us-central1-a --quiet
gcloud compute network-attachments delete psc-network-attachment --region=us-central1 --quiet
gcloud compute firewall-rules delete allow-ingress allow-limited-egress-to-lion deny-all-egress ssh-iap-consumer ssh-iap-producer vm-subnet-allow-ingress --quiet
gcloud compute networks subnets delete cosmo-subnet-1 intf-subnet lion-subnet-1 prod-subnet tiger-subnet-1 --region=us-central1 --quiet
gcloud compute routers delete cloud-router-for-nat --region=us-central1 --quiet
gcloud compute networks delete consumer-vpc --quiet
gcloud compute networks delete producer-vpc --quiet
16. 恭喜
恭喜!您已成功實作防火牆規則,成功設定和驗證 Private Service Connect 介面,而且取用端和生產端連線能力。
您建立了用戶基礎架構,還新增了網路連結,允許生產端建立多 nic VM,用來橋接消費端和生產端的通訊。您已瞭解如何在消費者虛擬私有雲網路中建立防火牆規則,允許連線至消費者和生產端虛擬私有雲中的執行個體。
Cosmopup 認為教學課程非常精彩!

後續步驟
快來看看一些教學課程...
- 透過 Private Service Connect 透過 GKE 發布及使用服務
- 使用 Private Service Connect 發布及使用服務
- 使用 Private Service Connect 和內部 TCP Proxy 負載平衡器,透過混合型網路連線至地端部署服務