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, 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.
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.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
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
For more information, see Authenticate 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, 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
-
In the Google Cloud console, go to the Instance groups page.
The remaining steps appear in the Google Cloud console.
-
Click
Create instance group . -
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. - Assign a name and optionally a description to your instance group.
- Choose an instance template for the instance group or create a new one.
-
Specify the
number of VMs for this group. Remember to provision enough VMs to support your application if a zone failure happens. -
For
Location , select Single zone. - 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.
- For stateless MIGs, Autoscaling is enabled by default. With autoscaling, your group automatically adds or removes instances based on its utilization.
- Optionally, enable autohealing to perform application-based health checking on VMs within the group.
- 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 ishttps://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
and a partial URL isprojects/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.
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, replacezones/ZONE
withregions/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
- 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, all-instances configuration, 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.
- Learn how to add an external HTTP(S) load balancer frontend to your instance group. For information about other types of load balancers, see the Load balancing overview.