Skip to main content

Auto Scaling

Autoscaling

Overview

Managed instance groups offer autoscaling capabilities that allow you to automatically add or remove instances from a managed instance group based on increases or decreases in load. Autoscaling helps your applications gracefully handle increases in traffic and reduces cost when the need for resources is lower. You just define the autoscaling policy, and the autoscaler performs automatic scaling based on the measured load.
Autoscaling works by scaling your instance group in or out. That is, it adds more instances to your instance group when there is more load (scaling out) and removes instances when the need for instances is lowered (scaling in).

Objectives

In this lab, you learn how to perform the following tasks:
  • Create a custom image for a web server
  • Create an instance template based on the custom image
  • Create a managed instance group
  • Create a load balancer
  • Stress test the autoscaler

What you'll need

To complete this lab, you'll need:
  • Access to a standard internet browser (Chrome browser recommended).
  • Time. Note the lab's Completion time in Qwiklabs, which is an estimate of the time it should take to complete all steps. Plan your schedule so you have time to complete the lab. Once you start the lab, you will not be able to pause and return later (you begin at step 1 every time you start a lab).
  • You do NOT need a Google Cloud Platform account or project. An account, project and associated resources are provided to you as part of this lab.
  • If you already have your own GCP account, make sure you do not use it for this lab.
  • If your lab prompts you to log into the console, use only the student account provided to you by the lab. This prevents you from incurring charges for lab activities in your personal GCP account.

Start your lab

When you are ready, click Start Lab. You can track your lab's progress with the status bar at the top of your screen.

Find Your Lab's GCP Username and Password

To access the resources and console for this lab, locate the Connection Details panel in Qwiklabs. Here you will find the account ID and password for the account you will use to log in to the Google Cloud Platform:
If your lab provides other resource identifiers or connection-related information, it will appear on this panel as well.

Task 1: Create a custom image for a web server

Create a VM

  1. In the GCP Console, on the Products & Services menu (), click Compute Engine > VM instances.
  2. Click Create.
  3. Specify the following, and leave the remaining settings as their defaults:
Property
Value
(type value or select option as specified)
Name
webserver
Zone
Choose a zone
Machine type
micro (1 shared vCPU)
Firewall
Allow HTTP traffic
Allow HTTPS traffic
  1. Click Management, disks, networking, SSH keys.
  2. Click Disks, and disable Delete boot disk when instance is deleted.
  3. Click Create.

Customize the VM

  1. For webserver, click SSH to launch a terminal and connect.
  2. To install Apache2, run the following commands:
sudo apt-get update
sudo apt-get install -y apache2
  1. To start the apache server, run the following command:
sudo service apache2 start
  1. To enable SSL and restart the apache server, run the following commands:
sudo a2ensite default-ssl
sudo a2enmod ssl
sudo service apache2 restart
  1. In the GCP Console, for webserver, click the External IP address.
  2. Click through the warning to see the actual page. For example, in Chrome, clickAdvanced, and then click Proceed to External IP Address.The default page for the Apache2 server should be displayed.

Set the Apache service to start at boot

The software installation was successful. However, when a new VM is created using this image, the freshly booted VM does not have the Apache web server running. Use the following command to set the Apache service to automatically start on boot. Then test it to make sure it works.
  1. In the webserver SSH terminal, set the service to start on boot:
sudo update-rc.d apache2 enable
  1. In the GCP Console, select webserver, and then click Reset.
  2. In the confirmation dialog, click Reset.
  1. For webserver, click the External IP address of the instance to verify that the Apache service is available. You should see the default page.
  2. You can also check the server by connecting via SSH to the VM and entering the following command:
sudo service apache2 status
  1. The result should show Started The Apache HTTP Server.

Prepare the disk to create a custom image

  1. On the VM instances page, click webserver to view the VM instance details and verify that Delete boot disk when instance is deleted is disabled.
  2. Return to the VM instances page, click webserver, and click Delete.
  3. In the confirmation dialog, click Delete.
  4. In the left pane, click Disks and verify that the webserver disk exists.

Create the custom image

  1. In the left pane, click Images.
  2. Click Create image.
  3. Specify the following, and leave the remaining settings as their defaults:
Property
Value
(type value or select option as specified)
Name
mywebserver1
Source
Disk
Source disk
webserver
  1. Click Create.

Task 2: Create an Instance Template based on the custom image

  1. In the left pane, click Instance templates.
  2. Click Create instance template.
  3. Specify the following, and leave the remaining settings as their defaults:
Property
Value
(type value or select option as specified)
Name
webserver-template
Machine type
micro (1 shared vCPU)
  1. For Boot disk, click Change.
  2. Click Custom images.
  3. Select mywebserver1.
  4. Click Select.
  5. For Firewall, enable Allow HTTP traffic and Allow HTTPS traffic.
  6. Click Create.
You created an instance template from the custom image. Now you can use it in a Managed Instance Group.

Task 3: Create a managed instance group

  1. In the left pane, click Instance groups.
  2. Click Create instance group.
  3. Specify the following, and leave the remaining settings as their defaults:
Property
Value
(type value or select option as specified)
Name
mywebserver-group
Location
Multi-zone
Region
Choose a region
Instance template
webserver-template
Autoscaling
On
Autoscale based on
HTTP load balancing usage
Maximum number of instances
5
  1. For Health check, select Create a health check.
  2. For Name, type webserver-healthcheck
  3. Click Save and continue.
  4. For Initial delay, type 60. This is how long the Instance Group waits after initializing the boot-up of a VM before it tries a health check. You don't want to wait 5 minutes for this during the lab, so you set it to 1 minute.
  5. Click Create. A warning tells you that there is no load balancer. That's OK; you are going to create and attach one to the Managed Instance Group in the next section.
  6. Click OK.
  7. In the left pane, click VM instances.
  8. Test the VM by clicking on the External IP address of the instance in the console.
  9. Click through the warning to see the actual page. For example, in Chrome, click Advanced, and then click Proceed to External IP Address.

Task 4: Create a load balancer

  1. On the Products & services menu, click Network services Load balancing.
  2. Click Create load balancer.
  3. In HTTP(S) Load Balancing, click Start configuration.
  4. Click Frontend configuration.
  5. For Name, type mywebserver-frontend
  6. Leave the remaining settings as their defaults, and click Done.
  7. Click Backend configuration.
  8. Click Create or select a backend service & backend buckets > Backend services > Create a backend service.
  9. For Name, type mywebserver-backend
  10. In Backends > New backend, for Instance group, click mywebserver-group.
  11. Leave the remaining settings as their defaults, and click Done.
  12. For Health check, click webserver-healthcheck.
  13. Click Create.
  14. Enter a name for your HTTP(S) load balancer: webserver-load-balancer
  15. Click Create.
  1. Click webserver-load-balancer.
  2. Find the External IP that was assigned to the Frontend, which is later referred to as [YOUR_LB_IP].
  3. On the Products & services menu, click Compute Engine > Instance groups.
  1. Open a new browser tab or window and browse to the load balancer's IP using http://[YOUR_LB_IP]/. You should see the Apache default page.

Task 5: Stress test the Autoscaler

The entire configuration is working, as evidenced by the fact that you could browse to the load balancer's IP and view the default page on the web server. However, you need to see if the Autoscaler is working and will actually start new VMs in response to a load.
To test this you need software that can send repeated requests to a web server. Fortunately, free web server benchmarking software, called Apache Bench, is part of the Apache2 package.
That means that when you created the webserver custom image, you also installed and created the image with pre-installed software for a benchmark server.
  1. In the GCP Console, on the Products & Services menu, click Compute Engine VM instances.
  2. Click Create instance.
  3. Specify the following, and leave the remaining settings as their defaults:
Property
Value
(type value or select option as specified)
Name
stress-test
Zone
Choose a zone
Machine type
micro (1 shared vCPU)
  1. For Boot Disk, click Change.
  2. Click Custom images.
  3. Select mywebserver1.
  4. Click Select, and then click Create.
  5. On the VM instances page, for stress-test, click SSH to launch a terminal and connect.
  6. To create an environment variable for your load balancer IP address, run the following command:
export LB_IP=<Enter [YOUR_LB_IP] here>
  1. To place a load on the load balancer, run the following command:
ab -n 50000 -c 1000 http://$LB_IP/
  1. In the GCP Console, in the left pane, click Instance groups.
  2. Click mywebserver-group. Verify that new instances have been created.

Task 6: Review

In this lab you set up an HTTP(S) load balancer with autoscaling and verified that it was working. To do this, you first created a VM, then you customized it by installing software and changing a configuration setting (making Apache start on boot). You used the custom image in an instance template, and then used the image template to make a managed instance group. After all the backend and frontend parts were connected together, you stress-tested the system and triggered autoscaling using Apache bench.
Cleanup
  1. In the Cloud Platform Console, sign out of the Google account.
  2. Close the browser tab.
Last Updated: 2018-03-27
End your lab
When you have completed your lab, click End Lab. Qwiklabs removes the resources you've used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

Comments

Popular posts from this blog

AWS Cheat Sheet

Year in Review | Facebook New Year video | Facebook

FB New Year in Review How can I see, edit and share my Year in Review? Your Year in Review is a personalized video that lets you highlight and share your meaningful moments from this year. These moments can include photos and posts that you've shared or been tagged in. To see your Year in Review visit  facebook.com/yearinreview2016  or click  Watch Yours  on a Year in Review that has been shared by a friend. You may also see your Year in Review video in your News Feed, but it's only visible to you unless you share it. To share your Year in Review: Go to  facebook.com/yearinreview2016 Click  Share Video Select the audience  for the post Click  Post To edit your Year in Review before you share it: Go to  facebook.com/yearinreview2016 Click  Edit Video  and then choose the photos you want to appear in your video Click  Share Select the audience  for the post Click  Post You can also edit and share your video by clicking  Share Video  or  Edi

Why Upgrading to Terraform 1.0. Should be a Priority ?

  HashiCorp Terraform version 1.0, released this week, contains few new technical feature updates. But that's actually the point. The company is known for its unconventional philosophy on what constitutes a "version 1.0" product and has spent seven years updating, supporting and marketing the infrastructure-as-code tool without this designation. Other HashiCorp products such as  Nomad  container orchestration and  Vault  secrets management also spent long periods being used in production before reaching version 1.0. Terraform is used to define infrastructure resources using programming code, which DevOps teams can then automatically test and deploy alongside applications using the same processes. Terraform is among the most widely used such tools, with more than 100 million open source downloads to date. The HashiCorp-hosted Terraform Cloud has amassed 120,000 customers. Despite its widespread production use, each new version of Terraform over the last three years came wi