设置服务速率限制政策

本页介绍了如何为区域级内部应用负载均衡器设置服务速率限制政策。

准备工作

创建服务速率限制政策

如需创建服务速率限制政策,请创建一个 YAML 文件来定义 目标和规则,然后使用 gcloud network-security service-rate-limit-policies 命令导入该文件。以下部分提供了服务速率限制政策的示例。

根据自定义群组应用速率限制

以下示例使用 HTTP 标头中的自定义标记 x-custom-group-tag 来标识两个群组:group1 和 group2。它将 group1 的每分钟请求数 (RPM) 限制为 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:转发规则的位置。

对所有客户端应用速率限制

以下示例将所有客户端的每分钟请求数限制为 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:您的 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:转发规则的位置。

后续步骤