サービス レート制限ポリシーを設定する

このページでは、リージョン内部アプリケーション ロードバランサのサービス レート制限ポリシーを設定する方法について説明します。

始める前に

サービス レート制限ポリシーを作成する

サービス レート制限ポリシーを作成するには、ターゲットとルールを定義する YAML ファイルを作成し、gcloud network-security service-rate-limit-policies コマンドを使用してインポートします。以降のセクションでは、サービス レート制限ポリシーの例を示します。

カスタム グループに基づいてレート制限を適用する

次の例では、HTTP ヘッダーのカスタムタグ x-custom-group-tag を使用して、group1 と group2 の 2 つのグループを識別します。1 分あたりのリクエスト数(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: 実際の Cloud de Confiance プロジェクト ID。
    • LOCATION: 転送ルールのロケーション。

すべてのクライアントにレート制限を適用する

次の例では、すべてのクライアントの 1 分あたりのリクエスト数を 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 キーに基づいてレート制限を適用する

次の例では、x-api-key HTTP ヘッダーから取得された API キーの一意の値ごとに、ホスト example.com を宛先とする API を 5 分あたり 1,000 リクエストにレート制限します。特別な 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: 転送ルールのロケーション。

URL パスとクライアント名に基づいてレート制限を適用する

次の例では、クライアント証明書の共通名で識別される各クライアントの /public パスでホストされている API を 1 分あたり 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: 実際の Cloud de Confiance プロジェクト ID。
    • 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: 実際の Cloud de Confiance プロジェクト ID。
    • LOCATION: 転送ルールのロケーション。

次のステップ