Skip to content

Latest commit

 

History

History
258 lines (198 loc) · 5.82 KB

File metadata and controls

258 lines (198 loc) · 5.82 KB

GSP007 - Set Up Network and HTTP Load Balancers

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

  1. Set default region

    gcloud config set compute/zone us-central1-f
  2. Set default zone

    gcloud config set compute/region us-central1

Task 2. Create multiple web server instances

  1. Create VM www1

      gcloud compute instances create www1 \
        --zone=us-central1-f \
        --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'
  2. Create VM www2

      gcloud compute instances create www2 \
        --zone=us-central1-f \
        --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'
  3. Create VM www3

      gcloud compute instances create www3 \
        --zone=us-central1-f \
        --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'
  4. firewall rule

    gcloud compute firewall-rules create www-firewall-network-lb \
        --target-tags network-lb-tag --allow tcp:80
  5. Dapetin ip address

    gcloud compute instances list
  6. Cek apakah web server udah jalan

    curl http://[EXTERNAL_IP]

Task 3. Configure the load balancing service

  1. buat static ip untuk load balancer

    gcloud compute addresses create network-lb-ip-1 \
        --region us-central1
  2. buat health check

    gcloud compute http-health-checks create basic-check
  3. add target pool

     gcloud compute target-pools create www-pool \
         --region us-central1 --http-health-check basic-check
  4. add instances to pool

    gcloud compute target-pools add-instances www-pool \
        --instances www1,www2,www3
  5. add forwarding rule

    gcloud compute forwarding-rules create www-rule \
        --region  us-central1 \
        --ports 80 \
        --address network-lb-ip-1 \
        --target-pool www-pool

Task 4. Sending traffic to your instances

  1. cek ip address forwarding rule

    gcloud compute forwarding-rules describe www-rule --region us-central1
  2. simpan ke variable

    IPADDRESS=$(gcloud compute forwarding-rules describe www-rule --region us-central1 --format="json" | jq -r .IPAddress)
  3. request ke ip address

    while true; do curl -m1 $IPADDRESS; done

Task 5. Create an HTTP load balancer

  1. Buat load balancer template

    gcloud compute instance-templates create lb-backend-template \
        --region=us-central1 \
        --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'
  2. Buat instance group

    gcloud compute instance-groups managed create lb-backend-group \
        --template=lb-backend-template --size=2 --zone=us-central1-f
  3. Buat health check

    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
  4. Set global static ip address

    gcloud compute addresses create lb-ipv4-1 \
        --ip-version=IPV4 \
        --global
    gcloud compute addresses describe lb-ipv4-1 \
        --format="get(address)" \
        --global
  5. Buat health check

        gcloud compute health-checks create http http-basic-check \
        --port 80
  6. Buat backend service

    gcloud compute backend-services create web-backend-service \
        --protocol=HTTP \
        --port-name=http \
        --health-checks=http-basic-check \
        --global
  7. Add instance group to backend service

    gcloud compute backend-services add-backend web-backend-service \
        --instance-group=lb-backend-group \
        --instance-group-zone=us-central1-f \
        --global
  8. Buat url map

    gcloud compute url-maps create web-map-http \
        --default-service web-backend-service
  9. Buat target http proxy

    gcloud compute target-http-proxies create http-lb-proxy \
        --url-map web-map-http
  10. Buat global forwarding rule

    gcloud compute forwarding-rules create http-content-rule \
        --address=lb-ipv4-1\
        --global \
        --target-http-proxy=http-lb-proxy \
        --ports=80

Task 6. Testing traffic sent to your instances

  1. Navigation > Network services > Load balancing