本教學課程說明如何使用 VPC 網路對等互連部署軸輻式架構。
本教學課程適用於雲端網路工程師和作業專業人員,他們想在Google Cloud 環境中,使用由 Compute Engine 虛擬機器組成的集中式設備,實作中樞輻射架構。在本教學課程中,您會將這些虛擬機器部署為 NAT 閘道,但您也可以將相同方法用於其他功能,例如新一代防火牆。本教學課程假設您熟悉虛擬私有雲網路和 Compute Engine。
架構
在這個架構中,一組輪輻 VPC 網路會透過中樞 VPC 網路與外部通訊,而流量會透過集中式設備集區 (在本例中為網路位址轉譯 (NAT) 閘道) 轉送。相關路徑會從中樞 VPC 網路匯出至輪輻 VPC 網路。NAT 閘道會設定為內部負載平衡器的後端,並採用新的預設路徑,該路徑的下一個躍點是 Cloud Load Balancing 的內部直通式網路負載平衡器。
您可以使用等價多路徑 (ECMP) 轉送,透過多個路徑達到相同類型的負載分配和高可用性。不過,使用內部直通式網路負載平衡器有下列優點:
- 如果使用健康檢查,流量只會轉送至健康狀態良好的執行個體。使用 ECMP 時,流量會轉送至路徑指向的所有有效執行個體;使用內部直通式網路負載平衡器則可避免出現未使用的路徑。此外,執行個體終止或重新啟動時,也不需要清除路徑。
- 您可以微調健康狀態檢查計時器,因此容錯移轉速度可能更快。如果您使用代管執行個體群組和自動修復功能,仍可自訂健康狀態檢查計時器,但這些計時器是用來重新建立執行個體,而非轉送流量。
Google 也提供Cloud NAT 代管服務,提供高可用性,且不須使用者管理和介入。不過,由於NAT 設定不會匯入對等互連網路,因此這個用途不支援 Cloud NAT。
下圖顯示您將在本教學課程中建構的拓撲。
拓撲包含一個中樞 VPC 網路,以及兩個透過 VPC 網路對等互連與中樞 VPC 網路對等互連的輪輻 VPC 網路。中樞 VPC 網路有兩個 NAT 閘道執行個體,位於內部直通式網路負載平衡器後方。靜態預設路徑 (0/0 NAT-GW-ILB) 會指向內部直通式網路負載平衡器,做為下一個躍點。這個靜態預設路徑會透過虛擬私有雲網路對等互連,使用自訂路徑匯出。
目標
- 建立多個 VPC 網路,並使用軸輻式架構對等互連。
- 在中心 VPC 網路中建立及設定 NAT 閘道。
- 設定內部直通式網路負載平衡器,並將其設為下一個躍點。
- 確認從輻射虛擬私有雲網路到公用網際網路的連線。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
您可以使用 Pricing Calculator,根據預測用量估算費用。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on 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.
-
Verify that billing is enabled for your Google Cloud project.
-
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. -
In the Google Cloud console, on 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.
-
Verify that billing is enabled for your Google Cloud project.
-
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. -
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
在本教學課程中,您將使用 Cloud Shell 執行所有指令。
在 Cloud Shell 中,確認您使用的是您建立或選取的Google Cloud 專案。將
project-id替換為您的 Google Cloud 專案。gcloud config set project project-id export PROJECT_ID=`gcloud config list --format="value(core.project)"`
設定預設運算地區和區域。
gcloud config set compute/region us-central1 gcloud config set compute/zone us-central1-c export REGION=us-central1 export ZONE=us-central1-c在本教學課程中,地區為
us-central1,區域為us-central1-c。在 Cloud Shell 中建立中心虛擬私有雲網路和子網路:
gcloud compute networks create hub-vpc --subnet-mode custom gcloud compute networks subnets create hub-subnet1 \ --network hub-vpc --range 10.0.0.0/24建立名為
spoke1-vpc和spoke2-vpc的輻射虛擬私有雲網路,每個網路各有一個子網路:gcloud compute networks create spoke1-vpc --subnet-mode custom gcloud compute networks create spoke2-vpc --subnet-mode custom gcloud compute networks subnets create spoke1-subnet1 \ --network spoke1-vpc --range 192.168.1.0/24 gcloud compute networks subnets create spoke2-subnet1 \ --network spoke2-vpc --range 192.168.2.0/24在中心虛擬私有雲網路和子網虛擬私有雲網路中建立防火牆規則。這些規則允許來自指定 RFC 1918 範圍的內部流量 (TCP/80 和 443、UDP/53 和 ICMP):
gcloud compute firewall-rules create hub-vpc-web-ping-dns \ --network hub-vpc --allow tcp:80,tcp:443,icmp,udp:53 \ --source-ranges 10.0.0.0/24,192.168.1.0/24,192.168.2.0/24 gcloud compute firewall-rules create spoke1-vpc-web-ping-dns \ --network spoke1-vpc --allow tcp:80,tcp:443,icmp,udp:53 \ --source-ranges 10.0.0.0/24,192.168.1.0/24 gcloud compute firewall-rules create spoke2-vpc-web-ping-dns \ --network spoke2-vpc --allow tcp:80,tcp:443,icmp,udp:53 \ --source-ranges 10.0.0.0/24,192.168.2.0/24在中心 VPC 網路和輻射 VPC 網路中建立防火牆規則,允許 IAP for SSH 存取所有虛擬機器:
gcloud compute firewall-rules create hub-vpc-iap \ --network hub-vpc --allow tcp:22 \ --source-ranges 35.235.240.0/20 gcloud compute firewall-rules create spoke1-vpc-iap \ --network spoke1-vpc --allow tcp:22 \ --source-ranges 35.235.240.0/20 gcloud compute firewall-rules create spoke2-vpc-iap \ --network spoke2-vpc --allow tcp:22 \ --source-ranges 35.235.240.0/20本教學課程使用 Identity-Aware Proxy (IAP) for SSH。詳情請參閱連線至沒有外部 IP 位址的執行個體。
建立防火牆規則,允許中樞 VPC 網路中自動修復執行個體群組的健康狀態檢查:
gcloud compute firewall-rules create hub-vpc-health-checks \ --network hub-vpc --allow tcp:443 --target-tags nat-gw \ --source-ranges 130.211.0.0/22,35.191.0.0/16在 Cloud Shell 中,為 NAT 閘道建立執行個體範本,其中包含設定 NAT 閘道的啟動指令碼:
gcloud compute instance-templates create \ hub-nat-gw-ilbnhop-template \ --network hub-vpc \ --subnet hub-subnet1 \ --machine-type n1-standard-2 --can-ip-forward \ --tags nat-gw --scopes default,compute-rw \ --metadata startup-script='#! /bin/bash apt-get update # Enable IP forwarding: echo 1 > /proc/sys/net/ipv4/ip_forward echo "net.ipv4.ip_forward=1" > /etc/sysctl.d/20-example.conf # Read VM network configuration: md_vm="http://metadata.google.internal/computeMetadata/v1/instance/" md_net="$md_vm/network-interfaces" nic0_gw="$(curl $md_net/0/gateway -H "Metadata-Flavor:Google")" nic0_mask="$(curl $md_net/0/subnetmask -H "Metadata-Flavor:Google")" nic0_addr="$(curl $md_net/0/ip -H "Metadata-Flavor:Google")" nic0_id="$(ip addr show | grep $nic0_addr | tail -c 5)" # Use a web server to pass the health check for this example. # In production, use a more complete test. sudo apt-get update sudo apt-get install apache2 -y sudo a2ensite default-ssl sudo a2enmod ssl echo "Example web page to pass health check" | \ tee /var/www/html/index.html sudo systemctl restart apache2 # Enable IP masquerading iptables -t nat -A POSTROUTING -o $nic0_id -j MASQUERADE'本教學課程使用
n1-standard-2做為執行個體類型,但您可以視需要使用任何其他閘道編號或大小。請務必考量 每個 VM 的最大輸出頻寬等因素。建立 HTTP 健康狀態檢查:
gcloud compute health-checks create http nat-gw-ilbnhop-health-check \ --region us-central1 \ --port 80建立區域性執行個體群組,其中包含分散在單一區域的兩個執行個體:
gcloud compute instance-groups managed create \ hub-nat-gw-ilbnhop-mig \ --region us-central1 --size=2 \ --template=hub-nat-gw-ilbnhop-template \ --health-check nat-gw-ilbnhop-health-check \ --initial-delay 15在本教學課程中,初始延遲時間設為 15 秒。在實際工作環境中部署時,請根據需求自訂這項設定。本教學課程不會使用自動調度資源政策。
建立後端服務並新增執行個體群組:
gcloud compute backend-services create hub-nat-gw-ilbnhop-backend \ --load-balancing-scheme=internal \ --protocol=tcp \ --health-checks=nat-gw-ilbnhop-health-check gcloud compute backend-services add-backend \ hub-nat-gw-ilbnhop-backend \ --instance-group=hub-nat-gw-ilbnhop-mig \ --instance-group-region=us-central1建立轉送規則:
gcloud compute forwarding-rules create \ hub-nat-gw-ilbnhop \ --load-balancing-scheme=internal \ --network=hub-vpc \ --subnet=hub-subnet1 \ --address=10.0.0.10 \ --ip-protocol=TCP \ --ports=all \ --backend-service=hub-nat-gw-ilbnhop-backend \ --backend-service-region=us-central1 \ --service-label=hub-nat-gw-ilbnhop即使轉送規則只定義了 TCP,但當您使用內部直通式網路負載平衡器做為下一個躍點時,轉送規則會將所有流量轉送至後端 VM 的所有通訊埠。內部直通式網路負載平衡器是區域性負載平衡器。
建立新路徑,並將轉送規則設為下一個躍點:
gcloud compute routes create hub-nat-gw-ilbnhop \ --network=hub-vpc \ --destination-range=0.0.0.0/0 \ --next-hop-ilb=hub-nat-gw-ilbnhop \ --next-hop-ilb-region=us-central1 \ --priority=800您可以指定網路標記,讓下一個躍點路徑只適用於已設定標記的用戶端執行個體,但標記不會透過 VPC 網路對等互連匯出或匯入。
從中樞虛擬私有雲刪除預設路徑:
export hub_default_route=$(gcloud compute routes list \ --format="value(name)" --filter="network:hub-vpc AND \ nextHopGateway:default-internet-gateway" | head -n 1) gcloud compute routes delete $hub_default_route -q建立新的已標記路徑,只允許來自 NAT 閘道的流量:
gcloud compute routes create hub-default-tagged \ --network hub-vpc --destination-range 0.0.0.0/0 \ --next-hop-gateway default-internet-gateway \ --priority 700 --tags nat-gw從每個輪輻的 VPC 刪除網際網路的預設路徑:
export spoke1_default_route=$(gcloud compute routes list \ --format="value(name)" --filter="network:spoke1-vpc AND \ nextHopGateway:default-internet-gateway") gcloud compute routes delete $spoke1_default_route -q export spoke2_default_route=$(gcloud compute routes list \ --format="value(name)" \ --filter="network:spoke2-vpc AND nextHopGateway:default-internet-gateway") gcloud compute routes delete $spoke2_default_route -q如果本機路線與匯入路線發生衝突,系統一律會優先採用本機路線。詳情請參閱「轉送順序」。
建立用戶端 VM:
gcloud compute instances create spoke1-client \ --subnet=spoke1-subnet1 --no-address \ --metadata startup-script='#! /bin/bash apt-get update apt-get install dnsutils -y' gcloud compute instances create spoke2-client \ --subnet=spoke2-subnet1 --no-address \ --metadata startup-script='#! /bin/bash apt-get update apt-get install dnsutils -y'在 Cloud Shell 中,從中樞虛擬私有雲網路建立虛擬私有雲連線至輻射虛擬私有雲網路,並啟用路徑匯出旗標:
gcloud compute networks peerings create hub-to-spoke1 \ --network hub-vpc --peer-network spoke1-vpc \ --peer-project $PROJECT_ID \ --export-custom-routes gcloud compute networks peerings create hub-to-spoke2 \ --network hub-vpc --peer-network spoke2-vpc \ --peer-project $PROJECT_ID \ --export-custom-routes從
spoke1虛擬私有雲網路建立虛擬私有雲網路對等互連連線,連至中樞虛擬私有雲網路,並啟用路由匯入標記:gcloud compute networks peerings create spoke1-to-hub \ --network spoke1-vpc --peer-network hub-vpc \ --peer-project $PROJECT_ID \ --import-custom-routes從
spoke2虛擬私有雲網路建立虛擬私有雲網路對等互連連線,連至中樞虛擬私有雲網路,並啟用路由匯入標記:gcloud compute networks peerings create spoke2-to-hub \ --network spoke2-vpc --peer-network hub-vpc \ --peer-project $PROJECT_ID \ --import-custom-routes在 Cloud Shell 中,確認靜態路徑已正確建立,成為啟動指令碼的一部分。
gcloud compute routes list --filter="network:hub-vpc"確認輸出內容中包含
hub-default-tagged和hub-nat-gw-ilbanhop路由:NAME NETWORK DEST_RANGE NEXT_HOP PRIORITY default-route-13a4b635b5eab48c hub-vpc 10.0.0.0/24 hub-vpc 1000 hub-default-tagged hub-vpc 0.0.0.0/0 default-internet-gateway 700 hub-nat-gw-ilbanhop hub-vpc 0.0.0.0/0 10.0.0.10 800 peering-route-3274f1257a9842a0 hub-vpc 192.168.2.0/24 hub-to-spoke2 1000 peering-route-798c5777f13094bc hub-vpc 192.168.1.0/24 hub-to-spoke1 1000
確認
spoke1-vpc轉送表,確保預設路徑已正確匯入:gcloud compute routes list --filter="network:spoke1-vpc"確認輸出內容中是否有以
peering-route開頭的路徑,且0.0.0.0/0為DEST_RANGE值:NAME NETWORK DEST_RANGE NEXT_HOP PRIORITY default-route-75f6ea8f5fc54813 spoke1-vpc 192.168.1.0/24 spoke1-vpc 1000 peering-route-6c7f130b860bfd39 spoke1-vpc 10.0.0.0/24 spoke1-to-hub 1000 peering-route-9d44d362f98afbd8 spoke1-vpc 0.0.0.0/0 spoke1-to-hub 800
透過 IAP 使用 SSH 連線至其中一個用戶端:
gcloud compute ssh spoke1-client --tunnel-through-iap透過 NAT 閘道測試 Google 公用 DNS,確認連線:
sudo hping3 -S -p 80 -c 3 dns.google由於內部直通式網路負載平衡器支援 TCP 和 UDP,因此您無法使用以 ICMP 為基礎的 ping 驗證網際網路連線,必須使用 hping3 等工具。
輸出結果會與下列內容相似:
HPING dns.google (eth0 8.8.4.4): S set, 40 headers + 0 data bytes len=44 ip=8.8.4.4 ttl=126 DF id=0 sport=80 flags=SA seq=0 win=65535 rtt=4.6 ms len=44 ip=8.8.4.4 ttl=126 DF id=0 sport=80 flags=SA seq=1 win=65535 rtt=4.4 ms len=44 ip=8.8.4.4 ttl=126 DF id=0 sport=80 flags=SA seq=2 win=65535 rtt=4.3 ms --- dns.google hping statistic --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 4.3/4.4/4.6 ms
確認用於與網際網路通訊的公開 IP 位址:
curl ifconfig.co輸出內容會顯示其中一個 NAT 閘道執行個體的公開 IP 位址。如果您再次執行指令,輸出內容可能會顯示不同的公開 IP 位址,因為系統會使用設定的內部負載平衡工作階段相依性 (預設為用戶端 IP、通訊協定和通訊埠) 分散連線。
虛擬私有雲網路對等互連不具遞移性,因此輪輻虛擬私有雲網路之間無法透過虛擬私有雲網路對等互連連線。
- 這項設定最適合臨時或非靜態的輸出連結。如果 NAT 閘道集區的大小變更了,TCP 連線可能會重新平衡,而這也許會導致重設已建立的連線。
- 節點不會自動更新,因此如果預設的 Debian 安裝作業有安全漏洞,您需要手動更新映像檔。
- 如果您在多個區域中設有 VM,則須在每個區域中設定 NAT 閘道。
- 每個閘道的頻寬會因硬體類型而異。請務必考量每個 VM 的最大輸出頻寬等因素。閘道失敗時,流量會分散到剩餘的其他閘道。由於執行中的流程並不會重新進行程式設計,因此就算閘道恢復上線,流量也不會立即穩定下來。因此重新調整大小時,請務必允許足夠的額外負荷。
- 想在發生未預期的結果時收到通知,請使用 Cloud Monitoring 監控代管的執行個體群組和網路流量。
- 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.
刪除虛擬私有雲網路對等互連連線:
gcloud compute networks peerings delete spoke2-to-hub \ --network spoke2-vpc -q gcloud compute networks peerings delete spoke1-to-hub \ --network spoke1-vpc -q gcloud compute networks peerings delete hub-to-spoke1 \ --network hub-vpc -q gcloud compute networks peerings delete hub-to-spoke2 \ --network hub-vpc -q刪除執行個體、負載平衡器資源、範本和路徑:
gcloud compute instances delete spoke1-client \ --zone=us-central1-c -q gcloud compute instances delete spoke2-client \ --zone=us-central1-c -q gcloud compute routes delete hub-nat-gw-ilbnhop -q gcloud compute forwarding-rules delete hub-nat-gw-ilbnhop -q gcloud compute backend-services delete -q hub-nat-gw-ilbnhop-backend -q gcloud compute instance-groups managed delete hub-nat-gw-ilbnhop-mig \ --region us-central1 -q gcloud compute health-checks delete nat-gw-ilbnhop-health-check -q gcloud compute instance-templates delete hub-nat-gw-ilbnhop-template -q gcloud compute routes delete hub-default-tagged -q刪除防火牆規則、子網路和虛擬私有雲網路:
gcloud compute firewall-rules delete spoke2-vpc-iap -q gcloud compute firewall-rules delete spoke2-vpc-web-ping-dns -q gcloud compute firewall-rules delete spoke1-vpc-iap -q gcloud compute firewall-rules delete spoke1-vpc-web-ping-dns -q gcloud compute firewall-rules delete hub-vpc-iap -q gcloud compute firewall-rules delete hub-vpc-web-ping-dns -q gcloud compute firewall-rules delete hub-vpc-health-checks -q gcloud compute networks subnets delete spoke1-subnet1 \ --region us-central1 -q gcloud compute networks subnets delete spoke2-subnet1 \ --region us-central1 -q gcloud compute networks subnets delete hub-subnet1 \ --region us-central1 -q gcloud compute networks delete spoke1-vpc -q gcloud compute networks delete spoke2-vpc -q gcloud compute networks delete hub-vpc -q- 請參閱虛擬私有雲設計的最佳做法與參考架構。
- 請參閱虛擬私有雲網路對等互連和內部直通式網路負載平衡器做為下一個躍點文件。
- 請參閱VM 執行個體的特殊設定。
設定環境
建立虛擬私有雲網路和子網路
建立執行個體和必要路徑
建立虛擬私有雲網路對等互連連線
虛擬私有雲網路對等互連是雙向的,因此必須在兩端定義。VPC 網路可以與多個 VPC 網路對等互連,但須遵守限制。如要透過虛擬私有雲網路對等互連連線至預設路徑,請使用「透過虛擬私有雲網路對等互連匯入及匯出自訂路徑」功能。
在本教學課程中,您會在同一個Google Cloud 專案中建立所有虛擬私有雲網路。
驗證路徑傳播和連線
正式環境注意事項
您在本教學課程中建立的設定,會在單一區域中提供兩個 NAT 閘道。不過,ECMP 負載平衡並不完美,且個別流程也不會分散到多個連結,這正是使用新一代防火牆等具狀態裝置時所需的功能。
如要在實際工作環境中部署這項設定,請注意下列事項:
清除所用資源
如要避免付費,最簡單的方法就是刪除您針對教學課程建立的專案。 Google Cloud 或者,您也可以刪除個別資源。
刪除專案
刪除個別資源
如要保留 Google Cloud 專案,可以刪除為本教學課程建立的資源。