When you create an autoscaling policy with multiple signals, the autoscaler scales based on the signal 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 signals, see Autoscaling policy.
Before you begin
- Read about autoscaler fundamentals.
-
If you haven't already, then set up authentication.
Authentication is
the process by which your identity is verified 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, then initialize it by running the following command:
gcloud init
- 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, then initialize it by running the following command:
gcloud init
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
How autoscaler handles multiple signals
An autoscaler handles multiple signals by calculating the recommended number of VMs for each signal and then picking the signal that results in the largest number of VMs in the MIG.
An autoscaler can handle one signal per metric type except in the case of Cloud Monitoring metrics and scaling schedules. You can choose up to 5 autoscaling signals 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 signal, up to 1 load balancing signal, up to 5 custom metric signals, and up to 128 scaling schedules.
For example, you can define an autoscaler with all of the following signals:
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
In this example, 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 the preceding values, the autoscaler calculates the recommended number of VMs based on each signal:
- 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 signal 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 signals
Creating an autoscaler with multiple signals is slightly different depending on whether you are using the Google Cloud console, gcloud CLI, or REST.
Console
In the Google Cloud console, go to the Instance groups page.
Click the name of a MIG from the list. Then click Edit.
On the edit page under Autoscaling mode, turn on autoscaling by selecting On: add and remove instances to the group from the drop-down menu.
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 its autoscaling signals.
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 signals.
For each metric-based autoscaling signal that you want to add, under Autoscaling signals, click Add a signal.
- To create an autoscaling signal based on average CPU utilization, under Signal 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 signal based on HTTP load balancing serving capacity, under Signal 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 signal based on Cloud Monitoring metrics, under Signal type select Cloud Monitoring metric 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 signal that you want to add, expand the Autoscaling schedules section, click Manage schedules, then click Create schedule.
In the new Create scaling 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 Initialization period, enter the number of seconds that your application needs to initialize—this lets your VMs finish initializing before the autoscaler considers its usage data reliable. For more information, see Initialization period.
Optional: to enable scale-in controls, click Scale-in controls, then select the Enable scale-in controls checkbox. For more information, see scale-in controls
Click Save.
gcloud
To create an autoscaler with multiple signals, use the
set-autoscaling
command.
Pass in multiple signal specifications using their respective
command-line flags.
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.
gcloud 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 signal, see the following pages:
REST
To create an autoscaler, use the
autoscalers.insert
method
for a zonal MIG or the
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.
POST https://compute.googleapis.com/compute/v1/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 signal, see the following pages: