Overview

In this hands-on lab you'll learn the differences between a network load balancer and an HTTP load balancer and how to set them up for your applications running on Compute Engine virtual machines (VMs).

There are several ways you can load balance on Google Cloud. This lab takes you through the set up of the following load balancers:

You are encouraged to type the commands yourself, which can help you learn the core concepts. Many labs include a code block that contains the required commands. You can easily copy and paste the commands from the code block into the appropriate places during the lab.

What you'll do

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

  1. In Cloud Shell, set the default zone:

    gcloud config set compute/zone us-central1-a
    
  2. Set the default region:

    gcloud config set compute/region us-central1
    

Task 2: Create multiple web server instances

For this load balancing scenario, create three Compute Engine VM instances and install Apache on them, then add a firewall rule that allows HTTP traffic to reach the instances.

  1. Create three new virtual machines in your default zone and give them all the same tag. The code provided sets the zone to us-central1-a. Setting the tags field lets you reference these instances all at once, such as with a firewall rule. These commands also install Apache on each instance and give each instance a unique home page.

    gcloud compute instances create www1 \\
      --image-family debian-9 \\
      --image-project debian-cloud \\
      --zone us-central1-a \\
      --tags network-lb-tag \\
      --metadata startup-script="#! /bin/bash
        sudo apt-get update
        sudo apt-get install apache2 -y
        sudo service apache2 restart
        echo '<!doctype html><html><body><h1>www1</h1></body></html>' | tee /var/www/html/index.html"
    
    gcloud compute instances create www2 \\
      --image-family debian-9 \\
      --image-project debian-cloud \\
      --zone us-central1-a \\
      --tags network-lb-tag \\
      --metadata startup-script="#! /bin/bash
        sudo apt-get update
        sudo apt-get install apache2 -y
        sudo service apache2 restart
        echo '<!doctype html><html><body><h1>www2</h1></body></html>' | tee /var/www/html/index.html"
    
    gcloud compute instances create www3 \\
      --image-family debian-9 \\
      --image-project debian-cloud \\
      --zone us-central1-a \\
      --tags network-lb-tag \\
      --metadata startup-script="#! /bin/bash
        sudo apt-get update
        sudo apt-get install apache2 -y
        sudo service apache2 restart
        echo '<!doctype html><html><body><h1>www3</h1></body></html>' | tee /var/www/html/index.html"
    
  2. Create a firewall rule to allow external traffic to the VM instances:

    gcloud compute firewall-rules create www-firewall-network-lb \\
        --target-tags network-lb-tag --allow tcp:80
    

    Now you need to get the external IP addresses of your instances and verify that they are running.

  3. Run the following to list your instances. You'll see their IP addresses in the EXTERNAL_IP column:

    gcloud compute instances list
    
  4. Verify that each instance is running with curl, replacing [IP_ADDRESS] with the IP address for each of your VMs:

    curl http://[IP_ADDRESS]