Skip to main content

Using Stackdriver to Monitor Instance Groups

Hands-On Lab

 

Photo of Broadus Palmer

Broadus Palmer

Google Cloud Training Architect

Length

01:15:00

Difficulty

Intermediate

In a production environment, there will be times where you need to scale. To stay on top of issues, it would be best for you to monitor your infrastructure and this is where Stackdriver comes to play. In this hands-on lab, we will learn how to create a customer-facing instance group and learn how to monitor and scale when traffic causes heavy load.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Stackdriver to Monitor Instance Groups

Introduction

In a production environment, there will be times where you need to scale. To stay on top of issues, it would be best for you to monitor your infrastructure and this is where Stackdriver comes to play. In this hands-on lab, we will learn how to create a customer-facing instance group and learn how to monitor and scale when traffic causes heavy load.

Solution

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says “Open Link in Incognito Window”). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.
On the Welcome to your new account screen, review the text, and click Accept. In the “Welcome L.A.!” pop-up once you’re signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Create a Stackdriver Workspace for the Project

  1. From the main console navigation, head over to Stackdriver and click on Monitoring.
  2. Click on Add to add this project to a new workspace within Stackdriver.

Create a Firewall Rule

  1. From the main console navigation, go to VPC Network > Firewall Rules.
  2. Choose Create Firewall Rule and name the rule default-allow-http.
  3. In the Direction of Traffic section, select the ingress option.
  4. In the Action on Match section, select Allow.
  5. For Target tags, type in http-allow.
  6. For Source IP range, type in 0.0.0.0/0.
  7. For Protocols and ports, we will select TCP and type in 80.
  8. Click Create.

Create an Instance Template

  1. From the main console navigation, head over to Compute Engine.

  2. Click on Instance Template.

  3. Click on Create Instance Template.

    • Name your template stackdriver-group.
    • Machine Type: f1-micro
    • Firewall: Allow HTTP traffic
  4. Click on Management, security, disks, networking, sole tenancy.

  5. Click on the Networking tab, head down to Network tags and enter in the tag http-allow.

  6. On the Management tab, head down to Startup Script.

  7. Paste this script into the Startup script section:

    #!/bin/bash
    apt-get install apache2 -y
    apt-get install git -y
    mkdir /stackd
    git clone https://github.com/BPalmerLA/StackdriverInstanceGroups.git /stackd
    cd /stackd
    bash -x ./stackd.sh
  8. Click on Create.

Create an Instance Group

  1. While in the Compute Engine dashboard, click the checkbox next to stackdriver-group, click the hamburger (or three dots) menu in the top bar, and then click Create instance group.
    • Name: stackdriver-instance-group
    • Instance template: stackdriver-group
    • Turn Autoscaling off
    • Click Create
  2. After the Instance Group has created, click on the instance group name.
  3. Verify Apache is working correctly by clicking on the External IP for the instance group. The default Apache page should load in a new tab.
  4. Back on our instance group page, click Edit group (the pencil icon in the top bar).
  5. Turn Autoscaling on.
  6. For Autoscaling Policy, we will choose Stackdriver Monitoring Metric.
  7. For Metric export scope, choose Time Series per instance.
  8. For Metric identifier, enter agent.googleapis.com/apache/request_count.
  9. For Utilization Target, choose 50.
  10. Minimum number of instances should be 1 and Maximum number of instances should be 3.
  11. Click Save.

Create the Load Balancer

  1. From the main console navigation, head over to Network Service > Load Balancing.
  2. Click Create Load Balancer and choose Start Configuration on HTTP(S) Load Balancing.
  3. Choose From Internet to my VMs and click Continue.
  4. Name the load balancer stackdriver-lb.
  5. Choose Backend Configuration > Create Backend Services > Backend Service > Create a Backend Service.
    • Name: stackdriver-backend
    • On Instance Group, select stackdriver-instance-group.
    • On Balancing Mode choose Rate.
    • For Maximum RPS, type 50.
    • Select Done.
  6. Select Create a Health Check, name the health check stackdriver-hc, and choose TCP port 80 for Protocol.
  7. Click Save and continue.
  8. Click Create.
  9. For Frontend Config, name your config stackdriver-frontend and leave everything as default, then click Done.
  10. Click Create.

Now, configure the health check for the instance group with the health check:

  1. From the main console navigation, head over to Compute Engine > Instance Groups.
  2. Click on the name of the instance group to access it.
  3. Click Edit Instance Group.
  4. At the bottom at Autohealing, choose the health check that was created for the load balancer called stackdriver-hc.
  5. Click Save.

Verify the load balancer is working properly:

  1. Navigate to Network Services and click Load balancing.
  2. Once the load balancer is ready, click the name of the load balancer to access it.
  3. Copy the IP Port to your clipboard, open a new tab in your browser, and navigate to this address to access the Apache default page.

Set up Alerting Policy for Stackdriver Monitoring

  1. In the Monitoring section, navigate to Alerting and click on Create a policy.
    • Click Add a condition
    • In the Find resource type and metric field, type Apache and click on Resource.
    • Alert at 50
    • Click Save
    • Optionally, click Email in the Notifications section and provide your email address
    • Name the policy stackdriver
    • Click Save
  2. From the main console navigation, head over to Compute Engine > VM Instances.
  3. Click SSH for the instance.
  4. Once logged in to the instance, we are going to use gcloud config set project $PROJECT_ID, where the $PROJECT_ID will be your Project ID for your project.
  5. Then, we will stress the Apache server by using ab -n 1000000 -c 1000 http://InstanceIPAddress/Path, where the InstanceIPAddress is the IP address we used to test the load balancer.
  6. Your instance group should scale to three instances.

Conclusion

Congratulations — you've completed this hands-on lab!