Create a MIG with VMs in multiple zones in a region (original) (raw)


This document describes how to create amanaged 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, seeAbout regional MIGs.

You can also read about otherbasic scenarios for creating a MIG.

Before you begin

Console

When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

gcloud

  1. After installing the Google Cloud CLI,initialize it by running the following command:
    gcloud init
    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
  2. Set a default region and zone.

Terraform

To use the Terraform samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.

  1. Install the Google Cloud CLI.
  2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
  3. To initialize the gcloud CLI, run the following command:
    gcloud init
  4. If you're using a local shell, then create local authentication credentials for your user account:
    gcloud auth application-default login
    You don't need to do this if you're using Cloud Shell.
    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
    For more information, see Set up authentication for a local development environment.

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.
After installing the Google Cloud CLI,initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, seeAuthenticate for using REST in the Google Cloud authentication documentation.

Limitations

To see the full list of MIG limitations, which varies based on the configuration that you use, seeMIG limitations.

Use the Google Cloud console, thegcloud CLI, Terraform, or REST.

Permissions required for this task

To perform this task, you must have the followingpermissions:

Console

  1. In the Google Cloud console, go to the Instance groups page.
    Go to Instance groups
    The remaining steps appear in the Google Cloud console.
  2. Click Create instance group to create a new instance group.
  3. Select one of the New managed instance group options:
  4. Assign a name and optionally a description to your instance group.
  5. Choose an instance template for the instance group orcreate a new one.
  6. In Number of instances or in Autoscaling, specify the number of instances for this group. Remember toprovision enough VMs to support your application if a zone failure happens.
  7. For Location, select Multiple zones.
  8. Choose a region and select the zones you want to use. If you chose a regional instance template, then the Region is automatically selected based on the template's region.
  9. 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.
  10. If you want to disable proactive instance redistribution, then under Instance redistribution, deselect the Allow instance redistribution checkbox.
  11. Continue with the rest of the MIG creation process.
  12. To create the MIG, click Create.

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 theinstance-groups managed create commandwith 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 zonesthat 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, seeTurning off proactive instance redistribution.

Terraform

If you haven't already created an instance template, which specifies the machine type, boot disk image, network, and other VM properties that you want for each VM in your MIG, create an instance template.

To create a regional MIG, you can use the google_compute_region_instance_group_managerresource.

To learn how to apply or remove a Terraform configuration, seeBasic Terraform commands.

REST

All MIGs require an instance template. If you don't have one, create an instance template.

Next, construct a POST request to theregionInstanceGroupManagers.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:

If you want toselect specific zonesor 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-rmigwith 10 managed instances distributed across us-east1-b and us-east1-czones:

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, seeChecking 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, seeZone 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:

Proactive instance redistribution

Proactive instance redistributionis enabled by default. If you need to manually manage the number of VMs in each zone, or if you need to set the MIG's target distribution shape to BALANCED orANY_SINGLE_ZONE, you must disable proactive instance redistribution.

For more information, see the following docs:

What's next