Menyiapkan perutean berbasis parameter kueri dan header untuk Load Balancer Aplikasi klasik

Halaman ini mencakup dua contoh untuk Load Balancer Aplikasi klasik:

Untuk mengonfigurasi pengelolaan traffic bagi Load Balancer Aplikasi eksternal global dan Load Balancer Aplikasi eksternal regional, lihat halaman berikut:

Sebelum memulai

Menyiapkan perutean berbasis parameter kueri

Contoh ini menunjukkan cara menggunakan parameter kueri untuk melakukan pengujian A/B dengan mencocokkan string kueri.

Menambahkan dua grup instance backend

Agar perutean bermanfaat, Anda harus memiliki beberapa backend.

Untuk menyiapkan dua backend, VM Anda harus berada dalam dua grup instance. Panduan ini menjelaskan cara membuat grup instance terkelola dengan VM Linux yang menjalankan Apache, lalu menyiapkan load balancing.

Grup instance terkelola menyediakan VM yang menjalankan server backend load balancer HTTP eksternal. Untuk tujuan demonstrasi, backend menyajikan nama host-nya sendiri.

Untuk mempermudah, backend berada di region yang sama. Jika menginginkan penyiapan multi-region, Anda harus menyiapkan template instance untuk region kedua.

Konsol

  1. Buat template instance. Di Google Cloud konsol, buka halaman Instance templates.

    Buka Instance templates

    1. Klik Create instance template.
    2. Untuk Name, masukkan lb-backend-template.
    3. Pastikan Boot disk disetel ke image Debian, seperti Debian GNU/Linux 12 (bookworm). Petunjuk ini menggunakan perintah yang hanya tersedia di Debian, seperti apt-get.
    4. Klik Advanced options.
    5. Klik Networking dan konfigurasi kolom berikut:
      1. Untuk Network tags, masukkan allow-health-check.
    6. Klik Management. Masukkan skrip berikut ke kolom Startup script.

      #! /bin/bash
      apt-get update
      apt-get install apache2 -y
      a2ensite default-ssl
      a2enmod ssl
      vm_hostname="$(curl -H "Metadata-Flavor:Google" \
      http://metadata.google.internal/computeMetadata/v1/instance/name)"
      echo "Page served from: $vm_hostname" | \
      tee /var/www/html/index.html
      systemctl restart apache2
      
    7. Klik Create.

  2. Buat grup instance terkelola. Buka halaman Instance groups di Google Cloud konsol.

    Buka Instance groups

    1. Klik Create instance group.
    2. Pilih New managed instance group (stateless). Untuk mengetahui informasi selengkapnya, lihat MIG stateless atau stateful.
    3. Untuk Name, masukkan first-example-ig.
    4. Di bagian Location, pilih Single zone.
    5. Untuk Region, pilih region yang Anda inginkan. Contoh ini menggunakan us-east1.
    6. Untuk Zone, pilih us-east1-b.
    7. Di bagian Instance template, pilih template instance lb-backend-template.
    8. Di bagian Maximum number of instances, masukkan 2.
    9. Di bagian Autoscaling mode, pilih Off:do not autoscale.
    10. Klik Create.

Buat grup instance terkelola lain seperti ini. Beri nama grup instance kedua second-example-ig, dan buat berdasarkan template lb-backend-template.

gcloud

  1. Buat template instance.

    gcloud compute instance-templates create lb-backend-template \
       --region=us-east1 \
       --network=default \
       --subnet=default \
       --tags=allow-health-check \
       --image-family=debian-12 \
       --image-project=debian-cloud \
       --metadata=startup-script='#! /bin/bash
         apt-get update
         apt-get install apache2 -y
         a2ensite default-ssl
         a2enmod ssl
         vm_hostname="$(curl -H "Metadata-Flavor:Google" \
         http://metadata.google.internal/computeMetadata/v1/instance/name)"
         echo "Page served from: $vm_hostname" | \
         tee /var/www/html/index.html
         systemctl restart apache2'
    
  2. Buat grup instance terkelola pertama berdasarkan template.

    gcloud compute instance-groups managed create first-example-ig \
       --template=lb-backend-template --size=2 --zone=us-east1-b
    
  3. Buat grup instance terkelola kedua berdasarkan template.

    gcloud compute instance-groups managed create second-example-ig \
       --template=lb-backend-template --size=2 --zone=us-east1-c
    

Mengonfigurasi aturan firewall

Dalam contoh ini, Anda akan membuat aturan firewall fw-allow-health-check. Ini adalah aturan ingress yang mengizinkan traffic dari sistem health check Google Cloud (130.211.0.0/22 dan 35.191.0.0/16). Contoh ini menggunakan tag target allow-health-check untuk mengidentifikasi VM.

Konsol

  1. Di Google Cloud konsol, buka halaman Firewall policies.
    Buka Firewall policies
  2. Klik Create firewall rule untuk membuat aturan firewall kedua:
  3. Masukkan Name fw-allow-health-check.
  4. Di bagian Network, pilih Default.
  5. Di bagian Targets, pilih Specified target tags.
  6. Isi kolom Target tags dengan allow-health-check.
  7. Setel Source filter ke IPv4 ranges.
  8. Setel Source IPv4 ranges ke 130.211.0.0/22 dan 35.191.0.0/16.
  9. Di bagian Protocols and ports, pilih Specified protocols and ports.
  10. Pilih kotak centang TCP dan masukkan 80 untuk nomor port.
  11. Klik Create.

gcloud

gcloud compute firewall-rules create fw-allow-health-check \
    --network=default \
    --action=allow \
    --direction=ingress \
    --source-ranges=130.211.0.0/22,35.191.0.0/16 \
    --target-tags=allow-health-check \
    --rules=tcp

Mencadangkan alamat IP eksternal

Setelah instance aktif dan berjalan, siapkan alamat IP eksternal statis global yang akan digunakan pelanggan untuk menjangkau load balancer Anda.

Konsol

  1. Buka halaman Alamat IP eksternal di Google Cloud konsol.
    Buka halaman alamat IP Eksternal
  2. Klik Reserve static address untuk memesan alamat IPv4.
  3. Tetapkan Name untuk lb-ipv4-1.
  4. Setel paket Jaringan ke Standard.
  5. Tetapkan IP version ke IPv4.
  6. Tetapkan Type ke Global.
  7. Klik Reserve.
  8. Pastikan Type disetel ke Global.
  9. Klik Reserve.

gcloud

gcloud compute addresses create lb-ipv4-1 \
    --ip-version=IPV4 \
    --network-tier=PREMIUM \
    --global

Perhatikan alamat IPv4 yang dicadangkan:

gcloud compute addresses describe lb-ipv4-1 \
    --format="get(address)" \
    --global

Menyiapkan backend load balancer

Konsol

Konsol saat ini tidak didukung untuk menyiapkan perutean berbasis header dan berbasis parameter. Google Cloud Gunakan gcloud atau API.

gcloud

  1. Buat health check.
        gcloud compute health-checks create http http-basic-check \
            --port 80
        
  2. Buat layanan backend pertama.
    • Untuk Load Balancer Aplikasi eksternal global, gunakan perintah gcloud CLI dengan load-balancing-scheme=EXTERNAL_MANAGED. Setelan ini menawarkan kapabilitas pengelolaan traffic tingkat lanjut.
    • Untuk Load Balancer Aplikasi klasik, gunakan load-balancing-scheme=EXTERNAL.
        gcloud compute backend-services create service-a \
            --load-balancing-scheme=LOAD_BALANCING_SCHEME \
            --global-health-checks \
            --protocol HTTP \
            --health-checks http-basic-check \
            --global
        
  3. Buat layanan backend kedua.
        gcloud compute backend-services create service-b \
            --load-balancing-scheme=LOAD_BALANCING_SCHEME \
            --global-health-checks \
            --protocol HTTP \
            --health-checks http-basic-check \
            --global
        
  4. Tambahkan grup instance pertama sebagai backend ke layanan backend pertama service.
        gcloud compute backend-services add-backend service-a \
            --balancing-mode=UTILIZATION \
            --max-utilization=0.8 \
            --capacity-scaler=1 \
            --instance-group=first-example-ig \
            --instance-group-zone=us-east1-b \
            --global
        
  5. Tambahkan grup instance kedua sebagai backend ke layanan backend kedua service.
        gcloud compute backend-services add-backend service-b \
            --balancing-mode=UTILIZATION \
            --max-utilization=0.8 \
            --capacity-scaler=1 \
            --instance-group=second-example-ig \
            --instance-group-zone=us-east1-c \
            --global
        

Membuat peta URL

Konsol

Konsol saat ini tidak didukung untuk menyiapkan perutean berbasis header dan berbasis parameter. Google Cloud Gunakan gcloud atau API.

gcloud

  1. Buat file YAML /tmp/web-map-http.yaml. Ganti PROJECT_ID dengan project ID Anda.

    defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
    hostRules:
    - hosts:
      - '*'
      pathMatcher: path-matcher-1
    name: web-map-http
    pathMatchers:
    - defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
      name: path-matcher-1
      routeRules:
        - matchRules:
            - prefixMatch: /
              queryParameterMatches:
                - name: ABTest
                  exactMatch: A
          priority: 0
          service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
        - matchRules:
            - prefixMatch: /
              queryParameterMatches:
                - name: ABTest
                  exactMatch: B
          priority: 1
          service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-b
    selfLink: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/urlMaps/web-map-http
    tests:
    - description: Test routing for query ABTest with A
      host: example.com
      path: /?ABTest=A
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
      expectedOutputUrl: http://example.com/?ABTest=A
    - description: Test routing for query ABTest with B
      host: example.com
      path: /?ABTest=B
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-b
      expectedOutputUrl: http://example.com/?ABTest=B
    
  2. Validasi peta URL.

    gcloud compute url-maps validate --source /tmp/web-map-http.yaml
    

    Jika pengujian lulus dan perintah menampilkan pesan keberhasilan, simpan perubahan pada peta URL.

  3. Perbarui peta URL.

    gcloud compute url-maps import web-map-http \
       --source /tmp/web-map-http.yaml \
       --global
    

Membuat proxy target dan aturan penerusan

Konsol

Konsol saat ini tidak didukung untuk menyiapkan perutean berbasis header dan berbasis parameter. Google Cloud Gunakan gcloud atau API.

gcloud

  1. Buat proxy HTTP target untuk mengarahkan permintaan ke peta URL:
        gcloud compute target-http-proxies create http-lb-proxy \
            --url-map web-map-http
        
  2. Buat aturan penerusan global untuk mengarahkan permintaan masuk ke proxy:
    • Untuk Load Balancer Aplikasi eksternal global, gunakan perintah gcloud CLI dengan load-balancing-scheme=EXTERNAL_MANAGED. Setelan ini menawarkan kapabilitas pengelolaan traffic tingkat lanjut.
    • Untuk Load Balancer Aplikasi klasik, gunakan load-balancing-scheme=EXTERNAL.
        gcloud compute forwarding-rules create http-content-rule \
            --load-balancing-scheme=LOAD_BALANCING_SCHEME \
            --network-tier=PREMIUM \
            --address=lb-ipv4-1 \
            --global \
            --target-http-proxy=http-lb-proxy \
            --ports=80
        

Pengujian

Perhatikan alamat IPv4 yang dicadangkan:

gcloud compute addresses describe lb-ipv4-1 \
    --format="get(address)" \
    --global

Uji penyiapan ini dengan menjalankan:

curl http://IP_ADDRESS?ABTest=A
curl http://IP_ADDRESS?ABTest=B

Di browser, buka http://IP_ADDRESS?ABTest=A dan http://IP_ADDRESS?ABTest=B.

Menyiapkan perutean berbasis header HTTP

Contoh ini menunjukkan cara menambahkan dan menghapus header HTTP untuk melakukan perutean cerdas.

Sebelum memulai

Anda dapat menggunakan Load Balancer Aplikasi eksternal yang sudah ada atau membuat yang baru.

Anda dapat menggunakan fitur ini dengan salah satu jenis backend yang didukung. Contoh ini mengasumsikan bahwa Anda menggunakan VM dalam grup instance.

Untuk menyiapkan load balancer sederhana, lihat contoh berbasis parameter kueri di atas.

Memperbarui peta URL

Konsol

Konsol saat ini tidak didukung untuk menyiapkan perutean berbasis header dan berbasis parameter. Google Cloud Gunakan gcloud atau API.

gcloud

  1. Contoh ini menunjukkan cara menggunakan header permintaan HTTP untuk melakukan pengujian A/B dengan mencocokkan nilai di header HTTP permintaan.

    Buat file YAML /tmp/web-map-http.yaml. Ganti PROJECT_ID dengan project ID Anda.

    defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
    kind: compute#urlMap
    name: web-map-http
    hostRules:
    - hosts:
      - '*'
      pathMatcher: path-matcher-1
    pathMatchers:
    - defaultService: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
      name: path-matcher-1
      routeRules:
        - matchRules:
            - prefixMatch: /
              headerMatches:
                - headerName: ABTest
                  exactMatch: A
          priority: 0
          service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
        - matchRules:
            - prefixMatch: /
              headerMatches:
                - headerName: ABTest
                  exactMatch: B
          priority: 1
          service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-b
    tests:
    - description: Test routing for query ABTest with A
      host: example.com
      path: /
      headers:
      - name: ABTest
        value: A
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-a
    - description: Test routing for query ABTest with B
      host: example.com
      path: /
      headers:
      - name: ABTest
        value: B
      service: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/service-b
    
  2. Validasi peta URL.

    gcloud compute url-maps validate --source /tmp/web-map-http.yaml
    

    Jika pengujian lulus dan perintah menampilkan pesan keberhasilan, simpan perubahan pada peta URL.

  3. Perbarui peta URL.

    gcloud compute url-maps import web-map-http \
       --source /tmp/web-map-http.yaml \
       --global
    

Pengujian

Dengan menggunakan alamat IPv4 load balancer terkait, uji penyiapan ini dengan menjalankan:

curl http://IP_ADDRESS -H "ABTest: A"
curl http://IP_ADDRESS -H "ABTest: B"

Langkah berikutnya