This document describes how to create a managed instance group (MIG) that has its VMs spread across multiple zones in a region. Spreading your application load across multiple zones protects your workload against zonal failures. If a zonal failure happens, your application can continue serving from instances running in the remaining available zones in the same region.
This type of MIG is also known as a regional MIG. For more information, see About regional MIGs.
You can also read about other basic scenarios for creating a MIG.
Before you begin
- If you want to use the API examples in this guide, set up API access.
- If you want to use the command-line examples in this guide, install the Google Cloud CLI.
- Create an instance template, which is required in order to create a managed instance group.
Limitations
To see the full list of MIG limitations, which varies based on the configuration that you use, see MIG limitations.
Create a MIG with VMs in multiple zones in a region
Use the console , the gcloud CLI or the Compute Engine API.
Console
-
In the console, go to the Instance groups page.
- Click Create instance group to create a new instance group.
-
Select one of the New managed instance group options:
- Stateless (default)
- Stateful
- Assign a name and optionally a description to your instance group.
- Choose an instance template for the instance group or create a new one.
- In Number of instances or in Autoscaling, specify the number of instances for this group. Remember to provision enough VMs to support your application if a zone failure happens.
- Under Location, select Multiple zones.
- Choose a region and select the zones you want to use.
- Under Target distribution shape, select Even. If you want to select a different shape, see Set a target distribution shape for VMs in a regional MIG.
-
If you want to disable proactive instance redistribution:
- Ensure that Autoscaling mode is set to Off: do not autoscale.
- Under Instance redistribution, deselect the Enable instance redistribution checkbox.
- Continue with the rest of the MIG creation process.
gcloud
All MIGs require an instance template. If you don't have one, create an instance template. For example, the following command creates a basic instance template with default properties:
gcloud compute instance-templates create example-template
Next, use the
instance-groups managed create
command
with the --region
flag. For example, the following command creates a
regional MIG in three zones within the us-east1
region:
gcloud compute instance-groups managed create example-rmig \ --template example-template \ --size 30 \ --region us-east1
If you want to select specific zones
that the group should use, provide the --zones
flag:
gcloud compute instance-groups managed create example-rmig \ --template example-template \ --size 30 \ --zones us-east1-b,us-east1-c
If you want to disable proactive instance redistribution,
set the --instance-redistribution-type
flag to NONE
. You cannot disable
proactive instance redistribution if autoscaling is enabled.
gcloud compute instance-groups managed create example-rmig \ --template example-template \ --size 30 \ --instance-redistribution-type NONE \ --region us-east1
API
All MIGs require an instance template. If you don't have one, create an instance template.
Next, construct a POST
request to the
regionInstanceGroupManagers.insert
method.
In the request body, specify the group name, group size, and the URL
to the instance template. Optionally, specify other fields, such as the base
name for instances in the group.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers { "baseInstanceName": "BASE_INSTANCE_NAME", "instanceTemplate": "global/instanceTemplates/INSTANCE_TEMPLATE_NAME", "name": "INSTANCE_GROUP_NAME", "targetSize": "TARGET_SIZE" }
Replace the following:
PROJECT_ID
: the project ID for this request.REGION
: the region for the group.BASE_INSTANCE_NAME
: the instance name for each VM instance that is created as part of the group. For example, a base instance name ofexample-instance
would create instances that have names likeexample-instance-[RANDOM_STRING]
where[RANDOM_STRING]
is generated by the server.INSTANCE_TEMPLATE_NAME
: the instance template to use.INSTANCE_GROUP_NAME
: the name of the MIG.TARGET_SIZE
: the target number of VMs for the group.
If you want to
select specific zones
or if you are creating VMs in a region with less than or more than three
zones, include the distributionPolicy
property in your request and supply
a list of zones. Replace ZONE
with the name of a
zone to create VMs in.
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers { "baseInstanceName": "BASE_INSTANCE_NAME", "instanceTemplate": "global/instanceTemplates/INSTANCE_TEMPLATE_NAME", "name": "INSTANCE_GROUP_NAME", "targetSize": "TARGET_SIZE", "distributionPolicy": { "zones": [ {"zone": "zones/ZONE"}, {"zone": "zones/ZONE"} ] } }
For example, the following creates a regional MIG named example-rmig
with 10 managed instances distributed across us-east1-b
and us-east1-c
zones:
POST https://compute.googleapis.com/compute/v1/projects/myproject/regions/us-east1/instanceGroupManagers { "instanceTemplate": "global/instanceTemplates/example-instance", "name": "example-rmig", "targetSize": 10, "distributionPolicy": { "zones": [ {"zone": "zones/us-east1-b"}, {"zone": "zones/us-east1-c"} ] } }
Depending on how you configure and act on a MIG, various policies and actions can affect the instances in the group. To determine which managed instances are up and running, see Checking the status of managed instances.
If there is not enough capacity in each zone to support VMs for the group, Compute Engine creates as many VMs as possible and continues attempting to create the remaining VMs when additional capacity becomes available.
If you do not explicitly specify individual zones in your request, Compute Engine automatically chooses three zones to create VMs in. If you need to create VMs in more than or fewer than three zones, or you want to pick which zones are used, you can provide a list of zones in your request. For more information, see Zone selection.
Distribution of VMs across zones
By default, a regional MIG distributes VMs evenly across selected zones. Because you are creating a regional MIG, keep in mind that certain resources are zonal, such as GPUs and existing persistent disks. If your MIG's instance template specifies zonal resources, you must make sure all those resources are present in all selected zones so they can be attached to the VMs created by the regional MIG. Alternatively, if you want the group to check for the existence of zonal resources for you, you can set the MIG's target distribution shape to create instances only in zones that contain those resources.
For more information, see the following documents:
Set a target distribution shape for VMs across zones
Proactive instance redistribution
Proactive instance redistribution
is enabled by default. If you need to manually manage the number of VMs in each
zone or set the MIG's target distribution shape to BALANCED
, you must
disable proactive instance redistribution.
For more information, see the following docs:
What's next
- Set up application-based autohealing, which periodically verifies that your application responds as expected on each of the MIG's VMs and automatically recreates unresponsive VMs.
- Enable autoscaling to automatically add or delete VMs from your MIG based on increases or decreases in load.
- Learn how to apply a new configuration to all or to a subset of the VMs in a MIG by setting and applying a new instance template or per-instance-configuration.
- Preserve disks, metadata, and IP addresses across disruptive events like VM recreation, autohealing, and updates by adding stateful configuration.
- Learn about working with managed instances, for example, to delete, abandon, and recreate VMs in a MIG.
- View info about MIGs and their VMs.
- Simulate a zone outage for a regional MIG