Autoscaling an Instance Group with Custom Cloud Monitoring Metrics
Create bucket and download application files
- Create a GCS bucket
export PROJECT_ID=$(gcloud info --format='value(config.project)') gsutil mb gs://$PROJECT_ID
- Copy application files from Google managed project bucket
gsutil cp -r gs://spls/gsp087/* gs://$PROJECT_ID
Creating an instance template
- In the Cloud Console, go to Navigation menu > Compute Engine > Instance templates.
- Click Create Instance Template at the top of the page.
- Name the instance templateÂ
autoscaling-instance01
. - Scroll down, click NETWORKING, DISKS, SECURITY, MANAGEMENT, SOLE-TENANCY to expand the input options.
- In the Metadata section of the Management tab, enter these metadata keys and values, clicking the + Add item button to add each one. Remember to substitute your bucket name for theÂ
[YOUR_BUCKET_NAME]
 placeholder:
startup-script-url: gs://[YOUR_BUCKET_NAME]/startup.sh
gcs-bucket: gs://[YOUR_BUCKET_NAME] - Click Create.
Creating the instance group
- In the left pane, click Instance groups.
- Click Create instance group at the top of the page.
Name: autoscaling-instance-group-1.
For Instance template: select the instance template you just created.
Set Autoscaling mode to Off: do not auto scale. - Click Create.
- Verify application is running by checking the logs in Cloud Logging
Configure autoscaling for the instance group
- In the Cloud Console, go to Compute Engine > Instance groups.
- Click theÂ
autoscaling-instance-group-1
 group. - Under Autoscaling click Configure.
- Set Autoscaling mode to On: add and remove instances to the group.
- Under Autoscaling metrics click ADD METRIC to edit metric. Set the following fields, leave all others at the default value.
- Metric type:Â
Cloud Monitoring metric
- Metric export scope:Â
Time series per instance
- Metric identifier:Â
custom.googleapis.com/appdemo_queue_depth_01
- Utilization target:Â
150
- Utilization target type:Â
Gauge
- Minimum number of instances:Â
1
- Maximum number of instances:Â
3
- Metric type:Â
- Click Save
Watching the instance group perform autoscaling
- In the left pane, click Instance groups.
- Click theÂ
builtin-igm
 instance group in the list. - Click the Monitoring tab.
- Enable Auto Refresh.
Tag:Google Cloud