Global Load Balancing with Google Compute Engine

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

Once you have successfully set up a group of managed instances, you have moved from the “pets” model to the “cattle” model, looking after a template rather than an individual machine so instances themselves can come and go as necessary. The final piece of the puzzle is how you direct users and traffic to your instances — and only to the healthy ones. In this lab, we will set up a managed instance group and then use a Google Cloud load balancer to manage incoming requests from the outside world.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set up HTTP Checks and a Firewall Rule

Before we can create a load balancer, we need to add a firewall rule for incoming HTTP requests and create an HTTP health check that will be used in our managed instance group and in our load balancer configuration.

Set up a Firewall Rule

  1. From the left menu, scroll down to Networking and select VPC network > Firewall.
  2. Click Create Firewall Rule.
  3. Set the following values:
    • Name: allow-http
    • Target tags: http-server
    • Source IP ranges: 0.0.0.0/0
  4. Scroll down to Protocols and ports.
  5. In Specified protocols and ports, select TCP and enter "80" in the field to the right.
  6. Leave the rest as their defaults and click Create.

    Note: If we weren’t opening HTTP to the world, we would still need to add a rule to allow incoming HTTP connections from GCP’s health check probes. For now, our "allow all" HTTP rule covers those connections already.

Set up a HTTP Health Check

  1. From the left menu, scroll down to Compute and select Compute Engine > Health checks.
  2. Click Create A Health Check.
  3. In Name, enter "http-port-80".
  4. Under Health criteria, set the following values:
    • Check interval: 10
    • Healthy threshold: 3
    • Unhealthy threshold: 3
  5. Leave the rest as their defaults and click Create.
Create the Instance Template and Managed Instance Group
  1. From the left menu, select Compute Engine > Instance groups and click Create Instance Group.

  2. In Name, enter "apache".

  3. Under Location, select Multiple zones.

  4. In Instance template, click the drop-down and select Create a new instance template.

  5. In Name, enter "apache-template".

  6. In Machine Type, select **e2-small.

  7. Scroll down to Firewall and select Allow HTTP traffic.

  8. Click Management, security, disks, networking, sole tenancy to expand the section.

  9. Under Automation, paste the following startup script:

    #! /bin/bash
    sudo apt update
    sudo apt -y install apache2
    ZONE=`curl -fs http://metadata/computeMetadata/v1/instance/zone -H "Metadata-Flavor: Google" | cut -d'/' -f4`
    cat <<EOF > /var/www/html/index.html
    <html><body><h1>Hello Cloud Gurus</h1>
    <p>This server is serving from ${ZONE}.</p>
    </body></html>
    EOF
  10. Click Save and continue

  11. Under Autoscaling, set the Minimum number of instances to "3" and the Maximum numbers of instances to "5".

  12. Under Autohealing, select http-port-80.

  13. Click Create.

    Note: After a few minutes, the instance group should be ready. You can select the instance group to see a list of the instances and click on any external IP link to see a web page that shows the zone serving the instance.

Create an HTTP Load Balancer
  1. From the left menu, scroll down to Networking and select Network services > Load balancing.
  2. Click Create load balancer.
  3. In HTTP(S) Load Balancing, click Start configuration.
  4. Click Continue.
  5. In Name, enter "apache-lb".
  6. Click Backend configuration.
  7. In Backend services & backend buckets, select Backend services > Create a backend service from the dropdown.
  8. In Name. enter "apache-backend".
  9. Scroll down to New backend and select apache as the instance group.
  10. In Port numbers, enter "80" and click Done.
  11. In Health check, select http-port-80.
  12. Click Create.
  13. Click Frontend configuration.
  14. In Name, enter "apache-frontend".
  15. Leave the rest as their defaults and click Done.
  16. Click Create.

    Note: It will take several minutes for your new configuration to be updated.

  17. To verify the load balancer works, click the load balancer to open it, copy the IP address, and load it in your browser.

Additional Resources

Log in to Google Cloud Platform by opening https://console.cloud.google.com/ in a private browser window. Then, sign in using the credentials provided on the lab page.

To complete this lab, you will need some files (such as images) that you don't mind temporarily sharing publicly. Alternatively, free images can be found at https://pixabay.com/.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?