Tuesday, November 12, 2024
GCP Load Balancer

Set Up Network and HTTP Load Balancers

load

“Set Up Network and HTTP Load Balancers”

Daftar Isi

Pengantar

Dalam lab praktis ini, Anda akan mempelajari perbedaan antara load balancer jaringan dan load balancer HTTP, serta cara menyiapkannya untuk aplikasi Anda yang berjalan di mesin virtual (VM) Compute Engine.

Solusi

Task 1. Set the default region and zone for all resources

  • settting region dan zone
gcloud config set compute/zone 
gcloud config set compute/region 

Task 2. Create multiple web server instances

  • Buat www1
  gcloud compute instances create www1 \
    --zone= \
    --tags=network-lb-tag \
    --machine-type=e2-small \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --metadata=startup-script='#!/bin/bash
      apt-get update
      apt-get install apache2 -y
      service apache2 restart
      echo "
<h3>Web Server: www1</h3>" | tee /var/www/html/index.html'
  • Buat www2
  gcloud compute instances create www2 \
    --zone= \
    --tags=network-lb-tag \
    --machine-type=e2-small \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --metadata=startup-script='#!/bin/bash
      apt-get update
      apt-get install apache2 -y
      service apache2 restart
      echo "
<h3>Web Server: www2</h3>" | tee /var/www/html/index.html'
  • Buat www3
  gcloud compute instances create www3 \
    --zone= \
    --tags=network-lb-tag \
    --machine-type=e2-small \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --metadata=startup-script='#!/bin/bash
      apt-get update
      apt-get install apache2 -y
      service apache2 restart
      echo "
<h3>Web Server: www3</h3>" | tee /var/www/html/index.html'
gcloud compute firewall-rules create www-firewall-network-lb \
    --target-tags network-lb-tag --allow tcp:80
  • Cek Ip external
gcloud compute instances list
curl http://[IP_ADDRESS]

Task 3. Configure the load balancing service

   gcloud compute addresses create network-lb-ip-1 \
    --region  
  • Tambahkan http health check
gcloud compute http-health-checks create basic-check
  • Tambahkan target pool
  gcloud compute target-pools create www-pool \
    --region  --http-health-check basic-check
gcloud compute target-pools add-instances www-pool \
    --instances www1,www2,www3
  • Tambahkan forwading Rule
gcloud compute forwarding-rules create www-rule \
    --region   \
    --ports 80 \
    --address network-lb-ip-1 \
    --target-pool www-pool

Task 4. Sending traffic to your instances

  • Cek Ip external
gcloud compute forwarding-rules describe www-rule --region 

IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region  --format="json" | jq -r .IPAddress)

echo $IPADDRESS

while true; do curl -m1 $IPADDRESS; done

Task 5. Create an HTTP load balancer

gcloud compute instance-templates create lb-backend-template \
   --region= \
   --network=default \
   --subnet=default \
   --tags=allow-health-check \
   --machine-type=e2-medium \
   --image-family=debian-11 \
   --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://169.254.169.254/computeMetadata/v1/instance/name)"
     echo "Page served from: $vm_hostname" | \
     tee /var/www/html/index.html
     systemctl restart apache2'
  • Buat instance group based
gcloud compute instance-groups managed create lb-backend-group \
   --template=lb-backend-template --size=2 --zone= 
  • Buat health-check rule
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:80
gcloud compute addresses create lb-ipv4-1 \
  --ip-version=IPV4 \
  --global


gcloud compute addresses describe lb-ipv4-1 \
  --format="get(address)" \
  --global
  • Buat health check untuk load balancer
gcloud compute health-checks create http http-basic-check \
  --port 80
  • buat Backend service
gcloud compute backend-services create web-backend-service \
  --protocol=HTTP \
  --port-name=http \
  --health-checks=http-basic-check \
  --global
  • Masukan instance grup ke backend
gcloud compute backend-services add-backend web-backend-service \
  --instance-group=lb-backend-group \
  --instance-group-zone= \
  --global
  • Buat Url map untuk routing incoming request
gcloud compute url-maps create web-map-http \
    --default-service web-backend-service
gcloud compute target-http-proxies create http-lb-proxy \
    --url-map web-map-http
  • Buat global forwading rule
gcloud compute forwarding-rules create http-content-rule \
    --address=lb-ipv4-1\
    --global \
    --target-http-proxy=http-lb-proxy \
    --ports=80

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Set Up Network and HTTP Load Balancers. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

(Visited 173 times, 1 visits today)

Similar Posts