在 Google Cloud 中建立防火牆規則

如果您未採取適當的預防措施,將導致系統暴露在新的安全漏洞中。因此,這是建立防火牆規則以控管 Google Cloud 中執行個體流量的金鑰。在本程式碼研究室中,您將瞭解 Google Cloud 中的防火牆規則,以及如何將規則套用至 Compute Engine VM 執行個體。

什麼是防火牆規則?

Google Cloud 的防火牆規則可讓您根據自己的設定,允許或拒絕 VM 執行個體的往來流量。無論系統是否啟用執行個體,系統一律會強制執行已啟用的防火牆規則,進而保護您的執行個體,無論執行個體的設定和作業系統為何都沒有影響。

每個虛擬私人雲端網路皆會以分散式防火牆的形式運作。雖然防火牆規則是在網路層級定義,但根據不同的執行個體,您可以允許或拒絕連線。防火牆規則的適用範圍不僅是執行個體與其他網路之間的相同,也位於相同網路內的個別執行個體之間。

建構項目

  • 兩個不同子網路中的 Compute Engine 執行個體
  • 網路連線偵測 (ping) 測試
  • 使用執行個體標記的防火牆規則
  • 下列架構:

您將會瞭解的內容

  • 防火牆規則的特性
  • 如何使用執行個體標記將防火牆規則套用至執行個體
  • 防火牆規則的最佳做法

軟硬體需求

  • Google 帳戶
  • 兩個 Compute Engine 執行個體

自行調整環境設定

如果您還沒有 Google 帳戶,請先建立帳戶。登入 Google Cloud Console 並建立新專案:

請記住專案 ID,是所有 Google Cloud 專案中的專屬名稱。這些程式碼研究室將稱為 PROJECT_ID。

接著,您必須在 Cloud Console 中啟用計費功能,才能使用 Google Cloud 資源。

完成這個程式碼研究室的成本應該不會超過新臺幣 $300 元,但如果您決定用到更多資源,或是不需要繼續執行,可能會產生更多成本 (請參閱本程式碼研究室結尾處的清除部分)。Google Cloud 新使用者可享有 $300 美元的免費試用期

初始化專案環境

前往「Compute > Compute Engine > VM 執行個體」

在您完成啟用 Compute Engine API 之後,Cloud Shell 就能完成大部分的工作,可讓您在任何瀏覽器中透過指令列管理基礎架構和應用程式。以 Debian 為基礎的 VM 會載入您需要的所有開發工具 (gcloud 指令列工具、Git 等),並提供永久的 5 GB 主目錄。按一下畫面右上角的圖示來開啟 Cloud Shell。

您也可以挑選其他區域。如需更多資訊,請參閱地區與區域

如要查看現有的防火牆規則,請前往「Networking > VPC network > 防火牆規則」

請注意,系統僅為預設網路建立了防火牆規則。

由於您在建立 custom-network1 時未指定任何防火牆規則,因此必須為現有的 Compute Engine 執行個體建立防火牆規則,以透過 SSH 連線至執行個體。

接著,建立防火牆規則,允許 SSH 存取標記的執行個體。

繼續使用「防火牆規則」頁面。按一下 [建立防火牆規則]。

新增下列規格:

  • 名稱:allow-ssh
  • 網路:custom-network1
  • 流量方向:Ingress
  • 符合動作時允許:允許
  • 目標:指定的目標標記
  • 目標標記:iperf-access
  • 源 IP 範圍:0.0.0.0/0
  • 通訊協定和通訊埠:指定的通訊協定和通訊埠:TCP:22

將標記套用至兩個現有的執行個體。

前往 Compute > Compute Engine > VM 執行個體,前往 Compute Engine 執行個體頁面。

按一下 instance-1 並查看 VM 執行個體詳細資料。按一下 [編輯],

向下捲動到「網路標記」部分,然後替它加上 iperf-access 標記。

按一下底部的 [儲存]。返回「VM instances」(VM 執行個體) 頁面,然後按一下 [編輯] 並再次新增 iperf-access 做為執行個體標記,即可重複建立執行個體 2。

iPerf

iPerf 是常用的網路測試工具,能夠建立 TCP/UDP 資料串流,並測量傳輸該網路的網路總處理量。您會使用這個金鑰在 custom-network1 內不同子網路的執行個體之間執行快速測試。

安裝 iPerf

前往 Compute Engine 執行個體頁面。

點選您在每個執行個體清單旁的 SSH 按鈕,前往您在上一個程式碼研究室中建立的兩個 VM。

畫面上會出現兩個 SSH 視窗。使用下列指令安裝 iPerf:

sudo apt-get install iperf

執行 iPerf 測試

instance-2 中,於 instance-2 ssh 終端機中輸入以下內容,以指定 iPerf 伺服器:

iperf -s

在 instance-1 ssh 視窗中,輸入下列指令,包含 instance-2 的內部 IP:

iperf -c [INSTANCE-2 INTERNAL IP]

由於您尚未執行另一個防火牆規則來允許 instance-1instance-2 之間的通訊,因此該訊息會停止運作。按下 Control+Z 鍵 (Mac 是 Command+Z 鍵),終止指令。

這次會使用 Cloud Shell 建立防火牆規則。

請確定您是在 Cloud Shell 中,而非執行個體的 SSH 工作階段。輸入以下指令,建立防火牆規則:

gcloud compute firewall-rules create iperf-access --allow tcp:5001 \
      --source-ranges 0.0.0.0/0 \
      --network custom-network1 \
      --target-tags=iperf-access

確認檔案是使用下列項目建立:

gcloud compute firewall-rules list

NAME                         NETWORK          DIRECTION  PRIORITY  ALLOW                         DENY  DISABLED
allow-ssh                    custom-network1  INGRESS    1000      tcp:22                              False
default-allow-icmp           default          INGRESS    65534     icmp                                False
default-allow-internal       default          INGRESS    65534     tcp:0-65535,udp:0-65535,icmp        False
default-allow-rdp            default          INGRESS    65534     tcp:3389                            False
default-allow-ssh            default          INGRESS    65534     tcp:22                              False
iperf-access                 custom-network1  INGRESS    1000      tcp:5001                            False

返回 instance-2 的 SSH 視窗。您的 iperf 工作階段仍然有效。如果沒有,請再次執行下列指令。

iperf -s

instance-1 SSH 視窗中,輸入下列指令,包含 instance-2 的內部 IP:

iperf -c [INSTANCE-2 INTERNAL IP]

您應該會看到 iPerf 與 instance-2 建立連線,並在 instance-1instance-2 之間傳回內部網路效能的輸出。

現在您已瞭解在 Google Cloud 中建立防火牆規則的基本概念。

說明

  • 執行個體對防火牆規則的影響
  • 如何透過 Cloud Console 和 Cloud Shell 建立防火牆規則
  • 如何將標記套用至防火牆規則和執行個體
  • 如何在執行個體之間執行 iPerf 測試

後續步驟

在 Cloud Shell 中執行下列指令,刪除 Compute Engine 執行個體:

gcloud compute instances delete instance-1 --zone us-central1-a

The following instances will be deleted. Any attached disks configured
 to be auto-deleted will be deleted unless they are attached to any
other instances or the `--keep-disks` flag is given and specifies them
 for keeping. Deleting a disk is irreversible and any data on the disk
 will be lost.
 - [instance-1] in [us-central1-a]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/ypc-demo/zones/us-central1-a/instances/instance-1].
gcloud compute instances delete instance-2 --zone europe-west1-d

The following instances will be deleted. Any attached disks configured
 to be auto-deleted will be deleted unless they are attached to any
other instances or the `--keep-disks` flag is given and specifies them
 for keeping. Deleting a disk is irreversible and any data on the disk
 will be lost.
 - [instance-2] in [europe-west1-d]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/ypc-demo/zones/europe-west1-d/instances/instance-2].

使用下列指令刪除防火牆規則:

gcloud compute firewall-rules delete allow-ssh

The following firewalls will be deleted:
 - [allow-ssh]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/global/firewalls/allow-ssh].
gcloud compute firewall-rules delete iperf-access

The following firewalls will be deleted:
 - [iperf-access]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/global/firewalls/iperf-access].

使用下列指令刪除子網路:

gcloud compute networks subnets delete subnet-us-central-192 --region us-central1

The following subnetworks will be deleted:
 - [subnet-us-central-192] in [us-central1]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/regions/us-central1/subnetworks/subnet-us-central-192].
gcloud compute networks subnets delete subnet-europe-west-192 --region europe-west1

The following subnetworks will be deleted:
 - [subnet-europe-west-192] in [europe-west1]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/regions/europe-west1/subnetworks/subnet-europe-west-192].

使用下列指令刪除自訂虛擬私人雲端:

gcloud compute networks delete custom-network1

The following networks will be deleted:
 - [custom-network1]
Do you want to continue (Y/n)?  y
Deleted [https://www.googleapis.com/compute/v1/projects/vpc-demo-241520/global/networks/custom-network1].