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
- From the left menu, scroll down to Networking and select VPC network > Firewall.
- Click Create Firewall Rule.
- Set the following values:
- Name: allow-http
- Target tags: http-server
- Source IP ranges: 0.0.0.0/0
- Scroll down to Protocols and ports.
- In Specified protocols and ports, select TCP and enter "80" in the field to the right.
- 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
- From the left menu, scroll down to Compute and select Compute Engine > Health checks.
- Click Create A Health Check.
- In Name, enter "http-port-80".
- Under Health criteria, set the following values:
- Check interval: 10
- Healthy threshold: 3
- Unhealthy threshold: 3
- Leave the rest as their defaults and click Create.
- Create the Instance Template and Managed Instance Group
From the left menu, select Compute Engine > Instance groups and click Create Instance Group.
In Name, enter "apache".
Under Location, select Multiple zones.
In Instance template, click the drop-down and select Create a new instance template.
In Name, enter "apache-template".
In Machine Type, select **e2-small.
Scroll down to Firewall and select Allow HTTP traffic.
Click Management, security, disks, networking, sole tenancy to expand the section.
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
Click Save and continue
Under Autoscaling, set the Minimum number of instances to "3" and the Maximum numbers of instances to "5".
Under Autohealing, select http-port-80.
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
- From the left menu, scroll down to Networking and select Network services > Load balancing.
- Click Create load balancer.
- In HTTP(S) Load Balancing, click Start configuration.
- Click Continue.
- In Name, enter "apache-lb".
- Click Backend configuration.
- In Backend services & backend buckets, select Backend services > Create a backend service from the dropdown.
- In Name. enter "apache-backend".
- Scroll down to New backend and select apache as the instance group.
- In Port numbers, enter "80" and click Done.
- In Health check, select http-port-80.
- Click Create.
- Click Frontend configuration.
- In Name, enter "apache-frontend".
- Leave the rest as their defaults and click Done.
- Click Create.
Note: It will take several minutes for your new configuration to be updated.
- To verify the load balancer works, click the load balancer to open it, copy the IP address, and load it in your browser.