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
- In the GCP Console, on the Products & Services menu (), click Compute Engine > VM instances.
- Click Create.
- 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
|
- Click Management, disks, networking, SSH keys.
- Click Disks, and disable Delete boot disk when instance is deleted.
- Click Create.
Customize the VM
- For webserver, click SSH to launch a terminal and connect.
- To install Apache2, run the following commands:
sudo apt-get update sudo apt-get install -y apache2
- To start the apache server, run the following command:
sudo service apache2 start
- To enable SSL and restart the apache server, run the following commands:
sudo a2ensite default-ssl sudo a2enmod ssl sudo service apache2 restart
- In the GCP Console, for webserver, click the External IP address.
- 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.
- In the webserver SSH terminal, set the service to start on boot:
sudo update-rc.d apache2 enable
- In the GCP Console, select webserver, and then click Reset.
- In the confirmation dialog, click Reset.
- For webserver, click the External IP address of the instance to verify that the Apache service is available. You should see the default page.
- You can also check the server by connecting via SSH to the VM and entering the following command:
sudo service apache2 status
- The result should show Started The Apache HTTP Server.
Prepare the disk to create a custom image
- 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.
- Return to the VM instances page, click webserver, and click Delete.
- In the confirmation dialog, click Delete.
- In the left pane, click Disks and verify that the webserver disk exists.
Create the custom image
- In the left pane, click Images.
- Click Create image.
- 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
|
- Click Create.
Task 2: Create an Instance Template based on the custom image
- In the left pane, click Instance templates.
- Click Create instance template.
- 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)
|
- For Boot disk, click Change.
- Click Custom images.
- Select mywebserver1.
- Click Select.
- For Firewall, enable Allow HTTP traffic and Allow HTTPS traffic.
- 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
- In the left pane, click Instance groups.
- Click Create instance group.
- 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
|
- For Health check, select Create a health check.
- For Name, type webserver-healthcheck
- Click Save and continue.
- 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.
- 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.
- Click OK.
- In the left pane, click VM instances.
- Test the VM by clicking on the External IP address of the instance in the console.
- 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
- On the Products & services menu, click Network services > Load balancing.
- Click Create load balancer.
- In HTTP(S) Load Balancing, click Start configuration.
- Click Frontend configuration.
- For Name, type mywebserver-frontend
- Leave the remaining settings as their defaults, and click Done.
- Click Backend configuration.
- Click Create or select a backend service & backend buckets > Backend services > Create a backend service.
- For Name, type mywebserver-backend
- In Backends > New backend, for Instance group, click mywebserver-group.
- Leave the remaining settings as their defaults, and click Done.
- For Health check, click webserver-healthcheck.
- Click Create.
- Enter a name for your HTTP(S) load balancer: webserver-load-balancer
- Click Create.
- Click webserver-load-balancer.
- Find the External IP that was assigned to the Frontend, which is later referred to as [YOUR_LB_IP].
- On the Products & services menu, click Compute Engine > Instance groups.
- 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.
- In the GCP Console, on the Products & Services menu, click Compute Engine > VM instances.
- Click Create instance.
- 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)
|
- For Boot Disk, click Change.
- Click Custom images.
- Select mywebserver1.
- Click Select, and then click Create.
- On the VM instances page, for stress-test, click SSH to launch a terminal and connect.
- To create an environment variable for your load balancer IP address, run the following command:
export LB_IP=<Enter [YOUR_LB_IP] here>
- To place a load on the load balancer, run the following command:
ab -n 50000 -c 1000 http://$LB_IP/
- In the GCP Console, in the left pane, click Instance groups.
- 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
- In the Cloud Platform Console, sign out of the Google account.
- 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
Post a Comment