Load Balancing Google Compute Engine Instances

Hands-On Lab

 

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

To maximize the access of virtual machines, you’ll need to optimize the traffic each receives. The proper tool to achieve this is a load balancer. In order to best use a load balancer, you’ll need to make sure other systems are in place, including correctly configured firewall rules, instance health checks, and uniformly tagged instances. In this hands-on lab, we’ll set up three Compute Engine instances, a firewall rule to allow internet traffic, and a load balancer that includes an appropriately configured backend (with health checks) and a reserved static IP address for easy access.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Load Balancing Google Compute Engine Instances

Introduction

In this hands-on lab, we’ll set up three Compute Engine instances, a firewall rule to allow internet traffic, and a load balancer that includes an appropriately configured backend (with health checks) and a reserved static IP address for easy access.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Solution

Create the First Compute Engine Instance

  1. From the main navigation, choose Compute Engine > VM instances.
  2. In the VM instances area, click Create.
  3. With New VM instance chosen from the options on the left, configure your instance:

    • Name: la-vm-1
    • Keep the suggested region and zone.
    • Under Firewall, choose Allow HTTP traffic.
    • Expand Management, security, disks, networking, sole tenancy.
    • Select the Management tab, and then copy the following and paste it in the Automation > Startup script field:
    #! /bin/bash
    sudo apt-get update
    sudo apt-get install apache2 -y
    sudo service apache2 restart
    echo '<!doctype html><html><body><h1 style="text-align:center; margin-top: 16px;">Linux Academy 1</h1></body></html>' | tee /var/www/html/index.html
    EOF
    • Select the Networking tab, and, in the Network tags field, enter "la-network-tag".
  4. Click Create.

Create the Other Compute Engine Instances

  1. On the VM instances page, click la-vm-1.
  2. Click Create similar.
  3. Keep the new name, and continue configuring the instance:
    • Expand Management, security, disks, networking, sole tenancy.
    • Select the Management tab, and, in the Startup script field, change Linux Academy 1 to Linux Academy 2.
  4. Click Create.
  5. On the VM instances page, click la-vm-1.
  6. Click Create similar.
  7. Keep the new name, and continue configuring the instance:
    • Expand Management, security, disks, networking, sole tenancy.
    • Select the Management tab, and, in the Startup script field, change Linux Academy 1 to Linux Academy 3.
  8. Click Create.

Verify Instances Are Running Properly

  1. From the main navigation, choose Compute Engine > VM instances.
  2. Copy the External IP value for the first VM instance.
  3. In a new browser tab, paste the IP address.
  4. Confirm the expected VM instance is displayed.
  5. Repeat steps 2–4 for the other two VM instances.

Create a Firewall Rule

  1. From the main navigation, choose VPC network > Firewall rules.
  2. Click Create firewall rule.
  3. Configure your firewall rule:
    • Name: la-firewall-lb
    • Targets: Specified target tags
    • Target tags: la-network-tag
    • Source IP ranges: 0.0.0.0/0
    • Protocols and ports: Specified protocols and ports
      • Check the box next to tcp.
      • In the associated field, enter 80.
  4. Click Create.

Create Load Balancer

  1. From the main navigation, choose Network services > Load balancing.
  2. On the Load balancing page, click Create load balancer.
  3. In the TCP Load Balancing section, click Start configuration.
  4. For Internet facing or internal only, select From Internet to my VMs.
  5. For Multiple regions or single region, select Single region only.
  6. Click Continue.
  7. Give it a name of "la-lb-001".
  8. Click Backend configuration, and set the following configurations:
    • Name: Pre-populated
    • Region: Choose your default region.
    • Backends: Select existing instances
      • Click Add an instance, and select la-vm-1.
      • Click Add an instance again, and select la-vm-2.
      • Click Add an instance again, and select la-vm-3.
    • Health check: Create a health check
      • For the health check name, enter "la-health-check".
      • Click Save and continue.
  9. Click Frontend configuration, and set the following configurations:
    • Name: la-frontend
    • IP: Create IP address
      • In the Reserve a new static IP address dialog, assign a unique name (e.g., "la-ip-" with a series of random numbers at the end).
      • Click Reserve.
    • Port: 80
  10. Click Done.
  11. Click Review and finalize.
  12. If the settings are correct, click Create.

Test Load Balancer

  1. On the Load balancing page, click the name of our newly created load balancer.

  2. Copy the IP address listed in the Frontend section.

  3. Activate the Cloud Shell.

  4. Enter the following command to test the load balancer:

    while true; do curl -m1 <IP_ADDRESS>; done
  5. Note that all three VMs' output is present.

  6. Press Ctrl+C to stop the process.

Conclusion

Congratulations on completing this hands-on lab!