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-attachment,以便執行下列用途:
- 建立 Cloud 防火牆規則,允許熊熊存取獅子
- 建立 Cloud 防火牆規則,拒絕熊熊存取老虎
- 建立 Cloud 防火牆規則,允許 cosmo 存取 bear
課程內容
- 如何建立網路連結
- 生產者如何使用網路連結建立 PSC 介面
- 如何建立生產者與消費者之間的通訊
- 如何允許生產者 VM (bear) 存取消費者 VM (lion)
- 如何封鎖從產生者 VM (bear) 存取消費者 VM (tiger) 的存取權
- 如何允許消費者 VM (cosmo) 存取生產者 VM (bear)
軟硬體需求
- Google Cloud 專案
- 身分與存取權管理權限
- Compute 網路管理員 (roles/compute.networkAdmin)
- Compute 執行個體管理員 (roles/compute.instanceAdmin)
- Compute 安全管理員 (roles/compute.securityAdmin)
2. 事前準備
更新專案以支援教學課程
本教學課程會使用 $變數,協助您在 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 中,對 lion 和 tiger VM 執行個體執行 describe 指令。
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
請記下 psc-network-attachment URI,因為生產端在建立 Private Service Connect 介面時會使用這個值。以下範例:
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
多個 FQDN 驗證
驗證 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 中,建立優先順序較高的規則,允許從 attachment-subnet (intf-subnet) 的 IP 位址範圍,傳送至 lion-subnet-1 的 IP 位址範圍。
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 Firewall 規則,拒絕熊熊存取所有範圍 (包括老虎)
在 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 Firewall 規則,允許 Cosmo 存取 Bear
在 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 介面的訪客作業系統名稱
如要設定路由,您必須知道 Private Service Connect 介面的訪客作業系統名稱,這與 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 虛擬機 bear。
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 介面傳出。
在 bear 例項中,將路由新增至消費者子網路。
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 執行個體 bear 能否透過執行 curl 與消費者執行個體 lion 通訊。
從 bear 執行個體,針對先前在教學課程中從 bear 執行個體取得的 lion 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. 驗證熊與老虎的連線是否遭到封鎖
我們來看看防火牆記錄,確認輸出防火牆規則是否封鎖了從 bear 到 tiger 的存取權。
在新的 Cloud 控制台工作階段中,依序前往「Logging」→「Logs Explorer」→「Select Show query」(顯示查詢)
將下列查詢字串貼到搜尋欄位,然後選取「stream」
jsonPayload.rule_details.reference="network:consumer-vpc/firewall:deny-all-egress"
從熊貓執行個體,針對教學課程中先前在熊貓執行個體中找出的 tiger 的 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 中,對教學課程先前所述的 bear 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 負載平衡器,透過混合式網路連線至地端服務