Create an autoscaler with multiple policies and the autoscaler will scale based on the policy that provides the largest number of virtual machines in the group. This ensures that there is always enough virtual machines to handle your application workloads and allows you to scale applications with multiple possible bottlenecks.
Before you begin
- If you want to use the command-line examples in this guide:
- Install or update to the latest version of the gcloud command-line tool.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
- Read about autoscaling policies.
How autoscaler handles multiple policies
The autoscaler handles multiple policies by calculating the recommended number of virtual machines for each policy and then picking the policy that leaves the largest number of virtual machines in the group.
An autoscaler can handle one policy per metric type except in the case of Stackdriver monitoring metrics; you can choose up to five policies for Stackdriver monitoring metrics. For example, you can create an autoscaler that uses one CPU utilization policy, one load balancing policy, and up to five custom metric policies.
For example, if you defined an autoscaler with all of the following policy definitions:
cpuUtilizationwith target of 0.8.loadBalancingUtilizationwith target of 0.6.customMetricUtilizationfor metric1 with target of 1000.customMetricUtilizationfor metric2 with target of 2000.
And the autoscaler measures the following average values for the managed instance group that contains 10 virtual machines:
- 0.5 for CPU utilization.
- 0.4 load balancing utilization.
- 1100 for metric1.
- 2700 for metric2.
Using the average values, the autoscaler calculates recommended changes to the number of virtual machines, based on each policy:
- 7 virtual machines based on CPU utilization.
- 7 virtual machines based on load balancing utilization.
- 11 virtual machines based on metric1.
- 14 virtual machines based on metric2.
Given that there is more than one policy for this autoscaler, the autoscaler chooses the largest recommend value, in this case, 14 machines, and resizes the managed instance group to 14 virtual machines.
Create an autoscaler with multiple policies
Creating an autoscaler with multiple policies is slightly different depending
on whether you are using the gcloud command-line tool or the API.
Console
- Go to the Instance groups page.
- Select the desired instance group from the list and click Edit.
- On the instance group details page under Autoscaler, select On from the drop-down menu to turn on autoscaling.
In the Autoscale based on section, select Multiple metrics from drop-down menu.
Fill in fields for CPU utilization and load balancing utilization, if desired.
To add custom metric policies, click the + icon under the Target monitoring metrics section and fill in the fields for a custom metric policy.
You can add multiple custom metric policies by clicking the + icon.
gcloud
To create an autoscaler with multiple policies, pass in multiple policy
specifications using the respective command-line flags. The following
command creates an autoscaler that uses CPU utilization and two custom
metrics:
gcloud compute instance-groups managed set-autoscaling example-managed-instance-group \
--target-cpu-utilization 0.8 \
--custom-metric-utilization metric=custom.cloudmonitoring.googleapis.com/path/to/metric1,utilization-target=1000.0,utilization-target-type=GAUGE \
--custom-metric-utilization metric=custom.cloudmonitoring.googleapis.com/path/to/metric2,utilization-target=2000.0,utilization-target-type=DELTA_PER_SECOND \
--max-num-replicas 50
With the exception of the custom metric flags, you can only pass in one flag maximum for each metric type.
For custom metrics, you can provide multiple custom metrics by repeating the following flags:
--custom-metric-utilization metric=METRIC,utilization-target=TARGET,utilization-target-type=TYPE
API
In the API, make a POST request with the request body containing all
of the desired policies:
POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/autoscalers
{
"autoscalingPolicy" : {
"cpuUtilization":{
"utilizationTarget": 0.8
},
"customMetricUtilizations":[
{
"metric": "custom.cloudmonitoring.googleapis.com/path/to/metric1",
"utilizationTarget": 1000,
"utilizationTargetType":"GAUGE"
},
{
"metric": "custom.cloudmonitoring.googleapis.com/path/to/metric2",
"utilizationTarget": 2000,
"utilizationTargetType": "DELTA_PER_SECOND"
}
],
"loadBalancingUtilization":{
"utilizationTarget": 0.6
},
"maxNumReplicas": 50,
"minNumReplicas": 1
}
"target": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroupManagers/example-managed-instance-group",
"name": "example-autoscaler"
}

