Scale based on Monitoring metrics Stay organized with collections Save and categorize content based on your preferences.
Autoscaling based onCloud Monitoringmetrics lets you adjust the capacity needed according to measurements from yourapp. When you autoscale a MIG based on a metric, the autoscalercreates VMs when the metric value increases and deletes VMs when the valuedecreases.
For example, you can define how many VMs you need per user count,latency, or the number of messages in a Pub/Sub subscription.You can use either thebuilt-in metricsprovided by the Monitoring service, or thecustom metrics that you export from yourapplication.
This document describes how to autoscale amanaged instance group (MIG) based on Monitoring metrics.
You can also autoscale a MIG based onCPU utilization,load balancing serving capacity,orschedules.
Before you begin
- Read about autoscalerfundamentals.
- Read aboutMonitoring metrics concepts used in an autoscaling configuration.
- If you want to autoscale based on anOps Agent metric, install theOps Agent.
- If you haven't already, set upauthentication. Authentication verifies your identity for access to Google Cloud services and APIs. To run code or samples from a local development environment, you can authenticate to Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.- Set a default region and zone.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI. After installation,initialize the Google Cloud CLI by running the following command:
gcloudinit
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
Note: If you installed the gcloud CLI previously, make sure you have the latest version by runninggcloud components update.For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.
Limitations
Scaling based on Monitoring metrics is restricted by thelimitations for all autoscalers aswell as by the following limitations:
- You can configure autoscaling based on up to 5 Monitoringmetrics per MIG.
- You can autoscale based on metrics that have
INT64orDOUBLEvalue typesonly. Othervalue types arenot supported. - You cannot configure the same metric more than once in an autoscaling policy.
Configure autoscaling based on Monitoring metrics
You can use a Monitoring metric value for autoscaling intwo different ways:
Utilization target: If you want the autoscaler to maintain a metricat a specific value,configure autilization target.The autoscaler creates VMs when the metric value is above the target anddeletes VMs when the metric value is below the target. This is useful formetrics like network traffic, memory or disk usage, or average latency of yourapplication. The following diagram shows how an autoscaler adds and removesVMs in response to a metric value to maintain a utilization target.
Single instance assignment: If you want to autoscale based on howmuch work is available to assign to each VM,configure asingle instance assignment.The single instance assignment that you specify represents how much work youexpect each VM to handle. The autoscaler divides the metric value by thesingle instance assignment value to calculate how many VMsare needed. For example, if the metric is equal to 100 and the single instanceassignment is 5, then the autoscaler creates 20 VMs in the MIG.This is useful for metricsthat reflect the amount of work like Pub/Sub queue length orbatch jobs count. Single instance assignment does not apply to metrics thatcome from each VM. The following diagram shows theproportional relationship between the metric value and the number of VMs whenscaling with single instance assignment.
Autoscale to maintain a metric at a target value
When you want to maintain a metric at a target value, specify autilization target. The autoscaler creates VMs when the metric value is abovethe target and deletes VMs when the metric value is below the target.
If the metric comes from each VM in your MIG,then the autoscaler takes the average metric value across all VMs in the MIGand compares it with the utilization target. For example, if youwant to autoscale using the
tcp_connectionsmetricthat gives the number of TCP connections on a VM, then the autoscalertakes an average number of TCP connections across all VMs in the MIG to comparewith the target. When you use such metrics that originate from a VM, the MIGcannot scale in to zero because the autoscaler requires at least one VM topublish a metric value.If the metric applies to the whole MIG and does not come from theVMs in your MIG, then the autoscaler compares the metric value with theutilization target. For example, you can use acustom metric that measures the latency of yourapplication. When you use such metrics that apply to the whole MIG, the MIGcan scale in to zero, provided that you set the minimum number of instances to
0and you don't specify other signals that require data from individual VMs.
When your metric has multiple values, apply a filter to autoscale using anindividual value from the metric. For more details about metric filters andother fields that you can use in your configuration, seeMonitoring metrics concepts.
Permissions required for this task
To perform this task, you must have the followingpermissions:
compute.autoscalers.createon the projectcompute.instanceGroupManagers.useon the project
Console
In the Google Cloud console, go to theInstance groups page.
If you don't have a managed instance group,create one.Otherwise, click the name of a MIG from the list to openthat instance group page.
ClickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. In theResource and metric pane that opens, dothe following:
- ClickSelect a metric.
- Select the metric that you want to use for autoscaling. You canfilter the metrics based on any keywords—for example, memory,bytes, disk.
- ClickApply. The pane displays a chart that shows the datafrom the selected metric.
If you want to use specific data from the metric, add a filter basedon labels as follows:
- In theFilters section, clickAdd a filter.
- Select aLabel and enter aValue.
- ClickDone. The chart refreshes to show you the filtered valueof the metric.
project_id=PROJECT_IDis addedby default because you can use the metric values from the resourcesin the current project only. If you selected a metric that comesfrom each VM (gce_instance), then an additional filterinstance_group=MIG_NAMEis also added because youcan autoscale based on the metric values from the VMs in the currentMIG only.To view the aggregated value of metric that will be used to autoscaleyour MIG, toggle theShow aggregation in chart button. The chartrefreshes to show the aggregated value.
In theMetric target options for autoscaling section, selectUtilization target.
Provide the following:
- Utilization target: Specify the value that the autoscaler mustmaintain. This must be a positive number. For example, both24.5 and 100 are acceptable values.
- Utilization target type: Select a target type that correspondsto the metric's kind of measurement. For accurate comparisons, ifthe utilization target is measured per seconds, then useDelta / second as the target type. Likewise, useDelta / min for a utilization target measured per minutes.
- Gauge: The autoscaler calculates the average value of thedata collected in the last couple of minutes and compares that tothe utilization target.
- Delta / min: The autoscaler calculates the average rate ofgrowth per minute and compares that to the utilization target.
- Delta / second: The autoscaler calculates the average rate ofgrowth per second and compares that to the utilization target.
When you've finished configuring the metric, clickSelect.
- ClickSelect a metric.
Review the metric details and clickDone.
To complete the configuration, clickSave.
gcloud
To configure autoscaling based on Monitoring metrics, use theset-autoscaling command.
set-autoscaling commandoverwrites the existing autoscaler to the new specifications.Use the following command to autoscale based on a Monitoringmetric with a utilization target.
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=METRIC_URL \ --stackdriver-metric-utilization-target=TARGET_VALUE \ --stackdriver-metric-utilization-target-type=TARGET_TYPE
If your metric has multiple values and you want to use an individual valuefor autoscaling, then use the--stackdriver-metric-filter flag as given inthe following command.
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=METRIC_URL \ --stackdriver-metric-utilization-target=TARGET_VALUE \ --stackdriver-metric-utilization-target-type=TARGET_TYPE \--stackdriver-metric-filter="METRIC_FILTER"
Replace the following:
MIG_NAME: the MIG in which you want to add anautoscaler.MAX_INSTANCES: the maximum number of VMs that theMIG can have.MIN_INSTANCES: the minimum number of VMs that theMIG needs to have.METRIC_URL: a protocol-free URL of aMonitoring metric. For example,compute.googleapis.com/instance/uptime. If you use a custom metric,it must meet thecustom metric requirements.TARGET_VALUE: the metric value that the autoscalerattempts to maintain.TARGET_TYPE: the value type for the metric.gaugethe autoscaler computes the average value of the datacollected in the last couple of minutes and compares that to theutilization target.delta-per-minutethe autoscaler calculates the average rate ofgrowth per minute and compares that to the utilization target.delta-per-secondthe autoscaler calculates the average rate ofgrowth per second and compares that to the utilization target.For accurate comparisons, if you set the utilization target in seconds,usedelta-per-secondas the target type. Likewise, usedelta-per-minutefor a utilization target in minutes.
METRIC_FILTER: apply a filter to use an individualvalue from a metric having multiple values and to specify themonitored resource type. If you use a metric that comes from each VM, youdon't have to specify the monitored resource type becausegce_instanceisused as default. For other metrics, useresource.typein the filterexpression to specify the monitored resource. To learn more about metricfilter, seeMonitoring metrics concepts.
To see a full list of available commands and flags for thegcloud CLI, see thegcloud reference.
REST
Note: Although autoscaling is a feature ofmanaged instance groups, it is a separate API resource. Keep that in mindwhen you construct API requests for autoscaling.To configure autoscaling based on Monitoring metrics for azonal MIG, use theautoscalersresource or, for a regional MIG, use theregionAutoscalersresource.
Make the following call to autoscale a zonal MIGbased on a Monitoring metric with a utilization target.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{ "name": "AUTOSCALER_NAME", "target": "zones/ZONE/instanceGroupManagers/MIG_NAME", "autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "metric": "METRIC_URL", "utilizationTarget":TARGET_VALUE, "utilizationTargetType":TARGET_TYPE } ], }}If your metric has multiple values and you want to use an individual valuefor autoscaling, then use thefilter parameter as given in thefollowing API call.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{ "name": "AUTOSCALER_NAME", "target": "zones/ZONE/instanceGroupManagers/MIG_NAME", "autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "metric": "METRIC_URL", "utilizationTarget":TARGET_VALUE, "utilizationTargetType":TARGET_TYPE,"filter": "METRIC_FILTER" } ], }}Replace the following:
PROJECT_ID: yourproject ID.ZONE: the zone where the MIG is located.AUTOSCALER_NAME: the name of the autoscaler.MIG_NAME: the MIG in which you want to add anautoscaler.MAX_INSTANCES: the maximum number of VMs that theMIG can have.MIN_INSTANCES: the minimum number of VMs that theMIG needs to have.METRIC_URL: a protocol-free URL of aMonitoring metric. For example,compute.googleapis.com/instance/uptime. If you use a custom metric,it must meet thecustom metric requirements.TARGET_VALUE: the metric value that the autoscalerattempts to maintain.TARGET_TYPE: the value type for the metric.GAUGE: The autoscaler computes the average value of the datacollected in the last couple of minutes and compares that to theutilization target.DELTA_PER_MINUTEThe autoscaler calculates the average rate ofgrowth per minute and compares that to the utilization target.DELTA_PER_SECONDThe autoscaler calculates the average rate ofgrowth per second and compares that to the utilization target. Foraccurate comparisons, if you set the utilization target in seconds,useDELTA_PER_SECONDas the target type. Likewise, useDELTA_PER_MINUTEfor a utilization target in minutes.
METRIC_FILTER: apply a filter to use an individualvalue from a metric with multiple values and to specify themonitored resource type. If you use a metric that comes from each VM, youdon't have to specify the monitored resource type becausegce_instanceisused as default. For other metrics, you must specify the monitoredresource usingresource.typeselector. To learn more about metricfilter, seeMonitoring metrics concepts.
Autoscale based on work available for each VM in a MIG
When you want to autoscale based on the quantity of work that is availablefor each VM in a MIG, specify a single instance assignment.The value of the single instance assignment that you set indicates howmuch work you expect each VM to handle. The autoscaler divides the metric valueby the single instance assignment value to calculate how many VMs are needed.
A metric value of0 indicates that there is no work for your MIG tocomplete. If your MIG's minimum number of instances is set to0 and yourmetric value drops to0, then the MIG scales in to0 until the metric valueincreases.
When your metric has multiple values, apply a filter to autoscale using anindividual value from the metric. For more details about metric filters andother fields that you can use in your configuration, seeMonitoring metrics concepts.
Permissions required for this task
To perform this task, you must have the followingpermissions:
compute.autoscalers.createon the projectcompute.instanceGroupManagers.useon the project
Console
In the Google Cloud console, go to theInstance groups page.
If you don't have a managed instance group,create one.Otherwise, click the name of a MIG from the list to open that instancegroup page.
ClickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. In theResource and metric pane that opens, dothe following:
- ClickSelect a metric.
- Select the metric that you want to use for autoscaling. You canfilter the metrics based on any keywords—for example, memory,bytes, disk.
- ClickApply. The pane displays a chart that shows the datafrom the selected metric.
If you want to use specific data from the metric, add a filter basedon labels as follows:
- In theFilters section, clickAdd a filter.
- Select aLabel and enter aValue.
- ClickDone. The chart refreshes to show you the filtered metricvalue.
project_id=PROJECT_IDis added bydefault because you can use the metric values from the resources inthe current project only.To view the aggregated value of metric that will be used to autoscaleyour MIG, toggle theShow aggregation in chart button. The chartrefreshes to show the aggregated value.
In theMetric target options for autoscaling section, selectSingle VM instance assignment.
Provide aSingle instance assignment value that represents the amountof work to assign to each VM in the MIG.
When you've finished configuring the metric, clickSelect.
- ClickSelect a metric.
Review the metric details and clickDone.
To complete the configuration, clickSave.
gcloud
To configure autoscaling based on Monitoring metrics, use theset-autoscaling command.
set-autoscalingcommand overwrites the existing autoscaler to the new specifications.In the command, specify the--stackdriver-metric-single-instance-assignmentflag to indicate the amount of work that you expect each VM in the groupto handle.
The following command creates an autoscaler based on work assignment foreach VM.
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=METRIC_URL \ --stackdriver-metric-filter="METRIC_FILTER" \ --stackdriver-metric-single-instance-assignment=INSTANCE_ASSIGNMENT
Replace the following:
MIG_NAME: the name of the MIG where you want toadd an autoscaler.MAX_INSTANCES: the maximum number of VMs that theMIG can have.MIN_INSTANCES: the minimum number of VMs that theMIG needs to have.METRIC_URL: a protocol-free URL of aMonitoring metric. For example,compute.googleapis.com/instance_group/size. If you use a custom metric,it must meet thecustom metric requirements.METRIC_FILTER: apply a filter to use an individualvalue from a metric with multiple values and to specify themonitored resource type. To learn more about metric filter, seeMonitoring metrics concepts.INSTANCE_ASSIGNMENT: the amount of work to assignto each VM instance inthe MIG.
REST
Note: Although autoscaling is a feature of managed instance groups,autoscalersare a separate API resource. Keep that in mind when you construct APIrequests for autoscaling.To configure autoscaling based on Monitoring metrics for azonal MIG, use theautoscalersresource or, for a regional MIG, use theregionAutoscalersresource.
Use thesingleInstanceAssignment parameter to specify the amount of workthat you expect each VM to handle.
For example, make the following call to create an autoscaler that scales azonal MIG based on the instance assignment.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{ "name": "AUTOSCALER_NAME", "target": "zones/ZONE/instanceGroupManagers/MIG_NAME", "autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "metric": "METRIC_URL", "filter": "METRIC_FILTER", "singleInstanceAssignment":INSTANCE_ASSIGNMENT } ], }}Replace the following:
PROJECT_ID: yourproject ID.ZONE: the zone where the MIG is located.AUTOSCALER_NAME: the name of the autoscaler.MIG_NAME: the name of the MIG where you want toadd an autoscaler.MAX_INSTANCES: the maximum number of VMs that theMIG can have.MIN_INSTANCES: the minimum number of VMs that theMIG needs to have.METRIC_URL: a protocol-free URL of aMonitoring metric. For example,compute.googleapis.com/instance_group/size. If you use a custom metric,it must meet thecustom metric requirements.METRIC_FILTER: apply a filter to use an individualvalue from a metric with multiple values and to specify themonitored resource type. To learn more about metric filter, seeMonitoring metrics concepts.INSTANCE_ASSIGNMENT: the amount of work to assignto each VM instance in the MIG.
Examples for autoscaling based on metrics
This section provides some examples of metrics used for autoscaling. For acomplete list of metrics, seeGoogle Cloud metrics.
Autoscale based on a custom metric
There might be a situation when the metric providing a relevant signal does notrepresent a total amount of available work or another resource applicable to thegroup, but instead an average, a percentile, or someother statistical property. For this example, assume that you scale based on thegroup's average processing latency.
Assume the following setup:
- A zonal MIG named
our-instance-groupis assigned to perform aparticular task. The group is located in zoneus-central1-a. - You have aMonitoring custom metricthat exports a value that you would like to maintain at a particular level. Forthis example, assume the metric represents the average latency of processingqueries assigned to the group.
- The custom metric is named:
custom.googleapis.com/example_average_latency. - The custom metric has a label with a key named
group_nameand valueequal to the MIG's name,our-instance-group. - The custom metric exports data for theglobal monitored resource,that is, it is not associated with any specific VM.
- The custom metric is named:
You have determined that when the metric value is above some specific value,you need to add more VMs to the group to handle the load, while when itis below that value, you can free up some resources. You want the autoscaler togradually add or remove VMs at a rate that is proportional to how much themetric is above or below the target. For this example, assume that you havedetermined your target value to be250 delta/sec.
You can configure autoscaling for the group using a utilizationtarget of250, which represents the rate of change of the metric value thatthe autoscaler will attempt to maintain:
Console
In the Google Cloud console, go to theInstance groups page.
Click the name of your MIG from the list to openthe instance group overview page.
ClickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. TheResource and metric pane opens.
- ClickSelect a metric.
- Choose your metric fromGlobal > Custom metrics.
- ClickApply.
- In theFilters section, do the following:
- ClickAdd a filter.
- In theLabel drop-down, select
group_name. - In theValue field, provide
our-instance-group. - ClickDone. The chart refreshes to show the filtered value fromthe metric.
- In theMetric target options for autoscaling section, do thefollowing:
- SelectUtilization target.
- Enter the utilization target value as 250.
- Set the utilization target type toDelta / sec.
- After you've configured the metric, clickSelect.
- ClickSelect a metric.
Review the metric details and then clickDone.
When you've finished, clickSave.
gcloud
gcloud compute instance-groups managed set-autoscaling our-instance-group \ --zone=us-central1-a \ --max-num-replicas=50 \ --min-num-replicas=0 \ --update-stackdriver-metric=custom.googleapis.com/example_average_latency \ --stackdriver-metric-filter="metric.labels.group_name = \"our-instance-group\" AND resource.type = \"global\"" \ --stackdriver-metric-utilization-target=250 \ --stackdriver-metric-utilization-target-type=delta-per-second
REST
POST https://compute.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/autoscalers{"name": "our-instance-group-autoscaler","target": "https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instanceGroupManagers/our-instance-group","autoscalingPolicy": { "maxNumReplicas": 50, "minNumReplicas": 0, "customMetricUtilizations": [ { "filter":"metric.labels.group_name=\"our-instance-group\" AND resource.type = \"global\"", "utilizationTargetType": "delta-per-second", "utilizationTarget": 250.0, "metric":"custom.googleapis.com/example_average_latency" } ]}}Autoscale based on unacknowledged messages in Pub/Sub
To configure autoscaling based on unacknowledged messages in aPub/Subsubscription, use thesubscription/num_undelivered_messages metricprovided bypubsuband filter by thesubscription ID.
Thesubscription/num_undelivered_messages metric exports the total number ofmessages in the subscription, including messages that are beingprocessed but that are not yet acknowledged. Using a metric that does notinclude the messages being processed is not recommended because such a metriccan drop-down to 0 when there is still work being done, which promptsautoscaling to scale in and possibly interrupt the actual work.
If you don't have a subscription, you can create apull,push, orBigQuery subscriptionbefore you configure autoscaling.
Console
In the Google Cloud console, go to theInstance groups page.
Click the name of your MIG from the list to openthe instance group overview page.
ClickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Pub/Sub queue.
Select aTopic. If you want a new topic,clickCreate a topic tocreate one.
Select theSubscription based on which you want to autoscale yourMIG. If you want a new subscription, clickCreate a subscription tocreate one.
In theNumber of messages to assign to each VM field, specify thenumber of unacknowledged messages that you expect each VM to handle.
ClickDone.
When you've finished, clickSave.
gcloud
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=pubsub.googleapis.com/subscription/num_undelivered_messages \ --stackdriver-metric-filter="resource.type=\"pubsub_subscription\" AND resource.labels.subscription_id=\"SUBSCRIPTION_ID\"" \ --stackdriver-metric-single-instance-assignment=NUMBER_OF_MESSAGES_TO_ASSIGN_TO_EACH_VM
REST
To configure autoscaling for a zonal MIG, use theautoscalers resource or, fora regional MIG, use theregionAutoscalersresource.
For example, make the following call to create an autoscaler for a zonal MIG.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{"name": "AUTOSCALER_NAME","target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME","autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "singleInstanceAssignment":NUMBER_OF_MESSAGES_TO_ASSIGN_TO_EACH_VM, "filter":"resource.type = \"pubsub_subscription\" AND resource.labels.subscription_id=\"SUBSCRIPTION_ID\"", "metric":"pubsub.googleapis.com/subscription/num_undelivered_messages" } ]}}Autoscale based on incoming network traffic
To configure autoscaling based on the incoming network traffic to VMs in yourMIG, use theinstance/network/received_bytes_count metric provided bycompute.
Console
In the Google Cloud console, go to theInstance groups page.
Click the name of your MIG from the list to openthe instance group overview page.
ClickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. TheResource and metric pane opens.
- ClickSelect a metric.
- SelectVM Instance > Instance > Received bytes(
compute.googleapis.com/instance/network/received_bytes_count). - ClickApply.
- SelectVM Instance > Instance > Received bytes(
- In theMetric target options for autoscaling section, do thefollowing:
- Make sure thatUtilization target is selected.
- Enter a utilization target value.
- Set a utilization target type.
- After you've configured the metric, clickSelect.
- ClickSelect a metric.
Review the metric details and then clickDone.
When you've finished, clickSave.
gcloud
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=compute.googleapis.com/instance/network/received_bytes_count \ --stackdriver-metric-utilization-target=TARGET_VALUE \ --stackdriver-metric-utilization-target-type=TARGET_TYPE
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{"name": "AUTOSCALER_NAME","target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME","autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "utilizationTargetType": "TARGET_TYPE", "utilizationTarget":TARGET_VALUE, "metric":"compute.googleapis.com/instance/network/received_bytes_count" } ]}}Autoscale based on memory usage
To configure autoscaling based on the percent of used memory,specify thepercent_used metric provided by thememory Ops Agent metrics. Youshould filter themetric bystate to use only theusedmemory state. If you don't specify the filter, then the autoscaler takes thesum of memory usage by all memory states labeled asbuffered,cached,free,slab, andused.
memory/percent_used metric is anOps Agent metric.You must install theOps Agent before youconfigure autoscaling based on this metric.Console
In the Google Cloud console, go to theInstance groups page.
Click the name of your MIG from the list to openthe instance group overview page.
On the instance group overview page, clickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. TheResource and metric pane opens.
- ClickSelect a metric.
- SelectVM Instance > Memory > Memory utilization(
compute.googleapis.com/instance/memory/percent_used). - ClickApply.
- SelectVM Instance > Memory > Memory utilization(
- In theFilters section, do the following:
- ClickAdd a filter.
- In theLabel drop-down, choose thestate metric label.
- In theValue field, typeused.
- ClickDone.
- In theMetric target options for autoscaling section, do thefollowing:
- Make sure thatUtilization target is selected.
- Enter a utilization target value.
- Set the utilization target type toGauge.
- After you've configured the metric, clickSelect.
- ClickSelect a metric.
Review the metric details and then clickDone.
When you've finished, clickSave.
gcloud
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=agent.googleapis.com/memory/percent_used \ --stackdriver-metric-filter="metric.labels.state = \"used\"" --stackdriver-metric-utilization-target-type=gauge \ --stackdriver-metric-utilization-target=TARGET_VALUE \
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{"name": "AUTOSCALER_NAME","target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME","autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "filter": "metric.labels.state=\"used\"", "utilizationTargetType": "GAUGE", "utilizationTarget":TARGET_VALUE, "metric":"agent.googleapis.com/memory/percent_used" } ]}}Autoscale based on disk I/O
To configure autoscaling based on the total count of diskI/O operations, use theoperation_count metric provided by thedisk Ops Agent metrics.If you want to scalebased on read or write operations, filter the metric using thedirectionlabel. Similarly, to scale based on disk operations in a particular device, usethedevice label in the metric filter.
disk/operation_count metric is anOps Agent metric.You must install theOps Agent before youconfigure autoscaling based on this metric.Console
In the Google Cloud console, go to theInstance groups page.
Click the name of your MIG from the list to openthe instance group overview page.
On the instance group overview page, clickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. TheResource and metric pane opens.
- ClickSelect a metric.
- SelectVM instance > Disk > Disk operations(
agent.googleapis.com/disk/operation_count). - ClickApply.
- SelectVM instance > Disk > Disk operations(
- In theMetric target options for autoscaling section, do thefollowing:
- Make sure thatUtilization target is selected.
- Enter a utilization target value.
- Set a utilization target type.
- After you've configured the metric, clickSelect.
- ClickSelect a metric.
Review the metric details and then clickDone.
When you've finished, clickSave.
gcloud
gcloud compute instance-groups managed set-autoscalingMIG_NAME \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=agent.googleapis.com/disk/operation_count \ --stackdriver-metric-utilization-target=TARGET_VALUE \ --stackdriver-metric-utilization-target-type=TARGET_TYPE
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{"name": "AUTOSCALER_NAME","target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_NAME","autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "utilizationTargetType": "TARGET_TYPE", "utilizationTarget":TARGET_VALUE, "metric":"agent.googleapis.com/disk/operation_count" } ]}}Autoscale based on size of another MIG
You can autoscale a MIG based on the size of another MIG within the sameproject. For example, you can have multi-tier application with a frontend MIGthatautoscales based on a load balancerand a backend MIG that autoscales proportionally to the frontend. Use a singleinstance assignment to define how many backend VMs are needed for every frontendVM. If you need 1 backend VM for every 4 frontend VMs, then set the singleinstance assignment to 4 in the backend MIG.
To autoscale a MIG (MIG_1) based on the size ofanother MIG (MIG_2), use theinstance_group/size metricprovided bycompute.
Console
In the Google Cloud console, go to theInstance groups page.
Click the name of your MIG from the list to openthe instance group overview page.
On the instance group overview page, clickEdit.
- ClickGroup size & autoscaling to expand the section.
- If autoscaling configuration doesn't exist, clickConfigure autoscaling.
By default, the autoscaling configuration adds a signal based onCPU utilization. If you don't require this signal, you can edit itto change the signal type.
- In theAutoscaling signals section, if a signal for a Monitoringmetric exists, you can click to edit it, or clickAdd a signal to addone.
Set theSignal type toCloud Monitoring metric.
ClickConfigure. TheResource and metric pane opens.
- ClickSelect a metric
- SelectInstance Group > Instance_group > Instance group size(
compute.googleapis.com/instance_group/size). - ClickApply.
- SelectInstance Group > Instance_group > Instance group size(
- In theFilters section, do the following:
- ClickAdd a filter.
- In theLabel drop-down, choose theinstance_group_nameresource label.
- In theValue field, type the name of the MIG (MIG_2)based on which you want to autoscale your MIG.
- ClickDone.
- In theMetric target options for autoscaling section, do thefollowing:
- SelectSingle VM instance assignment.
- In the Single VM instance assignment field, enter
4, if youwant to keep 1 VM in your current MIG (MIG_1) for every4 VMs inMIG_2.
- After you've configured the metric, clickSelect.
- ClickSelect a metric
Review the metric details and then clickDone.
When you've finished, clickSave.
gcloud
gcloud compute instance-groups managed set-autoscalingMIG_1 \ --max-num-replicas=MAX_INSTANCES \ --min-num-replicas=MIN_INSTANCES \ --update-stackdriver-metric=compute.googleapis.com/instance_group/size \ --stackdriver-metric-filter="resource.type = \"instance_group\" AND resource.labels.location = \"ZONE|REGION\" AND resource.labels.instance_group_name = \"MIG_2\"" \ --stackdriver-metric-single-instance-assignment=4
REST
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/autoscalers{"name": "AUTOSCALER_NAME","target": "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers/MIG_1","autoscalingPolicy": { "maxNumReplicas":MAX_INSTANCES, "minNumReplicas":MIN_INSTANCES, "customMetricUtilizations": [ { "singleInstanceAssignment": 4, "filter":"resource.type = \"instance_group\" and resource.labels.location = \"ZONE|REGION\" AND resource.labels.instance_group_name = \"MIG_2\"", "metric":"compute.googleapis.com/instance_group/size" } ]}}Monitoring metrics concepts
This section provides a brief description of theMonitoring metrics concepts that you need to know whileconfiguring autoscaling based on Monitoring metrics.
Metric identifier orMetric URL: The metric name in the form of aprotocol-free URL. You can find URLs for built-in metrics URL from themetric list.
For example, the URL of a Pub/Sub metric that gives the numberof unacknowledgedmessages is
pubsub.googleapis.com/subscription/num_undelivered_messages.Monitored resource type: The source of metric value. You can findthe monitored resource type of a metric from themetric list.
For example, the monitored resource type of the
pubsub.googleapis.com/subscription/num_undelivered_messagesmetric ispubsub_subscription. For moredetails about each monitored resource type, seeMonitored resource types.Metric filter: When your metric has multiple values, a filter enables theautoscaler to identify a specific metric value from the set of possible metricvalues. Use the labels defined on ametricand amonitored resource type tofilter the values. If you want to explore your metric values with differentfilters, you can try them in themetrics explorer.
For example, the following screenshot shows
pubsub.googleapis.com/subscription/num_undelivered_messagesmetric thatgives the number of unacknowledged messages in all available subscriptions.Each line on the chart indicates a subscription.Without a filter, the autoscaler takes the sum of metric values from allsubscriptions. To autoscale based on asingle subscription, apply a filter on the
subscription_idlabel defined forthepubsub_subscription. The following screenshot shows a singlesubscription after applying the filter.
Metric filtering requirements
When you use a metric that has multiple values (categorized usinglabels),you can apply a filter to autoscalebased on specific values from the metric.If the filter returns multiple values, then the values are added together.For best results, create a filter that is specific enough to return a singlevalue.
Autoscaler filtering for metrics is compatible with theMonitoring filter syntax. Thefilter must meet the following requirements:
- You must wrap the value of a filter in double quotes.
- You must use the direct equality comparison operator (
=). You must use the
ANDoperator to join different filter criteria.For example:
--stackdriver-metric-filter="resource.type=\"pubsub_subscription\" ANDresource.labels.subscription_id=\"our-subscription\"".You must use direct values. You cannot use wildcards or functions in thefilter.
For example, you cannot use
resource.labels.zone = starts_with("us-").You cannot useresource metadata labelsthat store metadata about a monitored resource.
For a full reference of metric labels and monitored resource labels that you canfilter on, seemetrics listandmonitored resources list.
Custom metric requirements
To use custom metrics, you must first create a custom metric that is associatedwith one of themonitored-resource types.
A custom metric used for autoscaling must have the following properties:
- If the autoscaling configuration uses data from each VM in the group, set upinstances in your MIG so that each VM exports the custom metric. The exportedvalues from each VM must be associated with a
gce_instancemonitored resource, which contains the following labels:zonewith the name of the zone the instance is in.instance_idwith the value of unique numerical ID assigned to theVM.
- The metric must export data at least every 60 seconds. If you export datamore often than every 60 seconds, the autoscaler can respondto load changes more quickly. If you export your data less frequent than every60 seconds, the autoscaler might not respond to load changes quickly enough.
- The metric must export data that is of
INT64orDOUBLEvalue type.
For information about creating a custom metric, seeUsing custom metrics.
What's next
- Learn aboutmanaging autoscalers.
- Learnhow autoscalers make decisions.
- Learn how to usemultiple autoscaling signalsto scale your group.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.