為生產者-消費者模型設定頻外整合

網路安全整合服務會使用生產者-消費者模型檢查及監控資料。這項服務採用頻外整合和 Packet Mirroring 技術,透過使用者管理的虛擬設備鏡像網路流量。

本教學課程說明如何建立及設定生產者和消費者資源,以設定頻外整合。

目標

本教學課程說明如何在專案中完成下列工作:

  1. 在同一項專案中,為供應商和消費者建立含有子網路的自訂虛擬私有雲 (VPC) 網路。
  2. 為供應商建立及設定供應商資源和鏡像部署資源。內部直通式網路負載平衡器就是供應商資源的一例。
  3. 為用戶建立及設定用戶端和伺服器虛擬機器 (VM) 執行個體、鏡像端點資源,以及防火牆政策,以便鏡像用戶端與伺服器之間的流量。
  4. 測試連線,並確認來自用戶 VM 的網路流量封包已鏡像至供應商的鏡像資源。

下圖顯示供應商和用戶虛擬私有雲網路之間的流量。

單一專案中生產者和消費者元件的高階部署架構。
圖 1. 單一專案中生產者和消費者元件的高階部署架構 (按一下即可放大)。

上圖顯示下列內容:

  • 網路流量透過鏡像端點群組,從用戶端網路流向供應商網路。
  • 供應商網路包含 VM 執行個體、內部直通式網路負載平衡器和鏡像部署作業。
  • 生產者網路的鏡像部署群組包含可用區鏡像部署。
  • 用戶網路包含用戶端和伺服器 VM 執行個體。用戶網路中的防火牆規則和政策會管理流量,而安全性設定檔群組則與防火牆政策相關聯。
  • 用戶網路會連結至鏡像端點群組關聯,該關聯會將用戶網路連結至鏡像端點群組。

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. 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

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

  4. 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

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

  6. 為專案啟用 Compute Engine API。
  7. 在要用於計費的 Google Cloud 專案中,啟用 Network Security API。
  8. 確認您在專案層級具備 Compute 網路管理員角色 (roles/compute.networkAdmin),並在組織層級具備安全性設定檔管理員角色 (roles/networksecurity.securityProfileAdmin)。
  9. 如果偏好透過指令列操作,請安裝 Google Cloud CLI。如需瞭解此工具的概念與安裝資訊,請參閱「gcloud CLI 總覽」。

    注意:如果您先前沒有執行 Google Cloud CLI,請先執行 gcloud init,將 gcloud CLI 目錄初始化。

  10. 取得專案 ID。您需要這個角色,才能建立鏡像和端點資源。如果您使用 gcloud CLI,可以在這裡或稍後在各個指令中替換 PROJECT_ID。
  11. 取得組織 ID。您需要這項權限,才能建立組織層級資源,例如安全性設定檔和安全性設定檔群組。如果您使用的是 gcloud CLI,可以在這裡或稍後在各個指令中替換 ORG_ID。

建立供應端資源

在本節中,您將為生產者建立下列資源:

  • 具有子網路的自訂虛擬私有雲網路。
  • 非代管執行個體群組,其中包含記錄傳入通用網路虛擬化封裝 (GENEVE) 封包的 VM 執行個體。
  • 具備後端服務和轉送規則的內部直通式網路負載平衡器。
  • 允許 Google Cloud 健康狀態檢查的防火牆規則。
  • 一個鏡像部署項目群組和一個鏡像部署項目。

建立自訂虛擬私有雲網路

在本節中,您將建立含有子網路的虛擬私有雲網路。

控制台

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

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

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

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

  4. 在「Description」(說明) 中輸入 Producer VPC network。

  5. 在「Subnets」(子網路) 區段中,執行下列操作:

    1. 在「子網路建立模式」部分,選取「自訂」。
    2. 在「New subnet」(新的子網路) 區段中,輸入以下資訊:
      • Name (名稱):producer-subnet
      • Region (區域):us-west1
      • IP stack type (IP 堆疊類型):IPv4 (單一堆疊)
      • IPv4 範圍:10.10.0.0/16
    3. 按一下「完成」。
  6. 點選「建立」。

  7. 開啟 gcloud CLI 並執行下列指令,提高 Google Cloud 控制台網路的最大傳輸單位 (MTU)。

    gcloud compute networks update producer-network \
        --mtu=1768
    

    在指令中,將 MTU 指定為 1768 個位元組,也就是 Google Cloud 控制台網路的預設 MTU (1460 個位元組) 加上網路安全整合服務 GENEVE 封裝負載 (308 個位元組)。

gcloud

  1. 建立虛擬私有雲網路。

    gcloud compute networks create producer-network \
        --subnet-mode=custom \
        --mtu=1768 \
        --description="Producer VPC network"
    

    在指令中,將 MTU 指定為 1768 個位元組,也就是 Google Cloud 控制台網路的預設 MTU (1460 個位元組) 和網路安全整合服務 GENEVE 封裝負載 (308 個位元組) 的總和。

  2. 在虛擬私有雲網路中建立子網路。

    gcloud compute networks subnets create producer-subnet \
        --network=producer-network \
        --region=us-west1 \
        --range=10.10.0.0/16
    

建立非代管執行個體群組

在本節中,您將建立非代管執行個體群組。

控制台

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往執行個體群組

  2. 點選「建立執行個體群組」。

  3. 按一下「New unmanaged instance group」(新增非代管執行個體群組)。

  4. 在「Name」(名稱) 中輸入 producer-instance-group。

  5. 在「Location」(位置) 區段,「Region」(區域) 選取 us-west1,「Zone」(可用區) 選取 us-west1-b。

  6. 在「Network and instances」(網路和執行個體) 部分,執行下列操作:

    1. 在「Network」(網路) 中選取 producer-network。
    2. 在「Subnetwork」(子網路) 中,選取 producer-subnet。
  7. 點選「建立」。

gcloud

gcloud compute instance-groups unmanaged create producer-instance-group \
    --zone=us-west1-b

設定負載平衡器元件

在本節中,您將建立內部直通式網路負載平衡器的元件,包括後端服務和轉送規則。

控制台

開始設定

  1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。

    前往「Load balancing」(負載平衡) 頁面

  2. 點選「建立負載平衡器」。
  3. 在「Type of load balancer」(負載平衡器類型) 部分,選取「Network Load Balancer (TCP/UDP/SSL)」(網路負載平衡器 (TCP/UDP/SSL)),然後點選「Next」(下一步)。
  4. 在「Proxy or passthrough」(直通或使用 Proxy) 部分,選取「Passthrough load balancer」(直通式負載平衡器),然後點選「Next」(下一步)。
  5. 在「Public facing or internal」(公開或內部) 部分,選取「Internal」(內部),然後點選「Next」(下一步)。
  6. 按一下 [設定]。

基本設定

在「Create internal passthrough Network Load Balancer」(建立內部直通式網路負載平衡器) 頁面中,輸入下列資訊:

  • 負載平衡器名稱:producer-ilb
  • Region (區域):us-west1
  • Network (網路):producer-network

設定後端

  1. 按一下「後端設定」。
  2. 在「Protocol」(通訊協定) 部分,選取「UDP」。
  3. 從「Health check」(健康狀態檢查) 清單中選取「Create a health check」(建立健康狀態檢查),輸入下列資訊,然後按一下「Create」(建立)。
    • 名稱:producer-health-check
    • 範圍:區域
    • 通訊埠:80
    • Proxy 通訊協定:NONE
  4. 在「Backends」(後端) 的「New Backend」(新增後端) 區段中,「IP stack type」(IP 堆疊類型) 請選擇「IPv4 (single-stack)」(IPv4 (單一堆疊))。
  5. 在「Instance group」(執行個體群組) 中,選取 producer-instance-group 執行個體群組,然後按一下「Done」(完成)。

  6. 請先確認「後端設定」旁出現藍色勾號,然後再繼續操作。

設定前端

  1. 在「New Frontend IP and port」(新增的前端 IP 和通訊埠) 區段中,輸入下列資訊,然後按一下「Done」(完成):
    1. 在「Name」(名稱) 中輸入 producer-ilb-fr。
    2. 在「Subnetwork」(子網路) 中,選取 producer-subnet。
    3. 在「Ports」(通訊埠) 部分,選取「Single」(單一),然後在「Port number」(通訊埠編號) 中輸入 6081。
    4. 在「Advanced configuration」(進階設定) 專區的「Packet mirroring」(Packet Mirroring) 部分,選取「Enable this load balancer for packet mirroring」(為這個負載平衡器啟用 Packet Mirroring)。
    5. 請先確認「Frontend configuration」(前端設定) 旁出現藍色勾號,然後再繼續操作。

檢查設定

  1. 點選「檢查並完成」。
  2. 查看負載平衡器設定。
  3. 點選「建立」。

gcloud

  1. 建立區域性健康狀態檢查。

    gcloud compute health-checks create tcp producer-health-check \
        --region=us-west1 \
        --port=80
    
  2. 建立後端服務。

    gcloud compute backend-services create producer-backend-service \
        --protocol=UDP \
        --region=us-west1 \
        --health-checks=producer-health-check \
        --health-checks-region=us-west1 \
        --load-balancing-scheme=INTERNAL
    
  3. 為後端服務建立轉送規則。

    gcloud compute forwarding-rules create producer-ilb-fr \
        --backend-service=producer-backend-service \
        --region=us-west1 \
        --network=producer-network \
        --subnet=producer-subnet \
        --ip-protocol=UDP \
        --load-balancing-scheme=INTERNAL \
        --is-mirroring-collector \
        --ports=6081
    

建立 VM 執行個體並新增至執行個體群組

在本節中,您將建立 VM 執行個體,並使用開機指令碼設定 GENEVE 封裝封包的記錄伺服器。建立 VM 執行個體前,請先取得子網路閘道的 IP 位址。您需要 IP 位址才能執行開機指令碼。

控制台

取得子網路閘道的 IP 位址

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

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

  2. 按一下「producer-network」虛擬私有雲網路。

  3. 點選「子網路」分頁標籤。

  4. 在「Subnets」(子網路) 區段中,記下「Gateway」(閘道) 欄中的閘道 IP 位址。

建立 VM 執行個體

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

    前往 VM 執行個體

    1. 點選「建立執行個體」。
    2. 在「Name」(名稱) 中輸入 producer-instance。
    3. 在「Region」(區域) 中選取 us-west1。
    4. 在「Zone」(可用區) 中選取 us-west1-b。
    5. 將「機型」設為「e2-micro」。
    6. 按一下「Networking」(網路),然後在「Network interfaces」(網路介面) 區段中,按一下「Add a network interface」(新增網路介面) 並設定下列項目:
      • Network (網路):producer-network
      • Subnet (子網路):producer-subnet
      • 外部 IPv4 位址:無
    7. 點選「進階」,並在「開機指令碼」中輸入下列指令碼:

      #!/bin/bash
      # Log incoming packets from the gateway IP and the GENEVE 6081 port.
      iptables -A INPUT -p udp -s '"$GW_IP"'/32 --dport 6081 -j LOG --log-prefix "[NSI MIRRORING] "
      
      # Spin up a simple server for health checks on port 80.
      nohup python3 -u -m http.server 80 &
      

      更改下列內容:

      • GW_IP:子網路閘道的 IP 位址。
    8. 點選「建立」。

將 VM 執行個體新增至執行個體群組

  1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

    前往執行個體群組

  2. 按一下「producer-instance-group」。

  3. 按一下 [編輯]。

  4. 在「VM instances」(VM 執行個體) 區段中,從「Select VMs」(選取 VM) 清單中選取 producer-instance VM。

  5. 按一下「儲存」。

gcloud

  1. 取得子網路閘道的 IP 位址。

    GW_IP=$(gcloud compute networks subnets describe producer-subnet \
        --region=us-west1 \
        --format="get(gatewayAddress)")
    
  2. 建立 VM 執行個體。

    gcloud compute instances create producer-instance \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=producer-subnet,no-address" \
        --metadata=startup-script='#!/bin/bash
         # Log incoming packets from the gateway IP and the GENEVE 6081 port.
         iptables -A INPUT -p udp -s '"$GW_IP"'/32 --dport 6081 -j LOG --log-prefix "[NSI MIRRORING] "
    
         # Spin up a simple server for health checks on port 80.
         nohup python3 -u -m http.server 80 &'
    
    
  3. 將 VM 執行個體新增至執行個體群組。

    gcloud compute instance-groups unmanaged add-instances producer-instance-group \
        --instances=producer-instance \
        --zone=us-west1-b
    
  4. 將執行個體群組新增至後端服務。

    gcloud compute backend-services add-backend producer-backend-service \
        --region=us-west1 \
        --instance-group=producer-instance-group \
        --instance-group-zone=us-west1-b
    

建立防火牆政策並新增防火牆規則

在本節中,您將建立防火牆政策並新增防火牆規則,允許 UDP 流量、 Google Cloud 健康狀態檢查,以及透過 Identity-Aware Proxy (IAP) 連至供應商 VM 執行個體的 SSH 連線。

控制台

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

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

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

  3. 在「Name」(名稱) 欄位中,輸入 producer-firewall-policy。

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

  5. 為政策建立下列規則。

    允許來自虛擬私有雲閘道 IP 位址、使用 GENEVE 通訊埠的 UDP 流量

    1. 按一下「Create firewall rule」(建立防火牆規則),然後設定下列欄位:
      • Priority (優先順序):100
      • Direction of traffic (流量方向):「Ingress」(輸入)
      • Action on match (相符時執行的動作):Allow (允許)
      • Source filters > IP ranges: GATEWAY_IP
      • 「Protocol and ports」(通訊協定和通訊埠):選取「Specified protocol and ports」(指定的通訊協定和通訊埠),勾選「UDP」核取方塊,然後在「Ports」(通訊埠) 中指定 6081。
    2. 點選「建立」。

    將 GATEWAY_IP 替換為子網路閘道的 IP 位址。

    允許 Google Cloud 健康狀態檢查

    1. 按一下「Create firewall rule」(建立防火牆規則),然後設定下列欄位:

      • Priority (優先順序):101
      • Direction of traffic (流量方向):「Ingress」(輸入)
      • Action on match (相符時執行的動作):Allow (允許)
      • IP 範圍:35.191.0.0/16 和 130.211.0.0/22
      • 「Protocol and ports」(通訊協定和通訊埠):選取「Specified protocol and ports」(指定的通訊協定和通訊埠),勾選「TCP」核取方塊,然後在「Ports」(通訊埠) 中指定 80。
    2. 點選「建立」。

    允許透過 Identity-Aware Proxy 建立與生產者 VM 執行個體的 SSH 連線

    1. 按一下「Create firewall rule」(建立防火牆規則),然後設定下列欄位:
      • Priority (優先順序):102
      • Direction of traffic (流量方向):「Ingress」(輸入)
      • Action on match (相符時執行的動作):Allow (允許)
      • IP 範圍:35.235.240.0/20
      • 「Protocol and ports」(通訊協定和通訊埠):選取「Specified protocol and ports」(指定的通訊協定和通訊埠),勾選「TCP」核取方塊,然後在「Ports」(通訊埠) 中指定 22。
    2. 點選「建立」。
  6. 如要將政策與網路建立關聯,請依序點選「Continue」(繼續) 和「Associate」(建立關聯)。

  7. 勾選「producer-network」核取方塊。

  8. 按一下「繼續」。

  9. 點選「建立」。

gcloud

  1. 建立全域網路防火牆政策。

    gcloud compute network-firewall-policies create producer-firewall-policy \
        --global
    
  2. 將防火牆政策與供應商網路建立關聯。

    gcloud compute network-firewall-policies associations create \
        --name=producer-firewall-policy-assoc \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --network=producer-network
    
  3. 建立防火牆規則,允許從虛擬私有雲閘道 IP 位址,透過 GENEVE 通訊埠建立 UDP 連線。

    gcloud compute network-firewall-policies rules create 100 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=udp:6081 \
        --src-ip-ranges=$GW_IP/32
    
  4. 建立防火牆規則,允許 Google Cloud 健康狀態檢查。

    gcloud compute network-firewall-policies rules create 101 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:80 \
        --src-ip-ranges=35.191.0.0/16,130.211.0.0/22 # Google Cloud health check ranges
    
  5. 建立防火牆規則,允許透過 Identity-Aware Proxy 連線至生產端 VM 執行個體。

    gcloud compute network-firewall-policies rules create 102 \
        --firewall-policy=producer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20 # Google Cloud IAP range
    

建立供應端鏡像資源

在本節中,您將建立鏡像部署項目群組和鏡像部署項目。

控制台

  1. 前往 Google Cloud 控制台的「Deployment groups」(部署作業群組) 頁面。

    前往「Deployment groups」(部署作業群組)

  2. 按一下「建立部署作業群組」。

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

  4. 在「Network」(網路) 中選取 producer-network。

  5. 在「用途」部分,選取「頻帶外 NSI」。

  6. 在「Mirroring deployments」(鏡像部署作業) 區段中,按一下「Create mirroring deployment」(建立鏡像部署作業),指定下列欄位,然後按一下「Create」(建立):

    • Name (名稱):producer-deployment。
    • 區域:us-west1。
    • 可用區:us-west1-b。
    • 內部負載平衡器:producer-ilb。
  7. 點選「建立」。

gcloud

  1. 建立鏡像部署作業群組。

    gcloud network-security mirroring-deployment-groups create producer-deployment-group \
        --location=global \
        --network=projects/PROJECT_ID/global/networks/producer-network \
        --no-async
    

    將 PROJECT_ID 替換為專案 ID。

  2. 建立鏡像部署項目。

    gcloud network-security mirroring-deployments create producer-deployment \
        --location=us-west1-b \
        --forwarding-rule=producer-ilb-fr \
        --forwarding-rule-location=us-west1 \
        --mirroring-deployment-group=projects/PROJECT_ID/locations/global/mirroringDeploymentGroups/producer-deployment-group \
        --no-async
    

建立用戶端資源

在本節中,您將為用戶建立下列資源:

  • 含有子網路的自訂虛擬私有雲網路
  • 伺服器和用戶端 VM
  • 防火牆政策和流量鏡像規則
  • 鏡像端點群組和鏡像端點群組關聯
  • 安全性設定檔和安全性設定檔群組

建立自訂虛擬私有雲網路

在本節中,您將建立含有子網路的虛擬私有雲網路。

控制台

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

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

  2. 按一下「Create VPC network」(建立虛擬私有雲網路)。

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

  4. 在「Description」(說明) 中輸入 Consumer VPC network。

  5. 在「Subnets」(子網路) 區段中,執行下列操作:

    1. 在「子網路建立模式」部分,選取「自訂」。
    2. 在「New subnet」(新的子網路) 區段中,輸入以下資訊:
      • Name (名稱):consumer-subnet
      • Region (區域):us-west1
      • IP stack type (IP 堆疊類型):IPv4 (單一堆疊)
      • IPv4 範圍:10.11.0.0/16
    3. 按一下「完成」。
  6. 點選「建立」。

gcloud

  1. 建立用戶虛擬私有雲網路。

    gcloud compute networks create consumer-network \
        --subnet-mode=custom \
        --description="Consumer VPC network"
    
  2. 在虛擬私有雲網路中建立子網路。

    gcloud compute networks subnets create consumer-subnet \
        --network=consumer-network \
        --region=us-west1 \
        --range=10.11.0.0/16
    

建立伺服器和用戶端 VM

在本節中,您將建立伺服器和用戶端 VM。

控制台

建立伺服器 VM

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

    前往 VM 執行個體

  2. 點選「建立執行個體」。

  3. 將「Name」(名稱) 設定為 consumer-server-vm。

  4. 將「區域」設為 us-west1。

  5. 將「可用區」設為 us-west1-b。

  6. 將「機型」設為「e2-micro」。

  7. 按一下「Networking」(網路),然後在「Network interfaces」(網路介面) 區段中,按一下「Add a network interface」(新增網路介面) 並設定下列項目:

    • Network (網路):consumer-network
    • Subnet (子網路):consumer-subnet
    • 外部 IPv4 位址:無
  8. 點選「進階」,並在「開機指令碼」中輸入下列指令碼:

    echo success > /tmp/connection_test && nohup python3 -u -m http.server --directory /tmp 8000 &
    

  9. 點選「建立」。

建立用戶端 VM

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

    前往 VM 執行個體

  2. 點選「建立執行個體」。

  3. 將「Name」(名稱) 設定為 consumer-client-vm。

  4. 將「區域」設為 us-west1。

  5. 將「可用區」設為 us-west1-b。

  6. 將「機型」設為「e2-micro」。

  7. 按一下「Networking」(網路),然後在「Network interfaces」(網路介面) 區段中,按一下「Add a network interface」(新增網路介面) 並設定下列項目:

    • Network (網路):consumer-network
    • Subnet (子網路):consumer-subnet
    • 外部 IPv4 位址:無
  8. 點選「建立」。

gcloud

  1. 建立伺服器 VM。

    gcloud compute instances create consumer-server-vm \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=consumer-subnet,no-address" \
        --metadata=startup-script="echo success > /tmp/connection_test && nohup python3 -u -m http.server --directory /tmp 8000 &"
    
  2. 建立用戶端 VM。

    gcloud compute instances create consumer-client-vm \
        --image-project=debian-cloud \
        --image-family=debian-11 \
        --machine-type=e2-micro \
        --zone=us-west1-b \
        --network-interface="subnet=consumer-subnet,no-address"
    

建立防火牆政策,允許輸入流量

在本節中,您將建立防火牆政策,並新增防火牆規則,允許輸入流量傳送至用戶 VM。

控制台

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

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

  2. 按一下「 Create firewall policy」(建立防火牆政策)。

  3. 在「Name」(名稱) 欄位中,輸入 consumer-firewall-policy。

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

  5. 按一下「Create firewall rule」(建立防火牆規則),設定下列欄位,然後點選「Create」(建立):

    • Priority (優先順序):101
    • Direction of traffic (流量方向):「Ingress」(輸入)
    • Action on match (相符時執行的動作):Allow (允許)
    • Source filters > IP ranges: 35.235.240.0/20
    • 「Protocol and ports」(通訊協定和通訊埠):選取「Specified protocol and ports」(指定的通訊協定和通訊埠),勾選「TCP」核取方塊,然後在「Ports」(通訊埠) 中指定 22。

    IPv4 範圍 35.235.240.0/20 包含 Identity-Aware Proxy 用於 TCP 轉送的所有 IP 位址。詳情請參閱「準備專案以進行 IAP TCP 轉送」。

  6. 如要允許 TCP 通訊埠 8000 上的流量進入伺服器 VM,請按一下「Create firewall rule」(建立防火牆規則),然後設定下列欄位:

    • Priority (優先順序):102
    • Direction of traffic (流量方向):「Ingress」(輸入)
    • Action on match (相符時執行的動作):Allow (允許)
    • Source filters > IP ranges: 10.11.0.0/16
    • 「Protocol and ports」(通訊協定和通訊埠):選取「Specified protocol and ports」(指定的通訊協定和通訊埠),勾選「TCP」核取方塊,然後在「Ports」(通訊埠) 中指定 8000。
    • 按一下「Create」(建立)
  7. 如要將政策與網路建立關聯,請依序點選「Continue」(繼續) 和「Associate」(建立關聯)。

  8. 勾選「consumer-network」核取方塊。

  9. 按一下「繼續」。

  10. 點選「建立」。

gcloud

  1. 建立全域網路防火牆政策。

    gcloud compute network-firewall-policies create consumer-firewall-policy \
        --global
    
  2. 將防火牆政策與用戶網路建立關聯。

    gcloud compute network-firewall-policies associations create \
        --name=consumer-firewall-policy-assoc \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --network=consumer-network
    
  3. 建立 SSH 允許規則,允許透過 Identity-Aware Proxy 建立與用戶端 VM 執行個體的 SSH 連線。

    gcloud compute network-firewall-policies rules create 101 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:22 \
        --src-ip-ranges=35.235.240.0/20 # Google Cloud IAP range
    

    IPv4 範圍 35.235.240.0/20 包含 IAP 用於 TCP 轉送的所有 IP 位址。詳情請參閱「準備專案以進行 IAP TCP 轉送」。

  4. 建立防火牆規則,允許 TCP 通訊埠 8000 的流量進入伺服器 VM。

    gcloud compute network-firewall-policies rules create 102 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=allow \
        --direction=INGRESS \
        --layer4-configs=tcp:8000 \
        --src-ip-ranges=10.11.0.0/16
    

建立用戶端點群組

在本節中,您將建立鏡像端點群組和鏡像端點群組關聯。

控制台

  1. 前往 Google Cloud 控制台的「Endpoint groups」(端點群組) 頁面。

    前往「Endpoint groups」(端點群組) 頁面

  2. 按一下「Create endpoint group」(建立端點群組)。

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

  4. 在「用途」部分,選取「頻帶外 NSI」。

  5. 在「Deployment group」(部署作業群組) 部分,選取「In project」(在專案中)。

  6. 在「Deployment group name」(部署作業群組名稱) 中輸入 producer-deployment-group。

  7. 按一下「繼續」。

  8. 按一下「Add endpoint group association」(新增端點群組關聯)。

  9. 在「Project」(專案) 選取目前的專案。

  10. 在「Network」(網路) 中選取 consumer-network。

  11. 點選「建立」。

gcloud

  1. 建立鏡像端點群組。

    gcloud network-security mirroring-endpoint-groups create consumer-endpoint-group \
        --location=global \
        --mirroring-deployment-group=projects/PROJECT_ID/locations/global/mirroringDeploymentGroups/producer-deployment-group \
        --no-async
    

    將 PROJECT_ID 替換為專案 ID。

  2. 建立鏡像端點群組關聯。

    gcloud network-security mirroring-endpoint-group-associations create consumer-endpoint-group-association \
        --location=global \
        --mirroring-endpoint-group=projects/PROJECT_ID/locations/global/mirroringEndpointGroups/consumer-endpoint-group \
        --network=consumer-network \
        --no-async
    

建立安全性設定檔和安全性設定檔群組

建立安全性設定檔群組和自訂安全性設定檔,以便鏡像流量。

控制台

建立自訂安全性設定檔

  1. 前往 Google Cloud 控制台的「Security profiles」(安全設定檔) 頁面。

    前往「Security profiles」(安全性設定檔) 頁面

  2. 在專案挑選器中選取貴組織。

  3. 在「安全性設定檔」分頁中,點選「建立設定檔」。

  4. 在「Name」(名稱) 中輸入 consumer-security-profile。

  5. 在「用途」部分,選取「頻帶外 NSI」。

  6. 在「Project」(專案) 選取目前的專案。

  7. 在「Endpoint group」(端點群組) 部分,選取「consumer-endpoint-group」。

  8. 點選「建立」。

建立安全性設定檔群組

  1. 前往 Google Cloud 控制台的「Security profile groups」(安全設定檔群組) 頁面。

    前往「Security profile groups」(安全性設定檔群組)

  2. 在專案挑選器中選取貴組織。

  3. 在「安全性設定檔群組」分頁中,點選「建立設定檔群組」。

  4. 在「Name」(名稱) 中輸入 consumer-security-profile-group。

  5. 在「用途」部分,選取「頻帶外 NSI」。

  6. 在「Custom mirroring profile」(自訂鏡像設定檔) 部分,選取「consumer-security-profile」。

  7. 點選「建立」。

gcloud

  1. 建立自訂鏡像安全性設定檔。

    gcloud network-security security-profiles custom-mirroring create consumer-security-profile \
        --location=global \
        --organization=ORG_ID \
        --mirroring-endpoint-group=projects/PROJECT_ID/locations/global/mirroringEndpointGroups/consumer-endpoint-group \
        --billing-project=PROJECT_ID \
        --no-async
    

    更改下列內容:

    • ORG_ID:組織 ID。安全性設定檔是組織層級的資源。如要建立,您必須具備組織層級的安全防護設定檔管理員角色 (networksecurity.securityProfileAdmin)。
    • PROJECT_ID:專案 ID。
  2. 建立鏡像安全性設定檔群組。

    gcloud network-security security-profile-groups create consumer-security-profile-group \
        --location=global \
        --organization=ORG_ID \
        --custom-mirroring-profile=organizations/ORG_ID/locations/global/securityProfiles/consumer-security-profile \
        --billing-project=PROJECT_ID \
        --no-async
    

    更改下列內容:

    • ORG_ID:組織 ID。安全性設定檔群組是組織層級的資源。如要建立,您必須具備組織層級的安全設定檔管理員角色 (networksecurity.securityProfileAdmin)。
    • PROJECT_ID:專案 ID。

建立防火牆政策規則,以便鏡像流量

在這個部分,您將建立鏡像規則來鏡像流量。

控制台

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

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

  2. 按一下「consumer-firewall-policy」。

  3. 按一下「鏡像規則」分頁標籤。

  4. 按一下「Create mirroring rule」(建立鏡像規則),然後設定下列欄位:

    • Priority (優先順序):100
    • Direction of traffic (流量方向):「Ingress」(輸入)
    • Action on match (相符時執行的動作):Mirror
    • 安全性設定檔群組: consumer-security-profile-group
    • 來源:IPv4
    • IP 範圍:10.11.0.0/16
    • 「Protocol and ports」(通訊協定和通訊埠):選取「Specified protocol and ports」(指定的通訊協定和通訊埠),勾選「TCP」核取方塊,然後在「Ports」(通訊埠) 中指定 8000。
  5. 點選「建立」。

gcloud

  1. 新增防火牆規則,在伺服器 VM 的 TCP 通訊埠 8000 建立流量鏡像。

    gcloud compute network-firewall-policies mirroring-rules create 100 \
        --firewall-policy=consumer-firewall-policy \
        --global-firewall-policy \
        --action=mirror \
        --security-profile-group=organizations/ORG_ID/locations/global/securityProfileGroups/consumer-security-profile-group \
        --direction=INGRESS \
        --layer4-configs=tcp:8000 \
        --src-ip-ranges=10.11.0.0/16
    

    將 ORG_ID 替換為組織 ID。

測試連線

在本節中,您會將一些網路流量從用戶端 VM 執行個體傳送至用戶伺服器 VM 執行個體,然後檢查供應商 VM 執行個體的記錄,確認鏡像功能是否正常運作。

  1. 執行下列指令,透過 SSH 連線至用戶端 VM 執行個體,並向伺服器 VM 執行個體傳送要求。

    gcloud compute ssh consumer-client-vm \
        --tunnel-through-iap \
        --zone=us-west1-b \
        --command="curl -m 3 -s http://consumer-server-vm:8000/connection_test || echo fail"
    

    執行上述指令時,您會看到 success 訊息。這表示流量是從用戶端傳送至伺服器。

  2. 執行下列指令,檢查生產者 VM 執行個體的記錄。

    gcloud compute ssh producer-instance \
        --tunnel-through-iap \
        --zone=us-west1-b \
        --command="cat /var/log/syslog | grep 'NSI MIRRORING'"
    

    您會看到類似 [NSI MIRRORING] IN=ens4 OUT= MAC=xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx SRC=10.10.0.1 DST=10.10.0.2 LEN=136 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF PROTO=UDP SPT=45554 DPT=6081 LEN=116 的訊息。這表示用戶端與伺服器之間的流量是由供應商 VM 執行個體鏡像。

清除所用資源

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

刪除消費者資源

  1. 取消防火牆政策與用戶網路的關聯,並刪除防火牆政策。

    控制台

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

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

    2. 按一下「consumer-firewall-policy」政策。

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

    4. 勾選 consumer-network 關聯核取方塊。

    5. 按一下「移除關聯」。

    6. 按一下 [移除]。

    7. 按一下頁面頂端的 [Delete] (刪除)。

    8. 再按一下 [刪除] 加以確認。

    gcloud

    1. 移除關聯。

      gcloud compute network-firewall-policies associations delete \
          --name=consumer-firewall-policy-assoc \
          --firewall-policy=consumer-firewall-policy \
          --global-firewall-policy
      
    2. 刪除防火牆政策。

      gcloud compute network-firewall-policies delete consumer-firewall-policy \
          --global
      
  2. 刪除安全性設定檔群組。

    控制台

    1. 前往 Google Cloud 控制台的「Security profiles」(安全設定檔) 頁面。

      前往「Security profiles」(安全性設定檔) 頁面

    2. 在專案挑選器中選取貴組織。

    3. 在「Security profile groups」(安全性設定檔群組) 分頁中,選取 consumer-security-profile-group 安全性設定檔群組,然後按一下「Delete」(刪除)。

    4. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security security-profile-groups delete consumer-security-profile-group \
        --location=global \
        --organization=ORG_ID \
        --billing-project=PROJECT_ID \
        --quiet
    

    更改下列內容:

    • ORG_ID:組織 ID
    • PROJECT_ID:專案 ID
  3. 刪除安全性設定檔。

    控制台

    1. 前往 Google Cloud 控制台的「Security profiles」(安全設定檔) 頁面。

      前往「Security profiles」(安全性設定檔) 頁面

    2. 在專案挑選器中選取貴組織。

    3. 在「Security profiles」(安全設定檔) 分頁中,選取 consumer-security-profile 安全設定檔,然後按一下「Delete」(刪除)。

    4. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security security-profiles custom-mirroring delete consumer-security-profile \
        --location=global \
        --organization=ORG_ID \
        --billing-project=PROJECT_ID \
        --quiet
    
  4. 刪除鏡像端點群組關聯。

    控制台

    1. 前往 Google Cloud 控制台的「Endpoint groups」(端點群組) 頁面。

      前往「Endpoint groups」(端點群組) 頁面

    2. 按一下「consumer-endpoint-group」。

    3. 選取 consumer-network 關聯,然後按一下「Delete」(刪除)。

    4. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security mirroring-endpoint-group-associations delete consumer-endpoint-group-association \
        --location=global \
        --no-async
    
  5. 刪除鏡像端點群組。

    控制台

    1. 前往 Google Cloud 控制台的「Endpoint groups」(端點群組) 頁面。

      前往「Endpoint groups」(端點群組) 頁面

    2. 選取 consumer-endpoint-group,然後按一下「Delete」(刪除)。

    3. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud network-security mirroring-endpoint-groups delete consumer-endpoint-group \
        --location=global \
        --no-async
    
  6. 刪除伺服器和用戶端 VM。

    控制台

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

      前往 VM 執行個體

    2. 勾選 consumer-client-vm 和 consumer-server-vm 的核取方塊,然後按一下「Delete」(刪除)。

    3. 再按一下 [刪除] 加以確認。

    gcloud

    1. 刪除用戶端 VM。

      gcloud compute instances delete consumer-client-vm \
          --zone=us-west1-b \
          --quiet
      
    2. 刪除伺服器 VM。

      gcloud compute instances delete consumer-server-vm \
          --zone=us-west1-b \
          --quiet
      
  7. 刪除用戶端網路和子網路。

    控制台

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

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

    2. 按一下「consumer-network」。

    3. 在「Subnets」(子網路) 分頁中,勾選 consumer-subnet 的核取方塊,然後按一下「Delete」(刪除)。

    4. 再按一下 [刪除] 加以確認。

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

    6. 在文字欄位中輸入 consumer-network,然後按一下「Delete」(刪除)。

    gcloud

    1. 刪除子網路。

      gcloud compute networks subnets delete consumer-subnet \
          --region=us-west1 \
          --quiet
      
    2. 刪除虛擬私有雲網路。

      gcloud compute networks delete consumer-network \
          --quiet
      

刪除供應端資源

  1. 取消防火牆政策與供應商網路的關聯,並刪除防火牆政策。

    控制台

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

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

    2. 按一下「producer-firewall-policy」政策。

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

    4. 勾選 producer-network 關聯核取方塊。

    5. 按一下「移除關聯」。

    6. 按一下 [移除]。

    7. 點選「刪除」。

    8. 再按一下 [刪除] 加以確認。

    gcloud

    1. 移除關聯。

      gcloud compute network-firewall-policies associations delete \
          --name=producer-firewall-policy-assoc \
          --firewall-policy=producer-firewall-policy \
          --global-firewall-policy
      
    2. 刪除防火牆政策。

      gcloud compute network-firewall-policies delete producer-firewall-policy \
          --global
      
  2. 刪除鏡像部署作業和鏡像部署作業群組。

    控制台

    1. 前往 Google Cloud 控制台的「Deployment groups」(部署作業群組) 頁面。

      前往「Deployment groups」(部署作業群組)

    2. 按一下「producer-deployment-group」。

    3. 刪除 producer-deployment。

      1. 勾選 producer-deployment 核取方塊,然後按一下「Delete」(刪除)。
      2. 再按一下 [刪除] 加以確認。
    4. 按一下 [Delete] (刪除) 按鈕。

    5. 再按一下 [刪除] 加以確認。

    gcloud

    1. 刪除鏡像部署項目。

      gcloud network-security mirroring-deployments delete producer-deployment \
          --location=us-west1-b \
          --no-async
      
    2. 刪除鏡像部署項目群組。

      gcloud network-security mirroring-deployment-groups delete producer-deployment-group \
          --location=global \
          --no-async
      
  3. 刪除負載平衡器資源。

    控制台

    1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。

      前往「Load balancing」(負載平衡)

    2. 勾選「producer-ilb」核取方塊。

    3. 點選頁面頂端的 [Delete] (刪除) 按鈕。

    4. 勾選「producer-health-check」核取方塊,然後按一下「Delete load balancer and the selected resources」(刪除負載平衡器和所選資源)。

    gcloud

    1. 刪除轉送規則。

      gcloud compute forwarding-rules delete producer-ilb-fr \
          --region=us-west1 \
          --quiet
      
    2. 移除後端服務。

      gcloud compute backend-services delete producer-backend-service \
          --region=us-west1 \
          --quiet
      
    3. 刪除健康狀態檢查。

      gcloud compute health-checks delete producer-health-check \
          --region=us-west1 \
          --quiet
      
  4. 刪除供應端 VM 執行個體。

    控制台

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

      前往 VM 執行個體

    2. 勾選 producer-instance 的核取方塊,然後按一下「刪除」。

    3. 再按一下 [刪除] 加以確認。

    gcloud

    gcloud compute instances delete producer-instance \
        --zone=us-west1-b \
        --quiet
    
  5. 刪除執行個體群組。

    控制台

    1. 前往 Google Cloud 控制台的「Instance groups」(執行個體群組) 頁面。

      前往執行個體群組

    2. 勾選「producer-instance-group」核取方塊。

    3. 點選「刪除」。

    4. 按一下確認視窗中的 [Delete]。

    gcloud

    gcloud compute instance-groups unmanaged delete producer-instance-group \
        --zone=us-west1-b \
        --quiet
    
  6. 刪除供應端網路和子網路。

    控制台

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

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

    2. 按一下「producer-network」。

    3. 在「子網路」分頁中,勾選 producer-subnet 的核取方塊,然後按一下「刪除」。

    4. 再按一下 [刪除] 加以確認。

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

    6. 在文字欄位中輸入 producer-network,然後按一下「Delete」(刪除)。

    gcloud

    1. 刪除子網路。

      gcloud compute networks subnets delete producer-subnet \
          --region=us-west1 \
          --quiet
      
    2. 刪除網路。

      gcloud compute networks delete producer-network \
          --quiet
      

後續步驟