In this lab, you'll set up an manged instance group with a web server on each instance, configure autoscaling and load balancing, and test scaling and balancing under load. You'll use an HTTP load balancer to scale instances based on network traffic, distribute load across availability zones, and set up a firewall rule allowing ingress HTTP traffic.
What you will build
An instance template
A managed instance group
An HTTP load balancer
A firewall rule for HTTP traffic.
What you'll learn how to
Create an instance template with a web app on it
Create a managed instance group that uses the template to scale
Create an HTTP load balancer that scales instances based on traffic and distributes load across availability zones
Create a managed instance group that uses the template and sets an auto scaling policy:
Either go to Compute Engine > Instance groups
Give the instance group a name like apache-service-1
Set Location to multi-zone and select a region like us-central1 where you want your instances to run.
Select the template you just created (if it's not pre-populated)
Turn autoscaling On
Set Autoscaling based on to HTTP load balancing usage, which you'll set shortly
Set Target load balancing usage to 100 percent of the load balancing capacity
Set Minimum number of instances to 2
Set Maximum number of instances to 10
Set Cool-down period to 60 seconds
Click the Health Check drop-down and select Create a health check
Give it a unique name like port80 and click Save and continue
Set Initial delay to 120 seconds
Notice there was a tiny optional link between Region and Instance template settings called Specify port name mapping, but don't click it.
Double check all your settings and click Create
In a minute, look for an error and warning, zone set to multi-zone, running instances as 2, and In use by as empty (the load balancer will appear here)
Click the group's link to see the running instances like the following:
Go to the Networking > Firewall rules to open port 80 for your web servers (you'll learn more about this later):
Click Create firewall rule
Give it a name like default-allow-http
Make sure the default network is selected along with ingress and allow
Set Targets to Specified target tags (these are instances the rule applies to)
Set Target tags to http-server (the instance template sets these)
Set Source filter to IP ranges (these are hosts requests come from)
Set Source IP ranges to 0.0.0.0/0 (this accepts requests from all hosts)
Click each instance's external IP link to test that it's working and returns its unique hostname.
Now you'll put the group behind a load balancer and test it under load.
Go to Networking > Load balancing
Go to Networking > Load balancing
Click Create load balancer
Click Start configuration for the HTTP Load Balancer option
Enter a name for the load balancer like apache-lb
Click backend configuration to setup the backend service and a backend containing your instance group
Click Create or select a backend service and select Create a backend service
Set the Backend service name to apache-service
Under Backends, select your instance group you just created from the Instance group drop-down and notice the port number filled in for you
Set Balancing mode to Rate
Set Maximum RPS to 10 per instance
Leave Capacity at 100 percent
Select Health check to port80 (HTTP)
Leave Session affinity and Affinity cookie TTL as defaults
Note the green check mark when it's complete. If not, check for completeness
Click Host and path rules
Note the green check mark
Click Front end configuration
Enter a name for it like port80, get an ephemeral IP, and click Done.
When done, click Create to save and activate the load balancer.
Test the load balancer to make sure it's working
Return to the Instance Groups page and refresh it until you see the load balancer show up under In use by on the right.
Refresh more until you get a yellow triangle saying the group hasn't received queries from the load balancer. This will change once traffic is flowing.
Return to the Load Balancing page and click Frontends tab to get your load balancer's external IP address.
Wait a minute or two for the instance and web server to start up.
Send a request from your laptop's web browser to the load balancer's external IP. You should get a response from one of the instances. Note its hostname's last 4 characters.
Send another request to get a response from the other host (note the last 4 characters are different).
Test the autoscaler and load balancer under load
For this section, you'll use a load testing tool called 'hey'.
Open your Cloud Shell and enter the following command to install it:
go get -u github.com/rakyll/hey
Enter the following command to send 10 requests per second to the load balancer (substituting your load balancer's external IP):
hey -n 12000 -c 10 -q 1 http://<load-balancer-IP>
Let this command keep sending requests for at least 5 minutes (maybe 10)
After it's been running for about 2 minutes, return to the Load Balancing page to view some of your traffic
Click the load balancer's link
Click the Monitoring tab
Select the apache-service from the Backend drop-down to see traffic split 50/50 between the two instances (1 per zone) with each receiving about 5 RPS. Since each instance is receiving only 5 RPS, and the limit is 10, no new instances are created.
Look at the bottom to see how many instances are running in each zone and the specific RPS. On the far left, note your front end location (North America in this case).
Return to the Instance Groups page and click your instance group's link to see the instances (no change yet, they're only receiving 5 RPS).
Let the system cool for 10 minutes.
Then run the command again, but double the rate to 20 RPS and number of total requests:
hey -n 24000 -c 10 -q 2 http://<load-balancer-IP>
After 2 minutes, return to the load balancer's monitoring page.
Initially both instances showed orange bar status on the bottom-right and were maxing out with 10+ RPS because one of the instances was a bit flaky. So a second instance was spun up in the us-central1-b zone (note 2 of 2 instances healthy in the bottom-middle). Now us-central1-b instances show green bar status on the bottom-right (indicating half utilization per instance or 5 RPS each). But the spread across zones isn't optimal and the third instance in us-central1-c is still maxing out
So after about 4 minutes, a fourth instance was spun up, and to distribute load across zones, it was started in a third zone, us-central1-f (shown below):
Now the traffic load is split evenly across all four instances (see the green bar charts bottom-right in the screenshot above). But it's really not necessary to have four instances serving 20 RPS with a 10 RPS limit per instance. So eventually, the system reduced to 3 instances, one per zone, as shown in the instance group's page with about 7 RPS per instance (RPS not shown).
Go to the Load balancing page and click the load balancer's link.
Click the trash can icon to delete the load balancer.
Click the Backends tab.
Select the backend and click Delete.
Go to the Instance groups page.
Select the instance group and click Delete.
Go to the Instance templates page, select your template, and click Delete.
Congratulations! Now you've seen how to scale and load balance web applications on instances.