1. 簡介
靜態自訂路徑會影響虛擬私有雲中的預設轉送行為。IPv6 自訂路徑現在支援新的下一個躍點屬性:next-hop-gateway、next-hop-instance 和 next-hop-address。本程式碼研究室說明如何搭配這些新的下一個中繼站選項使用 IPv6 自訂路徑,方法是透過多個 NIC VM 執行個體連線兩個 VPC。您也會示範如何混合使用 ULA 和 GUA 位址,以及如何使用新的自訂路徑功能,為 ULA 虛擬私有雲提供公開網際網路的連線能力。
課程內容
- 如何建立下一個躍點為執行個體的 IPv6 自訂路徑。
- 如何建立 IPv6 自訂路徑,並使用 next-hop-gateway 做為下一個躍點。
- 如何建立 IPv6 自訂路徑,並使用 next-hop-address 下一跳。
軟硬體需求
- 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)")
實驗室整體架構

為示範這兩種自訂路由的下一個躍點,您將建立 3 個虛擬私有雲:使用 GUA 位址的用戶端虛擬私有雲、使用 ULA 位址的伺服器虛擬私有雲,以及使用 GUA 位址的第二個伺服器虛擬私有雲。
如要讓用戶端 VPC 存取 ULA 伺服器,您可以使用自訂路徑,並同時使用 next-hop-instance 和 next-hop-address,指向多個 NIC 閘道執行個體。如要提供 GUA 伺服器的存取權 (刪除預設的 ::/0 路由後),您將使用下一個躍點閘道指向預設網際網路閘道的自訂路由,透過網際網路提供路由。
3. 用戶端 VPC 設定
建立用戶端虛擬私有雲
在 Cloud Shell 中執行下列操作:
gcloud compute networks create client-vpc \
--project=$projectname \
--subnet-mode=custom \
--mtu=1500 --bgp-routing-mode=regional
建立 Client 子網路
在 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 伺服器虛擬私有雲
在 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 伺服器虛擬私有雲
建立 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. 建立閘道執行個體
刪除用戶端虛擬私有雲的預設路徑
準備將 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 中執行下列操作:在 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 殼層中執行下列指令,啟用 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. 建立並測試通往閘道執行個體的路由 (使用執行個體的名稱)
在本節中,您將使用閘道執行個體名稱做為下一個躍點,在用戶端和伺服器虛擬私有雲中新增路徑。
記下伺服器位址
在 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 工作階段傳輸。
從用戶端對 ULA 伺服器執行個體執行 curl 指令
如要查看新增任何路徑前的行為,從用戶端執行個體對 server-instance1 執行 curl 指令。
在 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 中新增自訂路徑
由於用戶端 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/'
這個 curl 指令仍會逾時,因為 server1 VPC 尚未透過閘道執行個體,將路徑傳回用戶端 VPC。
結束 SSH 工作階段,繼續進行程式碼研究室。
在 ULA 伺服器 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
從用戶端對 ULA 伺服器執行個體執行 curl 指令
如要確認先前的路由已成功刪除,請從用戶端執行個體對 server-instance1 執行 curl 指令。
在 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 伺服器 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) 的功能。
從用戶端對 GUA 伺服器執行個體執行 curl 指令
如要查看新增任何路徑前的行為,從用戶端執行個體對 server2 的 IP 位址執行 curl 指令。
在 Cloud Shell 中,登入用戶端執行個體:
gcloud compute ssh client-instance \
--project=$projectname \
--zone=us-central1-a \
--tunnel-through-iap
在用戶端執行個體中,對 IPv6 端點執行 curl
curl -m 5.0 -g -6 'http://[GUA-ipv6-address-of-server2]:80/'
這個 curl 指令應該會逾時,因為用戶端 VPC 只有自己的子網路路徑,以及前往 server1 的 VPC 的路徑。如要連上 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 通訊。
後續步驟
查看一些程式碼研究室…