TCP Proxy 程式碼研究室 - 使用 TCP Proxy 負載平衡器的頻率限制和 IP 拒絕清單

1. 簡介

Google Cloud 負載平衡服務部署於 Google 遍布全球的網路邊緣服務點 (POP)。導向 TCP Proxy 負載平衡器的使用者流量會進入離使用者最近的 POP,接著透過 Google 的全球網路進行負載平衡,再傳至有足夠容量且距離最近的後端。

Cloud Armor 是 Google 的分散式阻斷服務和網路應用程式防火牆 (WAF) 偵測系統。Cloud Armor 與 Google Cloud TCP Proxy 負載平衡器緊密整合,可供您檢查傳入流量,找出不當要求。這項服務的速率限制功能可根據要求量減少後端資源的流量,並防止不必要的流量消耗虛擬私有雲 (VPC) 網路的資源。

Google Cloud TCP/SSL Proxy 負載平衡器可讓您在後端服務之間,對 TCP/ SSL 類型的流量進行 Proxy。

在本實驗室中,您將建立具備後端服務的 TCP/SSL 負載平衡器,並限制只有特定使用者用戶端可存取該負載平衡器。

be33dadf836374bb.png

課程內容

  • 如何建立 TCP/SSL Proxy 負載平衡器
  • 如何建立 Cloud Armor 安全性政策
  • 如何在 Cloud Armor 中為 TCP/SSL Proxy 負載平衡器建立 IP 拒絕清單規則
  • 如何在 Cloud Armor 中為 TCP Proxy 負載平衡器建立速率限制規則
  • 如何將安全性政策新增至 TCP/SSL 負載平衡後端服務

軟硬體需求

  • Google Compute Engine 基礎知識 ( 程式碼研究室)
  • 基本的網路和 TCP/IP 知識
  • 基本的 Unix/Linux 指令列知識
  • 建議您先完成 Google Cloud 網路導覽,瞭解 GCP 網路功能。

2. 需求條件

自修實驗室環境設定

  1. 登入 Cloud 控制台,建立新專案或重複使用現有專案。如果沒有 Gmail 或 Google Workspace 帳戶,請先建立帳戶

注意:記住以下網址,即可輕鬆存取 Cloud 控制台:console.cloud.google.com。

96a9c957bc475304.png

b9a10ebdf5b5a448.png

a1e3c01a38fa61c2.png

請記住專案 ID,這是所有 Google Cloud 專案中不重複的名稱 (上述名稱已遭占用,因此不適用於您,抱歉!)。本程式碼研究室稍後會將其稱為 PROJECT_ID。

注意:如果您使用 Gmail 帳戶,可以將預設位置設為「沒有機構」。如果您使用 Google Workspace 帳戶,請選擇適合貴機構的位置。

  1. 接著,您必須在 Cloud 控制台中啟用帳單,才能使用 Google Cloud 資源。

完成本程式碼研究室的費用應該不高,甚至完全免費。請務必按照「清除」部分的指示操作,瞭解如何停用資源,避免在本教學課程結束後繼續產生帳單費用。Google Cloud 新使用者可參加價值$300 美元的免費試用計畫。

啟動 Cloud Shell

雖然可以透過筆電遠端操作 Google Cloud,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。

在 GCP 主控台,按一下右上角工具列的 Cloud Shell 圖示:

bce75f34b2c53987.png

佈建並連線至環境的作業需要一些時間才能完成。完成後,您應該會看到如下的內容:

f6ef2b5f13479f3a.png

這部虛擬機器搭載各種您需要的開發工具,並提供永久的 5GB 主目錄,而且可在 Google Cloud 運作,大幅提升網路效能並強化驗證功能。本實驗室的所有工作都可在瀏覽器上完成。

事前準備

在 Cloud Shell 中,確認專案 ID 已設定完畢

gcloud config list project
gcloud config set project [YOUR-PROJECT-NAME]
PROJECT_ID=[YOUR-PROJECT-NAME]
echo $PROJECT_ID

啟用 API

啟用所有必要服務

gcloud services enable compute.googleapis.com
gcloud services enable logging.googleapis.com        
gcloud services enable monitoring.googleapis.com

3. 建立後端服務

依下列方式建立 2 個執行個體:在 us-central1-b 區域中建立 instance1-b1

gcloud compute instances create vm-1-b1 \
    --image-family debian-9 \
    --image-project debian-cloud \
    --tags tcp-lb \
    --zone us-central1-b \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install apache2 -y
      sudo sed -i '/Listen 80/c\Listen 110' /etc/apache2/ports.conf
      sudo service apache2 restart
      echo '<!doctype html><html><body><h1>This is VM1-b1 in central1-b</h1></body></html>' | tee /var/www/html/index.html
      EOF"

在區域 us-central1-b 中建立執行個體 1-b2

gcloud compute instances create vm-1-b2 \
    --image-family debian-9 \
    --image-project debian-cloud \
    --tags tcp-lb \
    --zone us-central1-b \
    --metadata startup-script="#! /bin/bash
      sudo apt-get update
      sudo apt-get install apache2 -y
      sudo sed -i '/Listen 80/c\Listen 110' /etc/apache2/ports.conf
      sudo service apache2 restart
      echo '<!doctype html><html><body><h1>This is VM1-b2 in central1-b</h1></body></html>' | tee /var/www/html/index.html
      EOF"

建立執行個體群組 vm-ig1

gcloud compute instance-groups unmanaged create vm-ig1  --zone us-central1-b

為執行個體群組建立命名通訊埠。在本實驗室中,我們將使用通訊埠 110

    gcloud compute instance-groups set-named-ports vm-ig1 \
--named-ports tcp 110:110 --zone us-central1-b

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

gcloud compute instance-groups unmanaged add-instances vm-ig1 \
   --instances vm-1-b1,vm-1-b2 --zone us-central1-b

4. 設定負載平衡器

接下來,我們將建立健康狀態檢查。

gcloud compute health-checks create tcp my-tcp-health-check --port 110

建立後端服務

gcloud compute backend-services create my-tcp-lb  --global-health-checks --global \
--protocol TCP --health-checks my-tcp-health-check --timeout 5m --port-name tcp110

將執行個體群組新增至後端服務

gcloud compute backend-services add-backend my-tcp-lb --global --instance-group \ vm-ig1 --instance-group-zone us-central1-b --balancing-mode UTILIZATION \ --max-utilization 0.8

設定目標 TCP Proxy

gcloud compute target-tcp-proxies create my-tcp-lb-target-proxy --backend-service \ my-tcp-lb --proxy-header NONE

保留全域靜態 IPv4 位址

您會使用這個 IP 位址來存取已平衡工作負載的服務。

gcloud compute addresses create tcp-lb-static-ipv4  --ip-version=IPV4   --global

設定負載平衡器 IP 位址的通用轉送規則。

gcloud compute forwarding-rules create my-tcp-lb-ipv4-forwarding-rule \
    --global --target-tcp-proxy my-tcp-lb-target-proxy --address LB_STATIC_IPV4 \ --ports 110

5. 建立 TCP Proxy 負載平衡器的防火牆規則

gcloud compute firewall-rules create allow-tcplb-and-health \
   --source-ranges 130.211.0.0/22,35.191.0.0/16 \
   --target-tags tcp-lb \
   --allow tcp:110

建立負載平衡器後,請使用下列指令進行測試

Curl LB_IP:110

接著建立 VM,驗證對負載平衡器的存取遭拒

您應建立 2 個執行個體,每個執行個體都有公開 IP 位址,並分別命名為 test-server1 和 test-server2

6. 在 Cloud Armor 中建立安全性政策

在本節中,您將在 Cloud Armor 中建立後端安全政策,以及政策中的 2 項規則。

第一條規則會設定安全性政策,拒絕特定 IP 存取 TCP 負載平衡器,第二條規則則會執行頻率限制。

  1. 在 Cloud Shell 中(如需如何使用 Cloud Shell 的操作說明,請參閱「設定和需求條件」下的「啟動 Cloud Shell」),建立名為 rate-limit-and-deny-tcp 的後端服務安全政策,如下所示:
gcloud compute security-policies create rate-limit-and-deny-tcp \
    --description "policy for tcp proxy rate limiting and IP deny"

在安全性政策中新增規則

接著,將拒絕清單規則新增至 Cloud Armor 政策「rate-limit-and-deny-tcp」。

gcloud compute security-policies rules create 1000 --action deny --security-policy \ rate-limit-and-deny-tcp --description "deny test-server1" --src-ip-ranges \ "enter-test-server-1ip-here"

在 Cloud Armor 安全性政策「rate-limit-and-deny-tcp」中新增頻率限制規則

gcloud compute security-policies rules create 3000   \ --security-policy=rate-limit-and-deny-tcp  \       
--expression="true"  --action=rate-based-ban  --rate-limit-threshold-count=5  \          
--rate-limit-threshold-interval-sec=60  --ban-duration-sec=300      \         
--conform-action=allow  --exceed-action=deny-404  --enforce-on-key=IP

將政策附加至 TCP Proxy 後端服務:

執行下列指令,確保安全政策已附加至 TCP Proxy 後端服務。

gcloud compute backend-services update my-tcp-lb --security-policy \ rate-limit-and-deny-tcp

在 TCP Proxy 負載平衡器上啟用記錄功能

gcloud beta compute backend-services update my-tcp-lb \ 
--enable-logging --logging-sample-rate=1

7. 驗證拒絕清單規則

登入 IP 位址已在拒絕清單規則中指定的測試伺服器,然後執行下列指令,驗證拒絕清單規則:

Curl LB_IP:110

立即提出要求時,負載平衡器可能會提供回應,但請等到 curl 要求遭到拒絕或捨棄,然後查看 Cloud Logging 中的記錄,確認 IP 拒絕規則是否已觸發記錄項目。

前往 Cloud Logging,然後在資源下方選取「tcp_ssl_proxy_rule」資源類型,並將後端目標設為「my-tcp-lb」。

定義用於篩選的資源後,我們可以從記錄檔項目的 PRIORITY 值 1000,以及已設定的「DENY」動作,驗證 IP 拒絕規則是否生效,因為這兩者都是從拒絕規則和遭拒絕的 IP 取得指示,如下所示

db9b835e0360dcaf.png

8. 驗證頻率限制規則

在短時間內傳送大量要求,超過定義的門檻 (每分鐘 5 個要求),驗證速率限制規則是否生效。

完成後,按一下 Cloud Armor 服務中的「查看記錄」,系統會將您帶往 Cloud Logging,您可以在這裡依負載平衡器篩選記錄,查看 Cloud Armor 記錄。

速率限制項目應如下方螢幕截圖所示。我們可以從記錄檔項目的 PRIORITY 值 3000,以及設定的動作驗證速率限制規則是否生效。如記錄檔所示,系統已根據速率限制規則,套用「RATE BASED BAN」動作。

37c76e5d7532623.png

9. 環境清理

請務必清除所建立的基礎架構,以免產生未使用的基礎架構執行費用。

最快的方法是刪除 GCP 中的整個專案,確保沒有任何閒置資源無人管理。不過,您也可以使用下列指令刪除個別資源

TCP Proxy 負載平衡器

gcloud compute target-tcp-proxies delete my-tcp-lb

執行個體群組

gcloud compute instance-groups unmanaged delete vm-ig1

已建立 2 個測試 VM 執行個體

gcloud compute instances delete Instance_name --zone=instance_zone

後端服務

gcloud compute backend-services delete BACKEND_SERVICE_NAME

政策中的 Cloud Armor 規則

gcloud compute security-policies rules delete 1000  \ --security-policy=rate-limit-and-deny-tcp && 
gcloud compute security-policies rules delete 3000  \ --security-policy=rate-limit-and-deny-tcp

Cloud Armor 安全性政策

gcloud compute security-policies delete rate-limit-and-deny-tcp