使用 IPv6 靜態路徑下一個躍點執行個體 (未標記和已標記)、下一個躍點位址和下一個躍點閘道

1. 簡介

靜態自訂路徑會影響 VPC 中的預設轉送行為。IPv6 自訂路徑現在支援新的下一個躍點屬性:next-hop-gateway、next-hop-instance 和 next-hop-address。本程式碼研究室說明如何使用 IPv6 自訂路徑,並透過兩個由多個 NIC VM 執行個體連接的 VPC 使用這些新的後續路徑選項。您也會示範如何混合使用 ULA 和 GUA 位址,並使用新的自訂路徑功能,讓 ULA 虛擬私有雲端網路可供公用網際網路存取。

課程內容

  • 如何使用下一個躍點執行個體下一個躍點建立 IPv6 自訂路由。
  • 如何使用 next-hop-gateway 下一個躍點建立 IPv6 自訂路由。
  • 如何使用 next-hop-address 下一個躍點建立 IPv6 自訂路徑。

軟硬體需求

  • Google Cloud 專案

2. 事前準備

更新專案以支援 Codelab

本程式碼研究室會使用 $variables 協助在 Cloud Shell 中實作 gcloud 設定。

在 Cloud Shell 中執行下列操作

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
export projectname=$(gcloud config list --format="value(core.project)")

整體研究室架構

eae86f3e371e74b8.png

為了示範兩種自訂路徑下一個躍點,您將建立 3 個虛擬私有雲網路:使用 GUA 位址的用戶端虛擬私有雲網路、使用 ULA 位址的伺服器虛擬私有雲網路,以及使用 GUA 位址的第二個伺服器虛擬私有雲網路。

如要讓用戶端虛擬私人雲端存取 ULA 伺服器,您必須使用自訂路徑,並同時使用下一個躍點執行個體和下一個躍點位址,指向多個 NIC 閘道執行個體。如要提供對 GUA 伺服器的存取權 (在刪除預設 ::/0 路徑後),您可以使用自訂路徑,並將下一個躍點閘道指向預設網際網路閘道,藉此提供透過網際網路的路由。

3. 用戶端虛擬私有雲設定

建立用戶端 VPC

在 Cloud Shell 中執行下列操作:

gcloud compute networks create client-vpc \
    --project=$projectname \
    --subnet-mode=custom \
    --mtu=1500 --bgp-routing-mode=regional

建立用戶端子網路

在 Cloud Shell 中執行下列操作:

gcloud compute networks subnets create client-subnet  \
    --network=client-vpc \
    --project=$projectname \
    --range=192.168.1.0/24 \
    --stack-type=IPV4_IPV6 \
    --ipv6-access-type=external \
    --region=us-central1

使用下列指令,在環境變數中記錄已指派的 GUA 子網路

export client_subnet=$(gcloud compute networks subnets \
    describe client-subnet \
    --project $projectname \
    --format="value(externalIpv6Prefix)" \
    --region us-central1)

啟動用戶端執行個體

在 Cloud Shell 中執行下列操作:

gcloud compute instances create client-instance \
    --subnet client-subnet \
    --stack-type IPV4_IPV6 \
    --zone us-central1-a \
    --project=$projectname

新增防火牆規則,用於用戶端 VPC 流量

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules create allow-gateway-client \
    --direction=INGRESS --priority=1000 \
    --network=client-vpc --action=ALLOW \
    --rules=tcp --source-ranges=$client_subnet \
    --project=$projectname 

新增防火牆規則,允許用戶端執行個體使用 IAP

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules create allow-iap-client \
    --direction=INGRESS --priority=1000 \
    --network=client-vpc --action=ALLOW \
    --rules=tcp:22 --source-ranges=35.235.240.0/20 \
    --project=$projectname 

確認用戶端執行個體的 SSH 存取權

在 Cloud Shell 中登入用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

如果成功,您會看到來自用戶端執行個體的終端機視窗。結束 SSH 工作階段,繼續使用程式碼研究室。

4. ULA 伺服器 VPC 設定

建立 ULA 伺服器 VPC

在 Cloud Shell 中執行下列操作:

gcloud compute networks create server-vpc1 \
    --project=$projectname \
    --subnet-mode=custom --mtu=1500 \
    --bgp-routing-mode=regional \
    --enable-ula-internal-ipv6

建立 ULA 伺服器子網路

在 Cloud Shell 中執行下列操作:

gcloud compute networks subnets create server-subnet1 \
    --network=server-vpc1 \
    --project=$projectname \
    --range=192.168.0.0/24 \
    --stack-type=IPV4_IPV6 \
    --ipv6-access-type=internal \
    --region=us-central1

使用下列指令,在環境變數中記錄已指派的 ULA 子網路

export server_subnet1=$(gcloud compute networks subnets \
    describe server-subnet1 \
    --project $projectname \
    --format="value(internalIpv6Prefix)" \
    --region us-central1)

啟動使用 ULA 內部 IPv6 位址的伺服器 VM

在 Cloud Shell 中執行下列操作:

gcloud compute instances create server-instance1 \
    --subnet server-subnet1 \
    --stack-type IPV4_IPV6 \
    --zone us-central1-a \
    --project=$projectname

新增防火牆規則,允許從用戶端存取伺服器

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules create allow-client-server1 \
    --direction=INGRESS --priority=1000 \
    --network=server-vpc1 --action=ALLOW \
    --rules=tcp --source-ranges=$client_subnet \
    --project=$projectname 

新增防火牆規則允許 IAP

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules create allow-iap-server1 \
    --direction=INGRESS --priority=1000 \
    --network=server-vpc1 --action=ALLOW \
    --rules=tcp:22 \
    --source-ranges=35.235.240.0/20 \
    --project=$projectname 

在 ULA 伺服器執行個體中安裝 Apache

在 Cloud Shell 中登入用戶端執行個體:

gcloud compute ssh server-instance1 \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在伺服器 VM 殼層中執行下列指令

sudo apt update && sudo apt -y install apache2

確認 Apache 是否正在執行

sudo systemctl status apache2

覆寫預設網頁

echo '<!doctype html><html><body><h1>Hello World! From Server1!</h1></body></html>' | sudo tee /var/www/html/index.html

結束 SSH 工作階段,繼續使用程式碼研究室。

5. GUA 伺服器 VPC 設定

建立 GUA 伺服器 VPC

在 Cloud Shell 中執行下列操作:

gcloud compute networks create server-vpc2 \
    --project=$projectname \
    --subnet-mode=custom --mtu=1500 \
    --bgp-routing-mode=regional

建立 GUA 伺服器子網路

在 Cloud Shell 中執行下列操作:

gcloud compute networks subnets create server-subnet2 \
    --network=server-vpc2 \
    --project=$projectname \
    --range=192.168.0.0/24 \
    --stack-type=IPV4_IPV6 \
    --ipv6-access-type=external \
    --region=us-central1

使用下列指令,在環境變數中記錄已指派的 GUA 子網路

export server_subnet2=$(gcloud compute networks subnets \
    describe server-subnet2 \
    --project $projectname \
    --format="value(externalIpv6Prefix)" \
    --region us-central1)

啟動含有 GUA IPv6 位址的伺服器 VM

在 Cloud Shell 中執行下列操作:

gcloud compute instances create server-instance2 \
    --subnet server-subnet2 \
    --stack-type IPV4_IPV6 \
    --zone us-central1-a \
    --project=$projectname

新增防火牆規則,允許子網路內的存取

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules create allow-client-server2 \
    --direction=INGRESS \
    --priority=1000 \
    --network=server-vpc2 \
    --action=ALLOW \
    --rules=tcp --source-ranges=$client_subnet \
    --project=$projectname 

新增防火牆規則允許 IAP

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules create allow-iap-server2 \
    --direction=INGRESS \
    --priority=1000 \
    --network=server-vpc2 \
    --action=ALLOW \
    --rules=tcp:22 \
    --source-ranges=35.235.240.0/20 \
    --project=$projectname 

確認 SSH 存取權可存取 GUA 伺服器執行個體,並安裝 Apache

在 Cloud Shell 中登入用戶端執行個體:

gcloud compute ssh server-instance2 \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在伺服器 VM 殼層中執行下列指令

sudo apt update && sudo apt -y install apache2

確認 Apache 是否正在執行

sudo systemctl status apache2

覆寫預設網頁

echo '<!doctype html><html><body><h1>Hello World! From Server2!</h1></body></html>' | sudo tee /var/www/html/index.html

結束 SSH 工作階段,繼續使用程式碼研究室。

6. 建立閘道執行個體

刪除用戶端 VPC 的預設路徑

準備將 ULA v6 流量重新導向至多 NIC 執行個體,並停用網際網路出口路由。刪除指向預設網際網路閘道的預設 ::/0 路徑。

在 Cloud Shell 中執行下列操作:

export client_defroutename=$(gcloud compute routes list \
--project $projectname \
--format='value(name)' \
--filter="network:client-vpc AND destRange~'::/0'")

gcloud compute routes delete $client_defroutename \
--project $projectname \
--quiet

啟動閘道多 NIC VM

在 Cloud Shell 中執行下列操作:

gcloud compute instances create gateway-instance \
    --project=$projectname \
    --zone=us-central1-a \
--network-interface=stack-type=IPV4_IPV6,subnet=client-subnet,no-address \
--network-interface=stack-type=IPV4_IPV6,subnet=server-subnet1,no-address \
    --can-ip-forward

設定閘道執行個體

在 Cloud Shell 中登入閘道執行個體 (在執行個體啟動時,可能需要幾分鐘才能成功執行 SSH):

gcloud compute ssh gateway-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在閘道 VM shell 中執行下列指令,啟用 IPv6 轉送功能,並在啟用轉送功能的情況下繼續接受 RA (accept_ra = 2)

sudo sysctl -w net.ipv6.conf.ens4.accept_ra=2
sudo sysctl -w net.ipv6.conf.ens5.accept_ra=2
sudo sysctl -w net.ipv6.conf.ens4.accept_ra_defrtr=1
sudo sysctl -w net.ipv6.conf.all.forwarding=1

驗證執行個體上的 IPv6 路由表

ip -6 route show

範例輸出內容顯示 ULA 和 GUA 子網路路徑,其中預設路徑會指向 GUA 介面。

::1 dev lo proto kernel metric 256 pref medium
2600:1900:4000:7a7f:0:1:: dev ens4 proto kernel metric 256 expires 83903sec pref medium
2600:1900:4000:7a7f::/65 via fe80::4001:c0ff:fea8:101 dev ens4 proto ra metric 1024 expires 88sec pref medium
fd20:3df:8d5c::1:0:0 dev ens5 proto kernel metric 256 expires 83904sec pref medium
fd20:3df:8d5c::/64 via fe80::4001:c0ff:fea8:1 dev ens5 proto ra metric 1024 expires 84sec pref medium
fe80::/64 dev ens5 proto kernel metric 256 pref medium
fe80::/64 dev ens4 proto kernel metric 256 pref medium
default via fe80::4001:c0ff:fea8:101 dev ens4 proto ra metric 1024 expires 88sec pref medium

結束 SSH 工作階段,繼續使用程式碼研究室。

7. 建立並測試閘道執行個體的路徑 (使用執行個體名稱)

在本節中,您將使用閘道執行個體名稱做為下一跳,為用戶端和伺服器 VPC 新增路徑。

記下伺服器位址

在 Cloud Shell 中執行下列操作:

gcloud compute instances list \
   --project $projectname \
   --filter="name~server-instance" \
--format='value[separator=","](name,networkInterfaces[0].ipv6Address,networkInterfaces[0].ipv6AccessConfigs[0].externalIpv6)'

這應該會輸出伺服器執行個體名稱和 IPv6 前置字元。輸出內容範例

server-instance1,fd20:3df:8d5c:0:0:0:0:0,
server-instance2,,2600:1900:4000:71fd:0:0:0:0

請記下這兩個位址,因為您稍後會在用戶端執行個體的 curl 指令中使用這些位址。很遺憾,環境變數無法輕易用於儲存這些資訊,因為這些資訊不會透過 SSH 工作階段傳輸。

從用戶端執行 curl 指令至 ULA 伺服器執行個體

如要查看新增路線前的行為,從用戶端執行個體執行 curl 指令,以便連線至 server-instance1。

在 Cloud Shell 中登入用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中,使用 server1 例項的 ULA IPV6 位址執行 curl (指令會設定 5 秒的短暫逾時時間,以免 curl 等待過久)

curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'

由於用戶端 VPC 尚未設有通往伺服器 VPC 的路徑,因此這個 curl 指令應會逾時。

我們來試著解決這個問題吧!請先結束 SSH 工作階段。

在用戶端 VPC 中新增自訂路徑

因為用戶端虛擬私有雲缺少前往 ULA 前置字串的路徑。我們現在就來新增。

在 Cloud Shell 中執行下列操作:

gcloud compute routes create client-to-server1-route \
   --project=$projectname \
   --destination-range=$server_subnet1 \
   --network=client-vpc \
   --next-hop-instance=gateway-instance \
   --next-hop-instance-zone=us-central1-a

使用 SSH 連回用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中,再次嘗試 curl 至伺服器例項。(這項指令會設定 5 秒的短暫逾時時間,以免 curl 等待太久)

curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'

由於 server1 VPC 尚未透過閘道執行個體建立回用戶端 VPC 的路徑,因此這個 curl 指令仍會逾時。

結束 SSH 工作階段,繼續使用程式碼研究室。

在 ULA Server VPC 中新增自訂路徑

在 Cloud Shell 中執行下列操作:

gcloud compute routes create server1-to-client-route \
   --project=$projectname \
   --destination-range=$client_subnet \
   --network=server-vpc1 \
   --next-hop-instance=gateway-instance \
   --next-hop-instance-zone=us-central1-a

使用 SSH 連回用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中,再次嘗試將 curl 傳送至伺服器例項。

curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'

這個 curl 指令現在已成功,表示您已從用戶端執行個體端對端連線至 ULA 伺服器執行個體。目前只有使用 IPv6 自訂路徑,並將下一個躍點執行個體設為下一個躍點,才能建立這種連線。

輸出內容範例

<user id>@client-instance:~$ curl -m 5.0 -g -6 'http://[fd20:3df:8d5c:0:0:0:0:0]:80/'
<!doctype html><html><body><h1>Hello World! From Server1!</h1></body></html>

結束 SSH 工作階段,繼續使用程式碼研究室。

8. 建立並測試通往閘道執行個體的路徑 (使用執行個體的地址)

在本節中,您將使用閘道執行個體的 IPv6 位址做為下一個躍點,為用戶端和伺服器 VPC 新增路徑。

刪除先前的路線

請刪除使用執行個體名稱的自訂路徑,還原新增任何自訂路徑前的環境。

在 Cloud Shell 中執行下列操作:

gcloud compute routes delete client-to-server1-route  --quiet --project=$projectname
gcloud compute routes delete server1-to-client-route  --quiet --project=$projectname

從用戶端執行 curl 指令至 ULA 伺服器執行個體

如要確認先前路徑已成功刪除,請從用戶端執行個體執行 curl 指令,並將其傳送至伺服器執行個體 1。

在 Cloud Shell 中登入用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中,使用 server1 例項的 ULA IPV6 位址執行 curl (指令會設定 5 秒的短暫逾時時間,以免 curl 等待過久)

curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'

由於用戶端 VPC 不再有通往伺服器 VPC 的路徑,因此這個 curl 指令應會逾時。

取得閘道執行個體的 IPv6 位址

我們必須先取得閘道執行個體的 IPv6 位址,才能編寫使用 next-hop-address 的路徑。

在 Cloud Shell 中執行下列操作:

export gateway_ula_address=$(gcloud compute instances \
   describe gateway-instance \
   --project $projectname  \
   --format='value(networkInterfaces[1].ipv6Address)')

export gateway_gua_address=$(gcloud compute instances \
   describe gateway-instance \
   --project $projectname  \
   --format='value(networkInterfaces[0].ipv6AccessConfigs[0].externalIpv6)')

在用戶端 VPC 中新增自訂路徑

我們現在可以在用戶端 VPC 中重新新增路徑,並使用閘道 GUA 位址做為下一個躍點,而非 ULA 前置字串。

在 Cloud Shell 中執行下列操作:

gcloud compute routes create client-to-server1-route \
   --project=$projectname \
   --destination-range=$server_subnet1 \
   --network=client-vpc \
   --next-hop-address=$gateway_gua_address

使用 SSH 連回用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中,再次嘗試 curl 至伺服器例項。

curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'

如預期,這個 curl 指令仍會逾時,因為 server1 VPC 尚未透過閘道執行個體建立回向用戶端 VPC 的路徑。

結束 SSH 工作階段,繼續使用程式碼研究室。

在 ULA Server VPC 中新增自訂路徑

在 Cloud Shell 中執行下列操作:

gcloud compute routes create server1-to-client-route \
   --project=$projectname \
   --destination-range=$client_subnet \
   --network=server-vpc1 \
   --next-hop-address=$gateway_ula_address

使用 SSH 連回用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中,再次嘗試將 curl 傳送至伺服器例項。

curl -m 5.0 -g -6 'http://[ULA-ipv6-address-of-server1]:80/'

這個 curl 指令現在已成功,表示您已從用戶端執行個體端對端連線至 ULA 伺服器執行個體。目前只有使用 IPv6 自訂路徑,並將下一個躍點位址設為下一個躍點,才能建立這種連線。

輸出內容範例

<user id>@client-instance:~$ curl -m 5.0 -g -6 'http://[fd20:3df:8d5c:0:0:0:0:0]:80/'
<!doctype html><html><body><h1>Hello World! From Server1!</h1></body></html>

結束 SSH 工作階段,繼續使用程式碼研究室。

9. 建立並測試前往網際網路閘道的路徑

在您完成本實驗室的設定後,我們也來測試新的下一個中繼點屬性:next-hop-gateway。

從用戶端執行 curl 指令至 GUA 伺服器執行個體

如要查看新增路線前的行為,從用戶端執行個體執行 curl 指令,以便連線至 server2 的 IP 位址。

在 Cloud Shell 中登入用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端執行個體中,執行 curl 以連線至 IPv6 端點

curl -m 5.0 -g -6 'http://[GUA-ipv6-address-of-server2]:80/'

由於用戶端虛擬私有雲網路只有自己的子網路路徑和通往 server1 虛擬私有雲的路徑,因此這個 curl 指令應會逾時。如要連線至 server2 虛擬私有雲的 GUA 範圍,您必須透過自訂路徑使用預設網際網路閘道。

結束 SSH 工作階段,繼續使用程式碼研究室。

在用戶端 VPC 中新增自訂閘道路徑

在 Cloud Shell 中執行下列操作:

gcloud compute routes create client-to-server2-route \
   --project=$projectname \
   --destination-range=$server_subnet2 \
   --network=client-vpc \
   --next-hop-gateway=default-internet-gateway

使用 SSH 連回用戶端執行個體:

gcloud compute ssh client-instance \
    --project=$projectname \
    --zone=us-central1-a \
    --tunnel-through-iap

在用戶端例項中重複相同的 curl

curl -m 5.0 -g -6 'http://[GUA-ipv6-address-of-server2]:80/'

這個 curl 指令現在應該能順利傳回自訂的「hello」訊息,表示您已成功透過預設網際網路閘道連線至其他伺服器的 IPv6 位址。

輸出內容範例:

<user id>@client-instance:~$ curl -m 5.0 -g -6 'http://[2600:1900:4000:71fd:0:0:0:0]:80/'
<!doctype html><html><body><h1>Hello World! From Server2!</h1></body></html>

結束 SSH 工作階段,並完成本實驗室的清理作業。

10. 清理

清理執行個體

在 Cloud Shell 中執行下列操作:

gcloud compute instances delete client-instance --zone us-central1-a --quiet --project=$projectname

gcloud compute instances delete server-instance1 --zone us-central1-a --quiet --project=$projectname

gcloud compute instances delete server-instance2 --zone us-central1-a --quiet --project=$projectname

gcloud compute instances delete gateway-instance --zone us-central1-a --quiet --project=$projectname

清理子網路

在 Cloud Shell 中執行下列操作:

gcloud compute networks subnets delete client-subnet --region=us-central1 --quiet --project=$projectname

gcloud compute networks subnets delete server-subnet1 --region=us-central1 --quiet --project=$projectname

gcloud compute networks subnets delete server-subnet2 --region=us-central1 --quiet --project=$projectname

清理防火牆規則

在 Cloud Shell 中執行下列操作:

gcloud compute firewall-rules delete allow-iap-client  --quiet --project=$projectname
gcloud compute firewall-rules delete allow-iap-server1  --quiet --project=$projectname
gcloud compute firewall-rules delete allow-iap-server2  --quiet --project=$projectname
gcloud compute firewall-rules delete allow-gateway-client  --quiet --project=$projectname
gcloud compute firewall-rules delete allow-client-server1  --quiet --project=$projectname
gcloud compute firewall-rules delete allow-client-server2  --quiet --project=$projectname

清除自訂路徑

在 Cloud Shell 中執行下列操作:

gcloud compute routes delete client-to-server1-route  --quiet --project=$projectname
gcloud compute routes delete client-to-server2-route  --quiet --project=$projectname
gcloud compute routes delete server1-to-client-route  --quiet --project=$projectname

清理虛擬私有雲

在 Cloud Shell 中執行下列操作:

gcloud compute networks delete client-vpc --quiet --project=$projectname
gcloud compute networks delete server-vpc1 --quiet --project=$projectname
gcloud compute networks delete server-vpc2 --quiet --project=$projectname

11. 恭喜

您已成功使用靜態自訂 IPv6 路徑,並將下一個躍點設為 next-hop-gateway、next-hop-instance 和 next-hop-address。您也驗證了使用這些路徑的端對端 IPv6 通訊。

後續步驟

查看一些程式碼研究室…

延伸閱讀資源和影片

參考文件