瞭解如何使用 Google Cloud 控制台建立及設定全域網路防火牆政策,允許輸出流量前往特定完整網域名稱 (FQDN)。防火牆政策會封鎖來自您網路的所有其他輸出流量。本快速入門導覽會建立含子網路的虛擬私有雲 (VPC) 網路、在 VPC 網路中建立虛擬機器 (VM) 執行個體、設定使用輸出規則的防火牆政策,然後從 VM 測試防火牆政策。
事前準備
- 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
      (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 and Identity-Aware Proxy (IAP) APIs. 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
      (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 and Identity-Aware Proxy (IAP) APIs. 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.
- 確認您具備 Compute 網路管理員角色 (roles/compute.networkAdmin)。
- 請務必瞭解防火牆政策和防火牆政策規則的概念。
- 請務必瞭解 Cloud NGFW 定價。 詳情請參閱 Cloud NGFW 定價。
- 在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。 
- 按一下「建立虛擬私有雲網路」。 
- 在「Name」(名稱) 中輸入 - vpc-fw-policy-egress。
- 在「子網路建立模式」部分,選取「自訂」。 
- 在「New subnet」(新子網路) 部分,指定子網路的以下設定參數: - 「Name」(名稱):輸入 subnet-1。
- 地區:選取 us-central1。
- IPv4 範圍:輸入 10.0.0.0/24。
 
- 「Name」(名稱):輸入 
- 按一下 [完成]。 
- 點選「建立」。 
- 前往 Google Cloud 控制台的「建立執行個體」頁面。 
- 在「機器設定」窗格中,執行下列操作: - 在「Name」(名稱) 中輸入 instance-1-us。
- 在「Region」(區域) 中選取 us-central1 (Iowa)。
 
- 在「Name」(名稱) 中輸入 
- 在導覽選單中,按一下「網路」。 - 在「Network interfaces」(網路介面) 區段中,按一下 default,然後指定下列設定參數:- 網路:vpc-fw-policy-egress
- 「Subnetwork」(子網路):subnet-1 IPv4 (10.0.0.0/24)
- 「外部 IPv4 位址」:選取「無」
 
- 網路:
- 按一下 [完成]。
 
- 在「Network interfaces」(網路介面) 區段中,按一下 
- 點選「建立」。 
- 前往 Google Cloud 控制台的「Cloud NAT」頁面。 
- 按一下「開始使用」或「建立 Cloud NAT 閘道」。 - 注意:如果這是您建立的第一個 Cloud NAT 閘道,請按一下「開始使用」。如果您已有現有閘道, Google Cloud會顯示「建立 Cloud NAT」閘道按鈕。如要建立其他閘道,請按一下「建立 Cloud NAT」閘道。 
- 在「Gateway name」(閘道名稱) 輸入 - fw-egress-nat-gw。
- 在「NAT type」(NAT 類型) 中,選取「Public」(公開)。 
- 在「Select Cloud Router」(選取 Cloud Router) 部分,指定下列設定參數: - 「Network」(網路):選取「vpc-fw-policy-egress」。
- 區域:選取「us-central1 (Iowa)」(us-central1 (愛荷華州))。
- Cloud Router:按一下「建立新路由器」。
- 在「Name」(名稱) 中輸入 fw-egress-router。
- 點選「建立」。
 
- 在「Name」(名稱) 中輸入 
 
- 點選「建立」。 
- 套用至所有您希望透過 IAP TCP 轉送功能存取的 VM。
- 允許來自 IP 位址範圍「35.235.240.0/20」的輸入流量。這個範圍包含 IAP 用於 TCP 轉送的所有 IP 位址。
- 允許連線至您希望透過 IAP TCP 轉送功能存取的所有通訊埠,例如適用於 SSH 的通訊埠「22」。
- 在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。 
- 按一下「建立防火牆政策」。 
- 在「設定政策」部分,於「政策名稱」輸入 - fw-egress-policy。
- 在「部署範圍」部分,選取「全域」,然後點選「繼續」。 
- 如要為政策建立規則,請在「新增規則」部分中,按一下「新增規則」。 - 在「Priority」(優先順序) 中輸入 100。
- 在「Direction of traffic」(流量方向) 中選取 [Ingress] (輸入)。
- 在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。
- 針對「記錄」,選取「開啟」。
- 在「目標」部分中,針對「目標類型」選取「網路中的所有執行個體」。
- 在「來源」部分,於「IP 範圍」中輸入 35.235.240.0/20。
- 在「通訊協定和通訊埠」部分,選取「指定的通訊協定和通訊埠」。
- 勾選「TCP」核取方塊,然後在「Ports」(通訊埠) 輸入 22。
- 點選「建立」。
 
- 在「Priority」(優先順序) 中輸入 
- 按一下「繼續」。 
- 如要將虛擬私有雲網路與政策建立關聯,請在「將政策連結到虛擬私有雲網路」部分中,按一下「連結」。 
- 選取「vpc-fw-policy-egress」核取方塊,然後按一下「Associate」(建立關聯)。 
- 按一下「繼續」。 
- 點選「建立」。 
- 在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。 
- 在「Network firewall policies」(網路防火牆政策) 部分,按一下「fw-egress-policy」。 
- 按一下「建立規則」。 
- 在「Priority」(優先順序) 中輸入 - 700。
- 在「Direction of traffic」(流量方向) 中選取「Egress」(輸出)。 
- 在「Action on match」(相符時執行的動作) 中選取「Deny」(拒絕)。 
- 針對「記錄」,選取「開啟」。 
- 在「Destination」(目的地) 區段中,在「IP ranges」(IP 範圍) 輸入 - 0.0.0.0/0。
- 點選「建立」。 
- 在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。 
- 在「Network firewall policies」(網路防火牆政策) 部分,按一下「fw-egress-policy」。 
- 按一下「建立規則」。 
- 在「Priority」(優先順序) 中輸入 - 600。
- 在「Direction of traffic」(流量方向) 中選取「Egress」(輸出)。 
- 在「Action on match」(相符時執行的動作) 中選取 [Allow] (允許)。 
- 針對「記錄」,選取「開啟」。 
- 在「Destination」(目的地) 區段中,在「FQDNs」(FQDN) 輸入 - ads.google.com。
- 點選「建立」。 
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。 
- 在 - instance-1-usVM 的「Connect」(連線) 欄中,按一下「SSH」。
- 在「SSH-in-browser」(直接透過瀏覽器進行 SSH 連線) 對話方塊中,按一下「Authorize」(授權),然後等待連線建立。 
- 如要確認系統允許前往 https://ads.google.com 的輸出流量,請執行下列指令: - curl -I https://ads.google.com - 上述指令會傳回 https://ads.google.com 的標頭資訊,表示允許輸出連線。 
- 如要確認系統是否封鎖前往任何其他目的地的輸出流量,請指定任何 FQDN 並執行下列指令: - curl -m 2 -I https://mail.yahoo.com - 上述指令會傳回 - Connection timed out訊息,這是預期行為,因為您已建立防火牆規則,拒絕輸出流量前往 https://ads.google.com 以外的所有目的地。
- 在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。 
- 在「Network firewall policies」(網路防火牆政策) 部分,按一下「fw-egress-policy」。 
- 如要查看套用至傳出流量的防火牆規則,請展開個別記錄。展開相關部分,即可查看連線、處置、遠端位置和規則詳細資料。 
- 在 Google Cloud 控制台中,前往「Firewall policies」(防火牆政策) 頁面。 
- 在「Network firewall policies」(網路防火牆政策) 部分,按一下「fw-egress-policy」。 
- 按一下「關聯項目」分頁標籤。 
- 選取 - vpc-fw-policy-egress核取方塊,然後按一下「移除關聯」。
- 在「Remove a firewall policy association」(移除防火牆政策連結關係) 對話方塊中,按一下「Remove」(移除)。 
- 點選「刪除」。 
- 在「刪除防火牆政策」對話方塊中,按一下「刪除」。 
- 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。 
- 勾選 - instance-1-usVM 的核取方塊。
- 點選「刪除」。 
- 在「Delete instance-1-us」(刪除 instance-1-us) 對話方塊中,按一下「Delete」(刪除)。 
- 前往 Google Cloud 控制台的「Cloud routers」頁面。 
- 勾選 - fw-egress-router的核取方塊。
- 點選「刪除」。 
- 在「Delete fw-egress-router」(刪除 fw-egress-router) 對話方塊中,按一下「Delete」(刪除)。 
- 在 Google Cloud 控制台中,前往「VPC networks」(虛擬私有雲網路) 頁面。 
- 在「Name」(名稱) 欄中,按一下「vpc-fw-policy-egress」。 
- 按一下「刪除虛擬私有雲網路」。 
- 在「刪除網路」對話方塊中,按一下「刪除」。 
- 如要建立、更新、監控及刪除虛擬私有雲防火牆規則,請參閱「使用虛擬私有雲防火牆規則」。
建立具有 IPv4 子網路的自訂虛擬私有雲網路
建立含有 IPv4 子網路的自訂模式 VPC 網路。
建立 VM
在您於前一節中設定的子網路中建立 VM。
建立 Cloud Router 和 Cloud NAT 閘道
在上一節中,您建立了沒有外部 IP 位址的 VM。如要讓 VM 存取公用網際網路,請為建立 VM 的相同區域和子網路建立 Cloud Router 和 Cloud NAT 閘道。
建立全域網路防火牆政策,允許 IAP TCP 通道
如要允許網路中的 VM 使用 Identity-Aware Proxy 通道,請建立全域網路防火牆政策,並在政策中新增防火牆規則。IAP 允許管理員存取 VM。
防火牆規則必須具備下列特性:
如要啟用 IAP 存取 vpc-fw-policy-egress 網路中的所有 VM,請按照下列步驟操作:
新增防火牆規則,拒絕所有目的地的輸出流量
如要拒絕將輸出流量傳送到所有目的地,請新增防火牆規則至 fw-egress-policy。
新增防火牆規則,只允許輸出流量傳送至特定 FQDN
如要只允許輸出流量傳送至特定 FQDN,請在 ads.google.com 中新增防火牆規則。fw-egress-policy
測試全域網路防火牆政策
設定全域網路防火牆政策後,請按照下列步驟測試政策:
查看記錄
您可以存取記錄,確認防火牆規則是否已套用至輸出流量。如要查看記錄詳細資料,請按照下列步驟操作:
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本快速入門所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
如要刪除在本快速入門導覽課程中建立的資源,請完成下列工作。
刪除防火牆政策
刪除 VM
刪除 Cloud NAT 閘道和 Cloud Router
刪除 Cloud Router 時,相關聯的 Cloud NAT 閘道也會一併刪除。
刪除虛擬私有雲網路及其子網路
刪除 VPC 網路時,系統也會一併刪除其子網路。