Private Service Connect 介面

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 Firewall 規則允許及拒絕從生產者到消費者運算資源的連線,如圖 1 所示。

圖 1

d39bf35e55bdf9e6.png

您會在消費者 VPC 中建立單一 psc-network-attachment,產生下列用途:

  1. 建立 Cloud Firewall 規則,允許從「bear」存取「lion」
  2. 建立 Cloud Firewall 規則,拒絕從 bear 存取 tiger
  3. 建立 Cloud Firewall 規則,允許從 cosmo 存取 bear

課程內容

  • 如何建立網路連結
  • 生產者如何使用網路連結建立 PSC 介面
  • 如何建立從製作人到消費者的通訊
  • 如何允許從生產者 VM (bear) 存取消費者 VM (lion)
  • 如何封鎖從生產者 VM (bear) 到消費者 VM (tiger) 的存取權
  • 如何允許從消費者 VM (cosmo) 存取生產者 VM (bear)

軟硬體需求

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. 消費者設定

建立 Consumer VPC

在 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 中,建立取名為 lion 的消費者 VM 執行個體。

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 執行個體執行說明。

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. 設定 Producer

建立供應商虛擬私有雲網路

在 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 Firewall 規則,允許從 bear 存取 lion

在 Cloud Shell 中,建立優先順序較高的規則,允許從附件子網路 (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 防火牆規則,拒絕從 bear 存取所有範圍 (包括 tiger)

在 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 介面的客體 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 (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 中,由於 PSC 介面與 vNIC1 相關聯,因此我們將使用 1

curl http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/1/gateway -H "Metadata-Flavor: Google" && echo

範例會產生預設閘道 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. 驗證熊與獅子是否成功連線

請執行 curl,確認生產者 VM 執行個體 (bear) 可以與消費者執行個體 (lion) 通訊。

從 bear 執行個體對 lion 的 IP 位址執行 curl,該 IP 位址稍早已在教學課程中從 bear 執行個體識別。

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」→ 選取「Show query」

2ae597e6d970cddf.png

將下列查詢字串貼到搜尋欄位,然後選取「stream」

jsonPayload.rule_details.reference="network:consumer-vpc/firewall:deny-all-egress"

30d7bfae315f2ee3.png

從 bear 執行個體對 tiger 的 IP 位址執行 curl,該 IP 位址是您稍早在教學課程中從 bear 執行個體識別出來的。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

確認記錄探索器已擷取遭拒絕的防火牆記錄。選取記錄項目並展開巢狀欄位,即可查看中繼資料。

5c42a6587300be55.png

14. 驗證 cosmo 是否成功連線至 Bear

開啟新的 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 認為教學課程很棒!

e6d3675ca7c6911f.jpeg

後續步驟

查看一些教學課程…

延伸閱讀和影片

參考文件