准备工作
- 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
- 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
- 在屏幕左上角,点击开始实验即可开始
Create a GCS bucket
/ 25
Create an instance template
/ 25
Create an instance group
/ 25
Configure autoscaling for the instance group
/ 25
Using autoscaling in a Compute Engine Managed Instance Group (MIG) ensures your application is reliable and cost-efficient by automatically matching its capacity to user demand.
In this lab you implement a metrics-based autoscaling Managed Instance Group, then use Monitoring to confirm that your application is self-managing its capacity.
In this lab, you will learn how to perform the following tasks:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud, in Google Cloud, refer to the gcloud CLI overview guide.
The instance template needs a way to install the necessary application and scripts on every new VM it creates. Cloud Storage acts as a stable, accessible source for the startup script (startup.sh), which will execute when a new instance launches.
You will make a copy of the startup script and application files for a sample application used by this lab that pushes a pattern of data into a custom metric that you can then use to configure as the metric that controls the autoscaling behavior.
In the Cloud console, from the Navigation menu select Cloud Storage > Buckets, then click Create.
Give your bucket a unique name, but don't use a name you might want to use in another project. For details about how to name a bucket, see the bucket naming guidelines. You can use your Project ID + "bucket" for the bucket name. This bucket will be referenced as YOUR_BUCKET throughout the lab.
Accept the default values then click Create.
Click Confirm for Public access will be prevented pop-up if prompted.
When the bucket is created, the Bucket details page opens.
Test completed task
Click Check my progress to verify your performed task. If you have successfully created a Cloud Storage bucket, you will see an assessment score.
<YOUR BUCKET> with the name of the bucket you just made:Startup.sh - A shell script that installs the necessary components to each Compute Engine instance as the instance is added to the managed instance group.writeToCustomMetric.js - A Node.js snippet that creates a custom monitoring metric whose value triggers scaling. In a production environment, this would be your actual application code, and the value it reports (appdemo_queue_depth_01) would be a critical business metric, such as: the number of pending messages in a queue, the number of open database connections, or the length of a batch processing backlog.Config.json - A Node.js config file that specifies the values for the custom monitoring metric and used in writeToCustomMetric.js.Package.json - A Node.js package file that specifies standard installation and dependencies for writeToCustomMetric.js.writeToCustomMetric.sh - A shell script that continuously runs the writeToCustomMetric.js program on each Compute Engine instance.This is the blueprint for all VMs in the group. Autoscaling works by destroying and creating instances, and it must know exactly how to rebuild them. The template defines the machine type, OS image, and, most importantly, the metadata that points to the startup.sh script in your Cloud Storage bucket.
In the Cloud Console, click Navigation menu > Compute Engine > Instance templates.
Click Create Instance Template at the top of the page.
Name the instance template autoscaling-instance01.
Set Location as Global.
Scroll down, click Advanced 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:
| Key | Value |
|---|---|
| startup-script-url | gs://[YOUR_BUCKET_NAME]/startup.sh |
| gcs-bucket | gs://[YOUR_BUCKET_NAME] |
Test completed task
Click Check my progress to verify your performed task. If you have successfully created an instance template, you will see an assessment score.
The instance group acts as the container and controller for the fleet of VMs. The Autoscaling mode is set to Off initially to control the environment and ensure the first instance is successfully running the custom metric script before enabling autoscaling.
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.
For Location, select Single Zone and use
Set Autoscaling mode to Off: do not autoscale.
You'll edit the autoscaling setting after the instance group has been created. Leave the other settings at their default values.
Autoscaling is turned off. The number of instances in the group won't change automatically. The autoscaling configuration is preserved. warning next to your instance group. Test completed task
Click Check my progress to verify your performed task. If you have successfully created an instance group, you will see an assessment score.
Wait to see the green check mark next to the new instance group you just created. It might take the startup script several minutes to complete installation and begin reporting values.
You must confirm that the single test instance is successfully executing the writeToCustomMetric.js script and that the data is flowing into Cloud Monitoring. If the metric data isn't being reported, the autoscaler has nothing to react to. Checking the VM logs for nodeapp: available confirms this data pipeline is operational.
Click Refresh if it seems to be taking more than a few minutes.
Still in the Instance Groups window, click the name of the autoscaling-instance-group-1 to display the instances that are running in the group.
Scroll down and click the instance name. Because autoscaling has not started additional instances, there is just a single instance running.
In the Details tab, in the Logs section, click the Logging link to view the logs for the VM instance.
Wait a minute or 2 to let some data accumulate. Enable the Show query toggle, you will see resource.type and resource.labels.instance_id in the Query preview box.
"nodeapp" as line 3, so the code looks similar to this:If the Node.js script is being executed on the Compute Engine instance, a request is sent to the API, and log entries that say nodeapp: available is displayed.
After you've verified that the custom metric is successfully reporting data from the first instance, the instance group can be configured to autoscale based on the value of the custom metric.
In the Cloud console, go to Compute Engine > Instance groups.
Click the autoscaling-instance-group-1 group.
Click Edit.
Under Autoscaling set Autoscaling mode to On: add and remove instances to the group.
Set Minimum number of instances: 1 and Maximum number of instances: 3
This defines the lower and upper bounds of your cost and performance guarantee. You pay for at least 1 instance, and you're guaranteed to never scale beyond 3 instances, which manages your cost exposure.
Under Autoscaling signals click Add a signal to edit metric. Set the following fields, leave all others at the default value.
Cloud Monitoring metric. Click Configure.150
When custom monitoring metric values are higher or lower than the Target value, the autoscaler scales the managed instance group, increasing or decreasing the number of instances. The target value can be any double value, but for this lab, the value 150 was chosen because it matches the values being reported by the custom monitoring metric.
Gauge. Click Select.GAUGE: Scales based on the average current value of the metric across all instances. (e.g., "The average queue depth is 200, which is over my target of 150, so I need another machine.") This is appropriate for metrics like queue depth or CPU utilization. (By contrast, setting Target mode to DELTA_PER_MINUTE or DELTA_PER_SECOND autoscales based on the observed rate of change rather than an average value.)
Click Save.
Test completed task
Click Check my progress to verify your performed task. If you have successfully configured autoscaling for the instance group, you will see an assessment score.
The Node.js script varies the custom metric values it reports from each instance over time. As the value of the metric goes up, the instance group scales up by adding VMs. If the value goes down, the instance group detects this and scales down by removing VMs. The script emulates a real-world metric whose value might similarly fluctuate up and down.
Next, watch how the instance group is scaling in response to the metric by clicking the Monitoring tab to view the Autoscaled size graph.
builtin-igm instance group in the list.Since this group had a head start, you can see the autoscaling details about the instance group in the autoscaling graph. The autoscaler will take about five minutes to correctly recognize the custom metric and it can take up to 10-15 minutes for the script to generate sufficient data to trigger the autoscaling behavior.
Mouse over the graphs to see more details.
You can switch back to the instance group that you created to see how it's doing (there may not be enough time left in the lab to see any autoscaling on your instance group).
For the remainder of the time in your lab, you can watch the autoscaling graph move up and down as instances are added and removed.
Read through this autoscaling example to see how capacity and number of autoscaled instances can work in a larger environment.
The number of instances depicted in the top graph changes as a result of the varying aggregate level of the custom metric property values reported in the lower graph. There is a slight delay of up to five minutes after each instance starts up before that instance begins to report its custom metric values. While your autoscaling starts up, read through this graph to understand what will be happening:
The script starts by generating high values for approximately 15 minutes in order to trigger scale-up behavior.
Congratulations! In this lab, you created a Compute Engine managed instance group that autoscales based on the value of a custom Cloud Monitoring metric. You also learned how to use the Cloud Console to visualize the custom metric and instance group size.
...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.
Manual Last Updated February 25, 2026
Lab Last Tested February 25, 2026
Copyright 2026 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
此内容目前不可用
一旦可用,我们会通过电子邮件告知您
太好了!
一旦可用,我们会通过电子邮件告知您
一次一个实验
确认结束所有现有实验并开始此实验