Create a MIG in a single zone


This document describes how to create a managed instance group (MIG) in a single zone. Putting all your MIG's VMs in a single zone helps to minimize latency, which is useful for certain workloads—for example, batch workloads.

This type of MIG is also known as a zonal MIG.

You can also read about other basic scenarios for creating a MIG.

Before you begin

  • Create an instance template, which is required in order to create a managed instance group.
  • If you haven't already, 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 as follows.

    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

    1. Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init
    2. Set a default region and zone.

    Terraform

    To use the Terraform samples on this page from 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. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. Create local authentication credentials for your Google Account:

      gcloud auth application-default login

    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.

      Install the Google Cloud CLI, then initialize it by running the following command:

      gcloud init

Limitations

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

Create a MIG in a single zone

To create a MIG in a single zone, use the Google Cloud console , the gcloud CLI, Terraform, or REST.

Console

  1. In the Google Cloud console, go to the Instance groups page.

    Go to Instance groups

    The remaining steps will appear automatically in the Google Cloud console.

  2. Click Create instance group.
  3. If you want to create a stateful MIG, select the New managed instance group (stateful) option. To help you decide, see When to use stateful MIGs.
  4. Assign a name and optionally a description to your instance group.
  5. Choose an instance template for the instance group or create a new one.
  6. Specify the number of VMs for this group. Remember to provision enough VMs to support your application if a zone failure happens.
  7. For Location, select Single zone.
  8. Select the Region and the Zone where you want to create your MIG. If you chose a regional instance template, then the Region is automatically selected based on the template's region.
  9. For stateless MIGs, Autoscaling is enabled by default. With autoscaling, your group automatically adds or removes instances based on its utilization.
  10. Optionally, enable autohealing to perform application-based health checking on VMs within the group.
  11. Click Create to create the new group.

gcloud

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.

Create a managed instance group with the instance-groups managed create command and specify the group name, group size, instance template, and zone.

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --size SIZE \
    --template INSTANCE_TEMPLATE \
    --zone ZONE

Replace the following:

  • INSTANCE_GROUP_NAME: the name for this instance group.
  • SIZE: the size of the instance group.
  • INSTANCE_TEMPLATE: the name of the instance template to use for this group. For a regional instance template, you must specify the full or partial URL of the template. An example of a full URL is https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template and a partial URL is projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template.
  • ZONE: one of the zones available for Compute Engine. If you want to distribute your MIG's VMs across multiple zones in a region, see Create a regional MIG.

You can optionally supply the --base-instance-name flag. Because these VMs are based on a common template, each VM is assigned a random string as part of its VM name. The base name is prepended to this random string. For example, if you set the base name to test, VMs will have names like test-yahs and test-qtyz. If you need specific names, see Creating instances with specific names in MIGs.

For example, the following command creates an instance group named example-group, with base VM name test. The group contains three instances:

gcloud compute instance-groups managed create example-group \
    --base-instance-name test \
    --size 3 \
    --template an-instance-template \
    --zone us-central1-f

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 zonal MIG, you can use the google_compute_instance_group_manager resource.

resource "google_compute_instance_group_manager" "default" {

  name               = "example-group"
  base_instance_name = "test"
  target_size        = 3
  zone               = "us-central1-f"

  version {
    instance_template = google_compute_instance_template.default.id
    name              = "primary"
  }
}

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

REST

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.

Create a managed instance group with the instanceGroupManagers.insert method. In the request body, include the group name, group size, and the URL to the instance template.

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instanceGroupManagers

{
  "versions": [
    {
      "instanceTemplate": "global/instanceTemplates/INSTANCE_TEMPLATE"
    }
  ],
  "name": "INSTANCE_GROUP_NAME",
  "targetSize": SIZE
}

Replace the following:

  • PROJECT_ID: the project ID for the request.
  • ZONE: the zone for the request. If you want to distribute your MIG's VMs across multiple zones in a region, replace zones/ZONE with regions/REGION and specify a region. For more information, see Create a regional MIG.
  • INSTANCE_GROUP_NAME: the name for this instance group.
  • SIZE: the size of the instance group.
  • INSTANCE_TEMPLATE: the instance template to use for this group.

You can optionally supply the base-name field. Because these VMs are based on a common template, each VM is assigned a random string as part of its VM name. The base name is prepended to this random string. For example, if you set the base name to test, VMs will have names like test-yahs and test-qtyz. If you need specific names, see Creating instances with specific names in MIGs.

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.

What's next