When you create an autoscaler with multiple policies, the autoscaler scales based on the policy that provides the largest number of virtual machine (VM) instances in the managed instance group (MIG). This ensures that there are always enough VMs to handle your application workloads and lets you scale applications with multiple possible bottlenecks.
To learn more about the different types of autoscaling policies, see policies.
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 VMs for each policy and then picking the policy that results in the largest number of VMs in the MIG.
An autoscaler can handle one policy per metric type except in the case of Cloud Monitoring metrics and scaling schedules (preview). You can choose up to 5 policies for Cloud Monitoring metrics and create up to 128 schedules per MIG. For example, you can create an autoscaler that uses up to 1 CPU utilization policy, up to 1 load balancing policy, up to 5 custom metric policies, and up to 128 scaling schedules.
For example, you can define an autoscaler with all of the following policy definitions:
cpuUtilization
with target of 0.8loadBalancingUtilization
with target of 0.6customMetricUtilization
for metric1 with target of 1000customMetricUtilization
for metric2 with target of 2000scalingSchedules
- A minimum of 6 VMs every Saturday and Sunday at 12:00 AM for 24 hours
- A minimum of 15 VMs every Monday through Friday at 9:00 AM for 8 hours
For those policies, suppose the autoscaler measures the following average utilization values for a MIG that contains 10 VMs at the time of measurement:
- 0.5 for CPU utilization
- 0.4 load balancing utilization
- 1100 for metric1
- 2700 for metric2
Using those values, the autoscaler calculates the recommended number of VMs based on each policy:
- 7 VMs based on CPU utilization
- 7 VMs based on load balancing utilization
- 11 VMs based on metric1
- 14 VMs based on metric2
- 6 VMs every Saturday and Sunday for the entire day based on the first scaling schedule
- 15 VMs every Monday through Friday from 9:00 AM to 5:00 PM based on the second scaling schedule
The autoscaler picks the policy that results in the largest number of VMs in the MIG and sets the MIG's recommended size to that value. In this case, the autoscaler resizes the MIG to 15 VMs if the second schedule is currently active or to 14 VMs otherwise.
Create an autoscaler with multiple policies
Creating an autoscaler with multiple policies is slightly different depending
on whether you are using the Cloud Console, gcloud
tool,
or the Compute Engine API.
Console
In the Cloud Console, go to the Instance groups page.
Click on the name of a MIG from the list. Then click Edit group.
On the edit page under Autoscaling mode, turn on autoscaling by selecting Autoscale from the drop-down menu.
For each metric-based autoscaling policy that you want to add, under Autoscaling policy, click Add new metric.
- To create an autoscaling policy based on average CPU utilization, under Metric type select CPU Utilization from the drop-down menu. Then, enter a Target CPU utilization and click Done. For more information, see Scaling based on CPU utilization.
- To create an autoscaling policy based on HTTP load balancing serving capacity, under Metric type select HTTP load balancing utilization from the drop-down menu. Then, enter a Target HTTP load balancing utilization and click Done. For more information, see Scaling based on load balancing serving capacity.
- To create an autoscaling policy based on Cloud Monitoring metrics, under Metric type select Stackdriver Monitoring from the drop-down menu. Then, complete the remaining fields and click Done. For more information, see Scaling based on Cloud Monitoring metrics.
For each schedule-based autoscaling policy that you want to add, under Autoscaling policy, click Add new scaling schedule.
In the new Create a schedule pane, complete the required fields and click Save. Then, in the Scaling schedules pane, click Done. For more information, see Scaling based on schedules.
Under Cool down period, enter the number of seconds that your application needs to initialize—this lets your VMs finish initializing before the autoscaler begins collecting usage information from them. For more information, see cool down periods.
Under Minimum number of instances, enter the minimum number of VMs that you want for this MIG. The minimum size of this MIG is always maintained regardless of the recommended size generated by its autoscaling policies.
Under Maximum number of instances enter the maximum number of VMs that you want for this MIG. The maximum size of this MIG is never exceeded regardless of the recommended size generated by its autoscaling policies.
Optional: to enable scale-in controls, select the Enable Scale In Controls checkbox. For more information, see scale-in controls
Click Save.
gcloud
To create an autoscaler with multiple policies, pass in multiple policy
specifications using their respective command-line flags. If you want to
create an autoscaler that includes scaling schedules, use the
gcloud beta compute instance-groups managed set-autoscaling
command. Otherwise, use the
gcloud compute instance-groups managed set-autoscaling
command.
For example, the following command creates
an autoscaler that uses CPU utilization, load balancing serving capacity,
two custom metrics, and one scaling schedule. The
--max-num-replicas
and --min-num-replicas
flags restrict how big
or small the MIG can get, regardless of each policy's recommended size.
gcloud beta compute instance-groups managed set-autoscaling MIG_NAME \ --target-cpu-utilization=0.8 \ --target-load-balancing-utilization=0.6 \ --custom-metric-utilization metric=PATH_TO_CUSTOM_METRIC_1,utilization-target=1000.0,utilization-target-type=GAUGE \ --custom-metric-utilization metric=PATH_TO_CUSTOM_METRIC_2,utilization-target=2000.0,utilization-target-type=DELTA_PER_SECOND \ --set-schedule=workday-capacity \ --schedule-cron="30 8 * * Mon-Fri" \ --schedule-duration-sec=30600 \ --schedule-min-required-replicas=10 \ --schedule-description="Have at least 10 VMs every Monday through Friday from 8:30 AM to 5 PM UTC" --min-num-replicas=1 --max-num-replicas=50
Replace the following:
MIG_NAME
: the name of a MIG.PATH_TO_CUSTOM_METRIC_1
andPATH_TO_CUSTOM_METRIC_2
: Optional: the paths to custom metrics with a format similar tocustom.cloudmonitoring.googleapis.com/path/to/metric1
andcustom.cloudmonitoring.googleapis.com/path/to/metric2
. For more information, see Custom metrics.
With the exception of the custom metric flags, you can only pass in one flag
maximum for each metric type per gcloud
command. For custom metrics, you
can provide multiple custom metrics in a single command by repeating
the --custom-metric-utilization
flag.
For more information about how to configure the flags for each type of autoscaling policy, see the following pages:
API
To create an autoscaler, use the
autoscalers.insert
method
for a zonal MIG or the
regionAutoscalers.insert
method
for a regional MIG.
If you want to create an autoscaler that includes scaling schedules,
you must use the
beta autoscalers.insert
method
for a zonal MIG or the
beta regionAutoscalers.insert
method
for a regional MIG.
For example, the following request creates an
autoscaler that uses CPU utilization, load balancing
serving capacity, two custom metrics, and two scaling schedules. The
maxNumReplicas
and minNumReplicas
fields restrict how big or
small the MIG can get, regardless of each policy's recommended size.
POST https://compute.googleapis.com/compute/beta/projects/PROJECT/zones/ZONE/autoscalers { "autoscalingPolicy" : { "cpuUtilization":{ "utilizationTarget": 0.8 }, "loadBalancingUtilization":{ "utilizationTarget": 0.6 }, "customMetricUtilizations":[ { "metric": "PATH_TO_CUSTOM_METRIC_1", "utilizationTarget": 1000, "utilizationTargetType":"GAUGE" }, { "metric": "PATH_TO_CUSTOM_METRIC_2", "utilizationTarget": 2000, "utilizationTargetType": "DELTA_PER_SECOND" } ], "scalingSchedules": { "workday-capacity": { "minRequiredReplicas": 10, "schedule": "30 8 * * Mon-Fri", "durationSec": 30600, "description": "Have at least 10 VMs every Monday through Friday from 8:30 AM to 5 PM UTC" }, "january-30-2030-schedule": { "minRequiredReplicas": 30, "schedule": "0 0 30 1 * 2030", "timeZone": "America/New_York", "durationSec": 86400, "description": "Schedule a minimum of 30 VMs all day for January 30, 2030" } }, "maxNumReplicas": 50, "minNumReplicas": 1 }, "target": "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instanceGroupManagers/MIG_NAME", "name": "MIG_NAME" }
Replace the following:
PROJECT
: your project id.ZONE
: the zone where your MIG is located.MIG_NAME
: the name of a MIG.PATH_TO_CUSTOM_METRIC_1
andPATH_TO_CUSTOM_METRIC_2
: Optional: the paths to custom metrics with a format similar tocustom.cloudmonitoring.googleapis.com/path/to/metric1
andcustom.cloudmonitoring.googleapis.com/path/to/metric2
. For more information, see Custom metrics.
As shown in this example, you can provide multiple custom metrics and scaling schedules in the same request.
For more information about how to configure the fields for each type of autoscaling policy, see the following pages: