menu
arrow_back

Autoscaling an Instance Group with Stackdriver Custom Metrics

60m access · 45m completion
Connection Details

7 Credits

info_outline
This lab costs 7 Credits to run. You can purchase credits or a subscription under My Account.

01:00:00

Autoscaling an Instance Group with Stackdriver Custom Metrics

GSP087

This lab will you will create a Compute Engine managed instance group that autoscales based on the value of a custom Stackdriver Monitoring metric.

Objectives

  • Deploy an autoscaling Compute Engine instance group.
  • Create a custom metric used to scale the instance group.
  • Use the Google Cloud Platform Console to visualize the custom metric and instance group size.

Application architecture

The autoscaling application uses a Node.js script installed on Compute Engine instances. The script reports a numeric value to a Stackdriver metric. You do not need to know Node.js or JavaScript for this lab. In response to the value of the metric, the application autoscales the Compute Engine instance group up or down as needed.

The Node.js script is used to seed a custom metric with values that the instance group can respond to. In a production environment, you would base autoscaling on a metric that is relevant to your use case.

The application includes the following components:

  1. Compute Engine instance template—A template used to create each instance in the instance group.
  2. Cloud Storage. A bucket used to host the startup script and other script files.
  3. Compute Engine startup script—A startup script that installs the necessary code components on each instance. The startup script is installed and started automatically when an instance starts. When the startup script runs, it in turn installs and starts code on the instance that writes values to the Stackdriver custom metric.
  4. Compute Engine instance group—An instance group that autoscales based on the Stackdriver monitoring metric values.
  5. Compute Engine instances—A variable number of Compute Engine instances.
  6. Custom Stackdriver Monitoring metric—A custom monitoring metric used as the input value for Compute Engine instance group autoscaling.

Setup and Requirements

Qwiklabs setup

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.

Log in to Google Cloud Console

Using the Qwiklabs browser tab/window (preferably in Incognito mode) or the separate browser you are using for the Qwiklabs session, copy the Username from the Connection Details panel and click the orange "Open Google Console" button. Paste in the Username, and then the Password as prompted:

Accept the terms and conditions.

Since this is a temporary account, which you will only have access to for this one lab:

  • Do not add recovery options
  • Do not sign up for free trials

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.

Run the following command in the cloud shell to confirm that you are authenticated:

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

Create a Stackdriver account

To use Stackdriver, your project must be in a Stackdriver account. The following steps create a new account that has a free trial of Stackdriver Premium service.

In the Google Cloud Platform Console, click on Products and Services to see the left menu. In the Stackdriver section, click on Monitoring.

Click "Log in with Google" and use the Qwiklabs credentials to log in.

Your Qwiklabs project should display as your project. Select Create account.

On the following pages:

  • Add Google Cloud Platform projects to monitor - click Continue
  • Monitor AWS accounts - Skip AWS Setup
  • Install the Stackdriver Agents - click Continue.
  • Get Reports by Email - select No reports then Continue.

You see the message "Gathering information..." at the top of the page. Click Launch monitoring when finishes:

A pop-up message will inform you that your account is in a free trial. Click Continue with the trial.

You see your Stackdriver account dashboard. Close the "Welcome to Stackdriver" banner if you don't need it.

Join Qwiklabs to Read the Rest of this Lab...and More!

  • Get temporary access to the Google Cloud Console.
  • Nearly 100 labs from beginner to advanced levels.
  • Bite-sized so you can learn at your own pace.
Join to Start This Lab