Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

Global Load Balancing with Google Compute Engine

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.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Apr 09, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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.
  2. Challenge

    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.

  3. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans