設定服務頻率限制政策

本頁面說明如何為區域性內部應用程式負載平衡器設定服務頻率限制政策。

事前準備

建立服務頻率限制政策

如要建立服務頻率限制政策,請建立 YAML 檔案來定義目標和規則,然後使用 gcloud network-security service-rate-limit-policies 指令匯入檔案。以下各節提供服務頻率限制政策的範例。

根據自訂群組套用頻率限制

以下範例使用 HTTP 標頭中的自訂標記 x-custom-group-tag,識別兩個群組:group1 和 group2。每分鐘要求數 (RPM) 上限為 group1 10,000 次,group2 20,000 次。

gcloud

  1. 建立 custom-groups-policy.yaml 檔案並指定政策參數。

    $ cat >custom-groups-policy.yaml <<EOF
    name: projects/PROJECT_ID/locations/LOCATION/rateLimitPolicies/custom-groups-policy
    description: Rate limit each API key based on custom tags.
    httpRules:
    # Match on traffic on the custom tag header.
    - to:
        destination:
          headerSet:
            headers:
            - name: "x-custom-group-tag"
              value:
                exact: "group1"
    
        rateLimitActions:
          - rateLimitBucket: rl_group1_rpm_limit
    
    - to:
        destination:
          headerSet:
            headers:
            - name: "x-custom-group-tag"
              value:
                exact: "group2"
    
        rateLimitActions:
          - rateLimitBucket: rl_group2_rpm_limit
    
    rateLimitBuckets:
    - name: rl_group1_rpm_limit
      # Set threshold for ALL the traffic matching this bucket.
      keys:
      - keyType: ALL
    
      defaultLimit:
        countLimit:
          count: 10000
          interval: 1
          intervalUnit: MINUTES
    
    - name: rl_group2_rpm_limit
      # Set threshold for ALL the traffic matching this bucket.
      keys:
      - keyType: ALL
    
      defaultLimit:
        countLimit:
          count: 20000
          interval: 1
          intervalUnit: MINUTES
    
    targets:
    - resource: projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_NAME
    EOF
    

    更改下列內容:

    • PROJECT_ID: Cloud de Confiance 專案的 ID。
    • LOCATION:轉送規則的位置。
    • FORWARDING_RULE_NAME:轉送規則的名稱。
  2. 匯入上一個步驟中建立的 YAML 檔案,建立服務速率限制政策:

    gcloud network-security service-rate-limit-policies import custom-groups-policy \
     --source=custom-groups-policy.yaml \
     --location=LOCATION \
     --project=PROJECT_ID
    

    更改下列內容:

    • PROJECT_ID:專案的 ID。 Cloud de Confiance
    • LOCATION:轉送規則的位置。

對所有用戶端套用頻率限制

以下範例將所有用戶端的每分鐘要求數限制為 10,000。

gcloud

  1. 建立 all-clients-policy.yaml 檔案,並指定政策參數。

    $ cat >all-clients-policy.yaml <<EOF
    name: projects/PROJECT_ID/locations/LOCATION/rateLimitPolicies/all-clients-policy
    description: Service rate limiting example.
    httpRules:
      # Match on all traffic.
    
      # Service rate limiting bucket
    - rateLimitActions:
      - rateLimitBucket: rl_default_rpm_limit
    
    rateLimitBuckets:
    - name: "rl_default_rpm_limit"
      # Match on 'ALL' requests for this bucket.
      keys:
      - keyType: ALL
    
      defaultLimit:
        countLimit:
          count: 10000
          interval: 1
          intervalUnit: MINUTES
    
    targets:
    - resource: projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_NAME
    EOF
    

    更改下列內容:

    • PROJECT_ID: Cloud de Confiance 專案的 ID。
    • LOCATION:轉送規則的位置。
    • FORWARDING_RULE_NAME:轉送規則的名稱。
  2. 匯入上一個步驟中建立的 YAML 檔案,建立服務速率限制政策:

    gcloud network-security service-rate-limit-policies import all-clients-policy \
     --source=all-clients-policy.yaml \
     --location=LOCATION \
     --project=PROJECT_ID
    

    更改下列內容:

    • PROJECT_ID: Cloud de Confiance 專案的 ID。
    • LOCATION:轉送規則的位置。

根據主機和 API 金鑰套用頻率限制

以下範例會限制傳送至主機 example.com 的 API 頻率限制,每 5 分鐘最多 1,000 個要求,每個要求都來自從 x-api-key HTTP 標頭擷取的 API 金鑰不重複值。但特殊 API 金鑰abc123的上限較高,每 5 分鐘可發出 10,000 個要求。

gcloud

  1. 建立 host-and-api-key-policy.yaml 檔案,並指定政策參數。

    $ cat >host-and-api-key-policy.yaml <<EOF
    name: projects/PROJECT_ID/locations/LOCATION/rateLimitPolicies/host-and-api-key-policy
    description: Rate limit each api key.
    httpRules:
      # Match on traffic to example.com
    - to:
        destination:
          hosts:
          - exact: "example.com"
    
      rateLimitActions:
        - rateLimitBucket: rl_default_rpm_limit
    
    rateLimitBuckets:
    - name: rl_default_rpm_limit
      # Identify API key from its header, say 'x-api-key'.
      keys:
      - header: "x-api-key"
        keyType: HTTP_HEADER
    
      defaultLimit:
        countLimit:
          count: 1000
          interval: 5
          intervalUnit: MINUTES
    
      # Override the threshold for specific API key 'abc123'.
      userOverrides:
      - overrideKey:
          httpHeaders:
          - header: "x-api-key"
            value: "abc123"
        limit:
          countLimit:
            count: 10000
            interval: 5
            intervalUnit: MINUTES
    
    targets:
    - resource: projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_NAME
    EOF
    

    更改下列內容:

    • PROJECT_ID: Cloud de Confiance 專案的 ID。
    • LOCATION:轉送規則的位置。
    • FORWARDING_RULE_NAME:轉送規則的名稱。
  2. 匯入上一個步驟中建立的 YAML 檔案,建立服務速率限制政策:

    gcloud network-security service-rate-limit-policies import host-and-api-key-policy \
     --source=host-and-api-key-policy.yaml \
     --location=LOCATION \
     --project=PROJECT_ID
    

    更改下列內容:

    • PROJECT_ID: Cloud de Confiance 專案的 ID。
    • LOCATION:轉送規則的位置。

依據網址路徑和用戶端名稱套用頻率限制

以下範例會限制每個用戶端 (以用戶端憑證通用名稱識別) 在 /public 路徑上託管的 API,每分鐘最多只能傳送 100 個要求。

gcloud

  1. 建立 path-and-client-name-policy.yaml 檔案,並指定政策參數。

    $ cat >path-and-client-name-policy.yaml <<EOF
    name: projects/PROJECT_ID/locations/LOCATION/rateLimitPolicies/path-and-client-name-policy
    description: Simple API rate limiting example.
    httpRules:
      # Match on traffic to /public .
    - to:
        destination:
          paths:
          - exact: "/public"
    
      # Default RPM bucket
      rateLimitActions:
      - rateLimitBucket: rl_default_rpm_limit
    
    rateLimitBuckets:
    - name: "rl_default_rpm_limit"
      # Identify client by its client cert name
      keys:
      - keyType: PRINCIPAL
        principalType: CLIENT_CERT_COMMON_NAME
      defaultLimit:
        countLimit:
          count: 100
          interval: 1
          intervalUnit: MINUTES
    
    targets:
    - resource: projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_NAME
    
    EOF
    

    更改下列內容:

    • PROJECT_ID:專案的 ID。 Cloud de Confiance
    • LOCATION:轉送規則的位置。
    • FORWARDING_RULE_NAME:轉送規則的名稱。
  2. 匯入上一個步驟中建立的 YAML 檔案,建立服務速率限制政策:

    gcloud network-security service-rate-limit-policies import path-and-client-name-policy \
     --source=path-and-client-name-policy.yaml \
     --location=LOCATION \
     --project=PROJECT_ID
    

    更改下列內容:

    • PROJECT_ID:專案的 ID。 Cloud de Confiance
    • LOCATION:轉送規則的位置。

後續步驟