Menyiapkan kebijakan pembatasan kapasitas layanan

Halaman ini menunjukkan cara menyiapkan kebijakan pembatasan kapasitas layanan untuk Load Balancer Aplikasi internal regional.

Sebelum memulai

Membuat kebijakan pembatasan kapasitas layanan

Untuk membuat kebijakan pembatasan frekuensi layanan, buat file YAML yang menentukan target dan aturan, lalu impor file menggunakan perintah gcloud network-security service-rate-limit-policies. Bagian berikut memberikan contoh kebijakan pembatasan frekuensi layanan.

Menerapkan pembatasan kapasitas berdasarkan grup kustom

Contoh berikut menggunakan tag kustom dari header HTTP, x-custom-group-tag, untuk mengidentifikasi dua grup: group1 dan group2. Hal ini membatasi permintaan per menit (RPM) hingga 10.000 untuk group1 dan 20.000 untuk group2.

gcloud

  1. Buat file custom-groups-policy.yaml dan tentukan parameter kebijakan.

    $ 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
    

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.
    • FORWARDING_RULE_NAME: nama aturan penerusan Anda.
  2. Buat kebijakan pembatasan frekuensi layanan dengan mengimpor file YAML yang Anda buat pada langkah sebelumnya:

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

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.

Menerapkan pembatasan kapasitas ke semua klien

Contoh berikut membatasi permintaan per menit menjadi 10.000 untuk semua klien.

gcloud

  1. Buat file all-clients-policy.yaml dan tentukan parameter kebijakan.

    $ 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
    

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.
    • FORWARDING_RULE_NAME: nama aturan penerusan Anda.
  2. Buat kebijakan pembatasan frekuensi layanan dengan mengimpor file YAML yang Anda buat pada langkah sebelumnya:

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

    Ganti kode berikut:

    • PROJECT_ID: ID Cloud de Confiance project Anda.
    • LOCATION: lokasi aturan penerusan Anda.

Menerapkan pembatasan kapasitas berdasarkan host dan kunci API

Contoh berikut membatasi kapasitas API yang ditujukan untuk host example.com hingga 1.000 permintaan per 5 menit untuk setiap nilai unik kunci API yang diambil dari Header HTTP x-api-key. Kunci API khusus abc123 memiliki batas yang lebih tinggi,yaitu 10.000 permintaan per 5 menit.

gcloud

  1. Buat file host-and-api-key-policy.yaml dan tentukan parameter kebijakan.

    $ 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
    

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.
    • FORWARDING_RULE_NAME: nama aturan penerusan Anda.
  2. Buat kebijakan pembatasan frekuensi layanan dengan mengimpor file YAML yang Anda buat pada langkah sebelumnya:

    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
    

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.

Menerapkan pembatasan kapasitas berdasarkan jalur URL dan nama klien

Contoh berikut membatasi API yang dihosting di jalur /public untuk setiap klien, yang diidentifikasi dengan Nama Umum Sertifikat Klien, hingga 100 permintaan per menit.

gcloud

  1. Buat file path-and-client-name-policy.yaml dan tentukan parameter kebijakan.

    $ 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
    

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.
    • FORWARDING_RULE_NAME: nama aturan penerusan Anda.
  2. Buat kebijakan pembatasan frekuensi layanan dengan mengimpor file YAML yang Anda buat pada langkah sebelumnya:

    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
    

    Ganti kode berikut:

    • PROJECT_ID: ID project Cloud de Confiance Anda.
    • LOCATION: lokasi aturan penerusan Anda.

Langkah berikutnya