設定防火牆政策的位址群組

本教學課程說明如何為網路中的防火牆政策建立及設定位址群組。 本教學課程會逐步說明如何建立含有子網路的虛擬私有雲 (VPC) 網路、建立專案範圍的位址群組、設定使用位址群組和防火牆規則的防火牆政策,然後測試防火牆規則。詳情請參閱「防火牆政策的位址群組」。

目標

本教學課程將說明如何完成下列工作:

  • 建立兩個含有子網路的自訂虛擬私有雲網路。
  • 建立三個虛擬機器 (VM) 執行個體 (兩個用戶 VM 位於一個虛擬私有雲網路的不同子網路,一個供應商 VM 位於第二個虛擬私有雲網路)。建立的所有 VM 都沒有外部 IP 位址。
  • 在生產者 VM 上安裝 Apache 伺服器。
  • 建立虛擬私有雲網路對接。
  • 建立 Cloud Router 和 Cloud NAT 閘道,讓供應商 VM 存取公開網際網路。
  • 建立特定專案適用的位址群組。
  • 建立全域網路防火牆政策,並加入下列規則:
    • 允許透過 Identity-Aware Proxy (IAP) SSH 連線至 VM。
    • 使用專案範圍的位址群組,允許從允許的用戶 VM 傳送流量至供應商 VM。
  • 測試連線。

下圖顯示兩個自訂虛擬私有雲網路中,us-central1 區域內供應商和用戶 VM 之間的流量。全域網路防火牆政策會使用專案範圍的位址群組規則,允許 vm-consumer-allowed 和 vm-producer VM 之間的輸入流量。vm-consumer-blocked VM 和 vm-producer VM 之間的流量會遭到拒絕,因為每個 VM 都有隱含的輸入防火牆規則,會拒絕所有流量。

全域網路防火牆政策,允許從子網路傳入的輸入流量,傳送至另一個虛擬私有雲網路中的目標 VM。
全域網路防火牆政策,允許從子網路到另一個虛擬私有雲網路中目標 VM 的輸入流量 (按一下可放大)。

事前準備

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. 為專案啟用 Compute Engine API。
  4. 確認您具備 Compute 網路管理員角色 (roles/compute.networkAdmin)。
  5. 為專案啟用 Identity-Aware Proxy API。
  6. 如果偏好使用指令列,請安裝 Google Cloud CLI。如需瞭解這項工具的概念與安裝資訊,請參閱「gcloud CLI 總覽」。

    注意:如果您先前未執行過 Google Cloud CLI,請執行 gcloud init 指令,初始化 gcloud CLI 目錄。

建立含有子網路的用戶虛擬私有雲網路

在本節中,您將建立用戶虛擬私有雲網路,其中包含兩個 IPv4 子網路:subnet-consumer-allowed 和 subnet-consumer-blocked。

控制台

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 按一下「建立虛擬私有雲網路」。

  3. 在「Name」(名稱) 中輸入 vpc-consumer。

  4. 在「子網路建立模式」部分,選取「自訂」。

  5. 在「New subnet」(新的子網路) 部分,指定子網路的下列設定參數:

    • Name (名稱):subnet-consumer-allowed
    • Region (區域):us-central1
    • IPv4 範圍:192.168.10.0/29
  6. 按一下「完成」。

  7. 按一下「Add subnet」(新增子網路),然後指定下列設定參數:

    • Name (名稱):subnet-consumer-blocked
    • Region (區域):us-central1
    • IPv4 範圍:192.168.20.0/29
  8. 按一下「完成」。

  9. 點選「建立」。

gcloud

  1. 在 Google Cloud 控制台啟用 Cloud Shell。

    啟用 Cloud Shell

  2. 如要建立虛擬私有雲網路,請執行下列指令:

    gcloud compute networks create vpc-consumer \
      --subnet-mode=custom
    
  3. 在「Authorize cloud shell」對話方塊中,按一下「Authorize」。

  4. 如要建立子網路,請執行下列指令:

    gcloud compute networks subnets create subnet-consumer-allowed \
      --network=vpc-consumer \
      --region=us-central1 \
      --range=192.168.10.0/29
    
  5. 如要建立其他子網路,請執行下列指令:

    gcloud compute networks subnets create subnet-consumer-blocked \
      --network=vpc-consumer \
      --region=us-central1 \
      --range=192.168.20.0/29
    

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

在本節中,您將建立具有 IPv4 子網路的供應商虛擬私有雲網路。

控制台

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 按一下「建立虛擬私有雲網路」。

  3. 在「Name」(名稱) 中輸入 vpc-producer。

  4. 在「子網路建立模式」部分,選取「自訂」。

  5. 在「New subnet」(新的子網路) 部分,指定子網路的下列設定參數:

    • Name (名稱):subnet-vpc-producer
    • Region (區域):us-central1
    • IPv4 範圍:172.16.10.0/29
  6. 按一下「完成」。

  7. 點選「建立」。

gcloud

  1. 如要建立虛擬私有雲網路,請執行下列指令:

    gcloud compute networks create vpc-producer \
      --subnet-mode=custom
    
  2. 執行下列指令來建立子網路:

    gcloud compute networks subnets create subnet-vpc-producer \
      --network=vpc-producer \
      --region=us-central1 \
      --range=172.16.10.0/29
    

建立 Cloud Router 和 Cloud NAT 閘道

如要讓 vm-producer VM 存取公開網際網路,請建立 Cloud Router 和 Cloud NAT 閘道。

控制台

  1. 前往 Google Cloud 控制台的「Cloud NAT」頁面。

    前往 Cloud NAT

  2. 點選「開始使用」或「建立 Cloud NAT 閘道」。

  3. 在「Gateway name」(閘道名稱) 輸入 nat-gateway-addressgrp。

  4. 在「NAT type」(網路位址轉譯類型) 部分,選取「Public」(公開)。

  5. 在「Select Cloud Router」(選取 Cloud Router) 部分,指定下列設定參數:

    • 網路:vpc-producer
    • 區域:us-central1 (lowa)
    • Cloud Router:點選「建立新路由器」。
      1. 在「Name」(名稱) 中輸入 router-addressgrp。
      2. 點選「建立」。
  6. 點選「建立」。

gcloud

  1. 執行下列指令,建立 Cloud Router:

    gcloud compute routers create router-addressgrp \
      --network=vpc-producer \
      --region=us-central1
    
  2. 執行下列指令,建立 Cloud NAT 閘道:

    gcloud compute routers nats create nat-gateway-addressgrp \
      --router=router-addressgrp \
      --region=us-central1 \
      --auto-allocate-nat-external-ips \
      --nat-all-subnet-ip-ranges
    

建立 VM

在您於上一節建立的虛擬私有雲網路中,為每個子網路建立沒有外部 IP 位址的 VM。

為用戶允許的虛擬私有雲網路建立 VM

在 subnet-consumer-allowed 子網路中建立 VM。

控制台

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「Create an instance」(建立執行個體)

  2. 在「機器設定」窗格中,執行下列操作:

    1. 在「Name」(名稱) 中輸入 vm-consumer-allowed。
    2. 在「Region」(區域) 中選取 us-central1 (Iowa)。
  3. 點選導覽選單中的「Networking」(網路)。

    1. 在「Network interfaces」(網路介面) 部分,按一下 default 並指定下列設定參數:
      • 網路:vpc-consumer
      • 子網路:subnet-consumer-allowed IPv4 (192.168.10.0/29)
      • 外部 IPv4 位址:無
    2. 按一下「完成」。
  4. 點選「建立」。

gcloud

gcloud compute instances create vm-consumer-allowed \
     --network=vpc-consumer \
     --zone=us-central1-a \
     --stack-type=IPV4_ONLY \
     --no-address \
     --subnet=subnet-consumer-allowed

為用戶端封鎖的虛擬私有雲網路建立 VM

在本節中,您會在 subnet-consumer-blocked 子網路中建立 VM。

控制台

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「Create an instance」(建立執行個體)

  2. 在「機器設定」窗格中,執行下列操作:

    1. 在「Name」(名稱) 中輸入 vm-consumer-blocked。
    2. 在「Region」(區域) 中選取 us-central1 (Iowa)。
  3. 點選導覽選單中的「Networking」(網路)。

    1. 在「Network interfaces」(網路介面) 部分,按一下 default 並指定下列設定參數:
      • 網路:vpc-consumer
      • 子網路:subnet-consumer-blocked IPv4 (192.168.20.0/29)
      • 外部 IPv4 位址:無
    2. 按一下「完成」。
  4. 點選「建立」。

gcloud

gcloud compute instances create vm-consumer-blocked \
    --network=vpc-consumer \
    --zone=us-central1-a \
    --stack-type=IPV4_ONLY \
    --no-address \
    --subnet=subnet-consumer-blocked

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

在子網路 subnet-vpc-producer 中建立 VM,並在其中安裝 Apache 伺服器。

控制台

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「Create an instance」(建立執行個體)

  2. 在「機器設定」窗格中,執行下列操作:

    1. 在「Name」(名稱) 中輸入 vm-producer。
    2. 在「Region」(區域) 中選取 us-central1 (Iowa)。
  3. 點選導覽選單中的「Networking」(網路)。

    1. 在「Network interfaces」(網路介面) 部分,按一下 default 並指定下列設定參數:
      • 網路:vpc-producer
      • 子網路:subnet-vpc-producer IPv4 (172.16.10.0/29)
    2. 按一下「完成」。
  4. 在導覽選單中,按一下「進階」,然後在「開機指令碼」欄位輸入下列指令碼:

      #! /bin/bash
      apt-get update
      apt-get install apache2 -y
      a2ensite default-ssl
      a2enmod ssl
      # Read VM network configuration:
      md_vm="http://169.254.169.254/computeMetadata/v1/instance/"
      vm_hostname="$(curl $md_vm/name -H "Metadata-Flavor:Google" )"
      filter="{print \$NF}"
      vm_network="$(curl $md_vm/network-interfaces/0/network \
      -H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
      vm_zone="$(curl $md_vm/zone \
      -H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
      # Apache configuration:
      echo "Page on $vm_hostname in network $vm_network zone $vm_zone" | \
      tee /var/www/html/index.html
      systemctl restart apache2
    

    上述指令碼會在這個 VM 中部署並啟動 Apache 網路伺服器。

  5. 點選「建立」。

gcloud

如要建立生產者 VM,請執行下列指令:

  gcloud compute instances create vm-producer \
      --network=vpc-producer \
      --zone=us-central1-a \
      --stack-type=IPV4_ONLY \
      --no-address \
      --subnet=subnet-vpc-producer \
      --image-project=debian-cloud \
      --image-family=debian-10 \
      --metadata=startup-script='#! /bin/bash
        apt-get update
        apt-get install apache2 -y
        a2ensite default-ssl
        a2enmod ssl
        # Read VM network configuration:
        md_vm="http://169.254.169.254/computeMetadata/v1/instance/"
        vm_hostname="$(curl $md_vm/name -H "Metadata-Flavor:Google" )"
        filter="{print \$NF}"
        vm_network="$(curl $md_vm/network-interfaces/0/network \
        -H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
        vm_zone="$(curl $md_vm/zone \
        -H "Metadata-Flavor:Google" | awk -F/ "${filter}")"
        # Apache configuration:
        echo "Page on $vm_hostname in network $vm_network zone $vm_zone" | \
        tee /var/www/html/index.html
        systemctl restart apache2'

建立虛擬私有雲網路對接連線

如要透過私人連線,連結同一專案中的 vpc-consumer 和 vpc-producer 虛擬私有雲網路,請使用虛擬私有雲網路對接。虛擬私有雲網路對接功能可在兩個虛擬私有雲網路之間建立內部 IP 位址連線,無論這些網路是否屬於相同專案或組織。

將 vpc-consumer 與 vpc-producer 對等互連

如要成功建立虛擬私有雲網路對等互連,您必須分別為 vpc-consumer 和 vpc-producer 網路設定對等互連關聯。

控制台

如要在 vpc-consumer 和 vpc-producer 網路之間建立虛擬私有雲網路對接,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「VPC network peering」(虛擬私有雲網路對接) 頁面。

    前往「VPC network peering」(虛擬私有雲網路對等互連) 頁面

  2. 點選「建立連線」。

  3. 按一下「繼續」。

  4. 在「Name」(名稱) 欄位中,輸入 peering-cp。

  5. 在「您的虛擬私有雲網路」下方,選取「vpc-consumer」。

  6. 在「虛擬私有雲網路名稱」下方,選取「vpc-producer」。

  7. 點選「建立」。

gcloud

如要在 vpc-consumer 和 vpc-producer 之間建立虛擬私有雲網路對接,請執行下列指令:

gcloud compute networks peerings create peering-cp \
    --network=vpc-consumer \
    --peer-network=vpc-producer \
    --stack-type=IPV4_ONLY

將 vpc-producer 網路與 vpc-consumer 網路對接

控制台

如要在 vpc-producer 和 vpc-consumer 之間建立虛擬私有雲網路對接,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「VPC network peering」(虛擬私有雲網路對接) 頁面。

    前往「VPC network peering」(虛擬私有雲網路對等互連) 頁面

  2. 點選「建立連線」。

  3. 按一下「繼續」。

  4. 在「Name」(名稱) 欄位中,輸入 peering-pc。

  5. 在「您的虛擬私有雲網路」下方,選取「vpc-producer」。

  6. 在「虛擬私有雲網路名稱」下方,選取「vpc-consumer」。

  7. 點選「建立」。

gcloud

如要在 vpc-producer 和 vpc-consumer 之間建立虛擬私有雲網路對接,請執行下列指令:

gcloud compute networks peerings create peering-pc \
    --network=vpc-producer \
    --peer-network=vpc-consumer \
    --stack-type=IPV4_ONLY

建立全域網路防火牆政策,啟用 IAP

如要啟用 IAP,請建立全域網路防火牆政策,並新增防火牆規則。IAP 可讓您取得 VM 執行個體的管理員權限。

防火牆規則包含下列特性。

  • 來自 IP 範圍 35.235.240.0/20 的輸入流量。這個範圍包含 IAP 用於 TCP 轉送的所有 IP 位址。
  • 連線至您希望透過 IAP TCP 轉送功能存取的所有通訊埠,例如適用於 SSH 的通訊埠 22。

控制台

如要允許 IAP 存取 vpc-consumer 和 vpc-producer 網路中的所有 VM 執行個體,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 按一下「建立防火牆政策」。

  3. 在「設定政策」步驟中,「政策名稱」請輸入 fw-policy-addressgrp。

  4. 在「Deployment scope」(部署範圍) 部分,選取「Global」(全域),然後點選「Continue」(繼續)。

  5. 在「新增規則」步驟中,按一下「建立防火牆規則」。

    1. 在「Priority」(優先順序) 中輸入 100。
    2. 在「Source」(來源) 部分,「IP ranges」(IP 範圍) 請輸入 35.235.240.0/20。
    3. 在「通訊協定和通訊埠」部分,選取「指定的通訊協定和通訊埠」。
    4. 勾選「TCP」核取方塊,並在「通訊埠」部分輸入 22。
    5. 點選「建立」。
  6. 按一下「繼續」。

  7. 在「Add mirroring rules」(新增鏡像規則) 步驟中,按一下「Continue」(繼續)。

  8. 在「將政策與網路建立關聯」步驟中,按一下「建立關聯」。

  9. 在「Associate policy with VPC networks」(將政策與虛擬私有雲網路建立關聯) 窗格中,勾選 「vpc-producer」和「vpc-consumer」核取方塊,然後按一下 「Associate」(建立關聯)。

  10. 按一下「繼續」。

  11. 點選「建立」。

gcloud

如要允許 IAP 存取 vpc-producer 網路中的 VM 執行個體,請執行下列指令:

  1. 執行下列指令,建立防火牆政策:

    gcloud compute network-firewall-policies create fw-policy-addressgrp \
        --global
    
  2. 如要建立防火牆規則,允許流量傳送至所有目的地並啟用記錄,請執行下列指令:

    gcloud compute network-firewall-policies rules create 100 \
        --firewall-policy=fw-policy-addressgrp \
        --direction=INGRESS \
        --action=ALLOW \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20 \
        --global-firewall-policy
    
  3. 如要將防火牆政策與供應商虛擬私有雲網路建立關聯,請執行下列指令:

    gcloud compute network-firewall-policies associations create \
        --firewall-policy=fw-policy-addressgrp \
        --network=vpc-producer \
        --name=pol-association-vpc-producer \
        --global-firewall-policy
    
  4. 如要將防火牆政策與用戶虛擬私有雲網路建立關聯,請執行下列指令:

    gcloud compute network-firewall-policies associations create \
        --firewall-policy=fw-policy-addressgrp \
        --network=vpc-consumer \
        --name=pol-association-vpc-consumer \
        --global-firewall-policy
    

建立專案範圍的位址群組

建立專案範圍的位址群組,並使用指派給vpc-consumer虛擬私有雲網路 subnet-consumer-allowed子網路的 IP 位址。

如要進一步瞭解專案範圍的位址群組,請參閱「在防火牆政策中使用位址群組」。

控制台

  1. 前往 Google Cloud 控制台的「Address groups」(位址群組) 頁面。

    前往「Address groups」(位址群組)

  2. 按一下「Create Address Group」。

  3. 在「Name」(名稱) 欄位中,輸入 address-group-pc。

  4. 在「Scope」(範圍) 部分,選取「Global」(全域)。

  5. 在「Type」(類型) 部分,選取「IPv4」。

  6. 在「Capacity」(容量) 欄位中輸入 1000。

  7. 在「IP Addresses」(IP 位址) 欄位中輸入 192.168.10.0/29。

  8. 點選「建立」。

gcloud

  1. 如果您是第一次使用 Cloud Shell 終端機,請按一下 Google Cloud 控制台中的alt=''「Activate Cloud Shell」(啟用 Cloud Shell)。

  2. 如要建立位址群組,請執行下列指令:

    gcloud network-security address-groups create address-group-pc \
        --type IPv4 \
        --capacity 1000 \
        --location global
    
  3. 在「Authorize cloud shell」對話方塊中,按一下「Authorize」。

  4. 如要將項目新增至位址群組,請執行下列指令:

    gcloud network-security address-groups add-items address-group-pc \
        --items 192.168.10.0/29 \
        --location global
    

    請注意,IP 範圍 192.168.10.0/29 會指派給 vpc-consumer 虛擬私有雲網路的 subnet-consumer-allowed 子網路。

新增防火牆規則,允許流量傳送至位址群組

如要允許來自 vm-consumer-allowed VM 的輸入連線,請建立防火牆規則,將專案範圍位址群組 address-group-pc 新增為來源 IP 位址。

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 在「Network firewall policies」(網路防火牆政策) 區段中,按一下 fw-policy-addressgrp。

  3. 按一下「Create firewall rule」(建立防火牆規則)。

  4. 在「Priority」(優先順序) 中輸入 150。

  5. 在「記錄」部分,選取「啟用」。

  6. 在「Source」(來源) 區段的「Address group」(位址群組) 清單中,選取 「address-group-pc (PROJECT_ID)」, 然後按一下「OK」(確定)。

    請注意,address-group-pc IP 位址群組的 IP 範圍為 192.168.10.0/29,已指派給用戶虛擬私有雲網路的子網路 subnet-consumer-allowed。

  7. 點選「建立」。

gcloud

如要更新防火牆政策,請執行下列指令:

gcloud compute network-firewall-policies rules create 150 \
    --firewall-policy=fw-policy-addressgrp \
    --direction=INGRESS \
    --action=ALLOW \
    --src-address-groups=projects/PROJECT_ID/locations/global/addressGroups/address-group-pc \
    --layer4-configs=all \
    --global-firewall-policy \
    --enable-logging

測試連線

測試從 vm-consumer-allowed VM 到 vm-producer VM 的連線,以及從 vm-consumer-blocked VM 到 vm-producer VM 的連線。

測試從 vm-consumer-allowed VM 到 vm-producer VM 的流量

控制台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 從 vm-producer VM 的「Internal IP」(內部 IP) 欄,複製 VM 的內部 IP 位址。

  3. 在 vm-consumer-allowed VM 的「連線」欄中,點選「SSH」。

  4. 在「SSH-in-browser」對話方塊中,按一下「Authorize」(授權),然後等待連線建立。

  5. 如要驗證連線,請執行下列指令:

    curl INTERNAL_IP -m 2
    

    將 INTERNAL_IP 替換為 vm-producer VM 的 IP 位址。

    輸出結果會與下列內容相似:

    <!doctype html><html><body><h1>Hello World!</h1></body></html>

  6. 關閉「SSH-in-browser」(透過瀏覽器進行 SSH 連線) 對話方塊。

gcloud

  1. 如要查看 vm-producer VM 的內部 IP 位址,請執行下列指令:

    gcloud compute instances describe vm-producer \
       --zone=us-central1-a \
       --format='get(networkInterfaces[0].networkIP)'
    

    出現提示時,按下 n 鍵確認,然後按下 Enter 鍵。請務必記下 vm-producer VM 的內部 IP 位址。

  2. 如要使用 SSH 連線至 vm-consumer-allowed VM,請執行下列指令:

    gcloud compute ssh vm-consumer-allowed \
       --zone=us-central1-a \
       --tunnel-through-iap
    
  3. 如要驗證連線,請執行下列指令:

    curl INTERNAL_IP -m 2
    

    將 INTERNAL_IP 替換為 vm-producer VM 的內部 IP 位址。

    預期的回覆訊息如下:

    <!doctype html><html><body><h1>Hello World!</h1></body></html>

  4. 如要結束 SSH 連線,請輸入 exit。

測試從 vm-consumer-blocked VM 到 vm-producer VM 的流量

控制台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 從 vm-producer VM 的「Internal IP」(內部 IP) 欄,複製 VM 的內部 IP 位址。

  3. 在 vm-consumer-blocked VM 的「連線」欄中,點選「SSH」。

  4. 在「SSH-in-browser」對話方塊中,按一下「Authorize」(授權),然後等待連線建立。

  5. 如要驗證連線,請執行下列指令:

    curl INTERNAL_IP -m 2
    

    將 INTERNAL_IP 替換為 vm-producer VM 的 IP 位址。

    Connection timed out 訊息是預期會出現的,因為每個 VM 都會建立默示輸入防火牆規則,拒絕所有流量。如要允許流量,請在防火牆政策中新增輸入規則。

  6. 關閉「SSH-in-browser」(透過瀏覽器進行 SSH 連線) 對話方塊。

gcloud

  1. 如要查看 vm-producer VM 的內部 IP 位址,請執行下列指令:

    gcloud compute instances describe vm-producer \
       --zone=us-central1-a \
       --format='get(networkInterfaces[0].networkIP)'
    

    出現提示時,按下 n 鍵確認,然後按下 Enter 鍵。請務必記下 vm-producer VM 的內部 IP 位址。

  2. 如要使用 SSH 連線至 vm-consumer-blocked VM,請執行下列指令:

    gcloud compute ssh vm-consumer-blocked \
       --zone=us-central1-a \
       --tunnel-through-iap
    
  3. 如要驗證連線,請執行下列指令:

    curl INTERNAL_IP -m 2
    

    將 INTERNAL_IP 替換為 vm-producer VM 的內部 IP 位址。

    Connection timed out 訊息是預期會出現的,因為每個 VM 都會建立默示輸入防火牆規則,拒絕所有流量。如要允許流量,請在防火牆政策中新增輸入規則。

  4. 如要結束 SSH 連線,請輸入 exit。

查看記錄

如要確認位址群組防火牆規則是否已套用至輸入流量,請存取記錄。如要查看記錄詳細資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 在「Network firewall policies」(網路防火牆政策) 區段中,按一下 fw-policy-addressgrp 名稱。

  3. 在「Hit count」(命中次數) 欄中,選取您在「新增防火牆規則,允許流量傳送至位址群組」期間建立的規則編號。「Logs Explorer」頁面隨即開啟。

  4. 如要查看套用至輸入流量的防火牆規則,請展開個別記錄。您可以查看規則詳細資料、處置和執行個體詳細資料。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

如要刪除在本教學課程中建立的資源,請完成下列步驟。

刪除位址群組

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 在「Network firewall policies」(網路防火牆政策) 區段中,按一下 fw-policy-addressgrp。

  3. 在「Firewall rules」(防火牆規則) 區段中,勾選防火牆規則 150 的核取方塊。

  4. 按一下 「Delete」(刪除)。

  5. 前往 Google Cloud 控制台的「Address groups」(位址群組) 頁面。

    前往「Address groups」(位址群組)

  6. 在「Address groups」(位址群組) 區段中,勾選 address-group-pc 旁的核取方塊。

  7. 按一下「 刪除」,然後再次點選「刪除」確認操作。

gcloud

  1. 如要刪除與 address-group-pc IP 位址群組相關聯的防火牆規則,請執行下列指令:

    gcloud compute network-firewall-policies rules delete 150 \
        --firewall-policy fw-policy-addressgrp \
        --global-firewall-policy
    
  2. 如要從位址群組中移除現有項目,請執行下列指令:

    gcloud network-security address-groups remove-items address-group-pc \
        --items 192.168.10.0/29 \
        --location global
    
  3. 如要刪除 IP 位址群組,請執行下列指令:

    gcloud network-security address-groups delete address-group-pc \
        --location global
    

    出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

刪除防火牆政策

控制台

  1. 前往 Google Cloud 控制台的「Firewall policies」(防火牆政策) 頁面。

    前往「Firewall policies」(防火牆政策)

  2. 在「Network firewall policies」(網路防火牆政策) 區段中,按一下 fw-policy-addressgrp 名稱。

  3. 按一下「關聯項目」分頁標籤。

  4. 勾選 vpc-producer VM 和 vpc-consumer VM 的核取方塊,然後按一下「Remove association」(移除關聯)。

  5. 在「Remove a firewall policy association」(移除防火牆政策關聯) 對話方塊中,按一下「Remove」(移除)。

  6. 按一下 fw-policy-addressgrp 標題旁邊的「刪除」。

  7. 在「Delete a firewall policy」(刪除防火牆政策) 對話方塊中,按一下「Delete」(刪除)。

gcloud

  1. 移除防火牆政策與虛擬私有雲供應商網路之間的關聯。

    gcloud compute network-firewall-policies associations delete \
      --name=pol-association-vpc-producer \
      --firewall-policy=fw-policy-addressgrp \
      --global-firewall-policy
    
  2. 移除防火牆政策與虛擬私有雲用戶網路之間的關聯。

    gcloud compute network-firewall-policies associations delete \
      --name=pol-association-vpc-consumer \
      --firewall-policy=fw-policy-addressgrp \
      --global-firewall-policy
    
  3. 刪除防火牆政策。

    gcloud compute network-firewall-policies delete fw-policy-addressgrp \
        --global
    

刪除虛擬私有雲網路對等互連

控制台

  1. 前往 Google Cloud 控制台的「VPC network peering」(虛擬私有雲網路對接) 頁面。

    前往「VPC network peering」(虛擬私有雲網路對等互連) 頁面

  2. 勾選 peering-cp 和 peering-pc 的核取方塊。

  3. 點選「刪除」。

  4. 在「Delete 2 peerings?」(要刪除 2 個對接嗎?) 對話方塊中,點選「Delete」(刪除)。

gcloud

  1. 如要刪除用戶端虛擬私有雲和供應商虛擬私有雲之間的對接,請執行下列指令:

    gcloud compute networks peerings delete peering-cp \
        --network=vpc-consumer
    
  2. 如要刪除供應商虛擬私有雲和用戶虛擬私有雲之間的對接,請執行下列指令:

    gcloud compute networks peerings delete peering-pc \
        --network=vpc-producer
    

刪除 Cloud NAT 閘道和 Cloud Router

控制台

  1. 前往 Google Cloud 控制台的「Cloud Router」頁面。

    前往 Cloud Router

  2. 勾選「router-addressgrp」核取方塊。

  3. 點選「刪除」。

  4. 在「Delete router-addressgrp」對話方塊中,點選「Delete」。

刪除 Cloud Router 時,相關聯的 Cloud NAT 閘道也會一併刪除。

gcloud

如要刪除 router-addressgrp Cloud Router,請執行下列指令:

gcloud compute routers delete router-addressgrp \
    --region=us-central1

出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

刪除 Cloud Router 時,相關聯的 Cloud NAT 閘道也會一併刪除。

刪除 VM

控制台

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 勾選 vm-consumer-allowed、vm-consumer-blocked 和 vm-producer VM 的核取方塊。

  3. 點選「刪除」。

  4. 在「Delete 3 instances?」(要刪除 3 個執行個體嗎?) 對話方塊中,點選「Delete」(刪除)。

gcloud

  1. 如要刪除所有 VM,請執行下列指令:

    gcloud compute instances delete vm-consumer-allowed vm-consumer-blocked vm-producer \
        --zone=us-central1-a
    

    出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

刪除用戶虛擬私有雲網路及其子網路

控制台

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 在「Name」(名稱) 欄中,按一下 vpc-consumer。

  3. 按一下「刪除虛擬私有雲網路」。

  4. 在「Delete a network」(刪除網路) 對話方塊中,按一下「Delete」(刪除)。

刪除虛擬私有雲時,子網路也會一併刪除。

gcloud

  1. 如要刪除 vpc-consumer 虛擬私有雲網路的子網路,請執行下列指令:

    gcloud compute networks subnets delete subnet-consumer-allowed subnet-consumer-blocked \
       --region=us-central1
    

    出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

  2. 如要刪除 vpc-consumer 虛擬私有雲網路,請執行下列指令:

    gcloud compute networks delete vpc-consumer
    

    出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

刪除供應商虛擬私有雲網路及其子網路

控制台

  1. 前往 Google Cloud 控制台的「VPC Networks」(虛擬私有雲網路) 頁面。

    前往「VPC networks」(虛擬私有雲網路)

  2. 在「Name」(名稱) 欄中,按一下 vpc-producer。

  3. 按一下「刪除虛擬私有雲網路」。

  4. 在「Delete a network」(刪除網路) 對話方塊中,按一下「Delete」(刪除)。

刪除虛擬私有雲時,子網路也會一併刪除。

gcloud

  1. 如要刪除 vpc-producer 虛擬私有雲網路的子網路,請執行下列指令:

    gcloud compute networks subnets delete subnet-vpc-producer \
       --region=us-central1
    

    出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

  2. 如要刪除 vpc-producer 虛擬私有雲網路,請執行下列指令:

    gcloud compute networks delete vpc-producer
    

    出現提示時,請按下 Y 鍵確認,然後按下 Enter 鍵。

後續步驟