Add instance flexibility


This document describes how to add instance flexibility that lets you set multiple machine types in a MIG.

You can add instance flexibility when creating a MIG or by editing an existing MIG. To add the flexibility, configure an instance flexibility policy in the MIG. The policy overrides the machine type specified in the instance template. Each time the MIG creates a virtual machine (VM) instance, it automatically selects one of the machine types you've listed in the policy based on resource availability. You can also assign ranks to the lists of machine types to indicate your preference.

To know how instance flexibility works in a MIG, see About instance flexibility .

Before you begin

  • Make sure you choose machine types that are supported in the region where your MIG is located. To see the machine types in a region, see Available regions and zones .
  • 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:

    gcloud

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

      gcloud init
    2. 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.

Create a MIG with instance flexibility

Before creating a MIG, you must first create an instance template , if you don't already have one. You can then create a MIG with instance flexibility to override the machine type specified in the instance template.

Create a MIG with instance flexibility in one of the following ways:

Create a MIG with multiple machine types

gcloud

To create a regional MIG with multiple machine types, use the beta instance-groups managed create command as follows:

gcloud beta compute instance-groups managed create INSTANCE_GROUP_NAME 
\
    --region REGION 
\
    --size TARGET_SIZE 
\
    --template INSTANCE_TEMPLATE 
\
    --target-distribution-shape SHAPE 
\
    --instance-redistribution-type none \
    --instance-selection-machine-types MACHINE_TYPE,MACHINE_TYPE,... 

Zonal MIGs don't support instance flexibility. However, if you want to create a MIG in a single zone, set the target distribution shape to any-single-zone . Furthermore, if you want a specific zone, use the any-single-zone distribution shape and include the --zones ZONE flag.

Replace the following:

  • INSTANCE_GROUP_NAME : the name of the MIG.
  • REGION : the region where you want to create the MIG.
  • TARGET_SIZE : the number of VMs you want the MIG to create and maintain.
  • INSTANCE_TEMPLATE : the name of the instance template.
  • SHAPE : the target distribution shape. The value can be balanced or any-single-zone . The other target distribution shapes are not supported.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG—for example, n1-standard-16,n2-standard-16,e2-standard-16 .

REST

To create a regional MIG with multiple machine types, make a POST request to the beta regionInstanceGroupManagers.insert method .

POST https://compute.googleapis.com/compute/beta/projects/ PROJECT_ID 
/regions/ REGION 
/instanceGroupManagers

{
  "name": " INSTANCE_GROUP_NAME 
",
  "targetSize": TARGET_SIZE 
,
  "instanceTemplate": " INSTANCE_TEMPLATE_URL 
",
  "distributionPolicy": {
    "targetShape": " SHAPE 
"
  },
  "updatePolicy": {
    "instanceRedistributionType": "NONE"
  },
  "instanceFlexibilityPolicy": {
    "instanceSelections": {
      " INSTANCE_SELECTION 
": {
        "machineTypes": [
          " MACHINE_TYPE_1 
",
          " MACHINE_TYPE_2 
",
          ...
        ]
      }
    }
  }
}

Zonal MIGs don't support instance flexibility. However, if you want to create a MIG in a single zone, set the target distribution shape to ANY_SINGLE_ZONE . Furthermore, if you want a specific zone, use the ANY_SINGLE_ZONE distribution shape and include the distributionPolicy.zones[].zone field in the request.

Replace the following:

  • PROJECT_ID : your project ID .
  • REGION : the region where you want to create the MIG.
  • INSTANCE_GROUP_NAME : the name of the MIG.
  • TARGET_SIZE : the number of VMs you want the MIG to create and maintain.
  • INSTANCE_TEMPLATE_URL : the URL of the instance template.
  • SHAPE : the target distribution shape. The value can be BALANCED or ANY_SINGLE_ZONE . The other target distribution shapes are not supported.
  • INSTANCE_SELECTION : the name for the list of machine types.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG—for example, "n1-standard-16","n2-standard-16","e2-standard-16" .

Create a MIG with multiple machine types and preference

gcloud

To create a regional MIG with multiple machine types and preferences, use the beta instance-groups managed create command .

gcloud beta compute instance-groups managed create INSTANCE_GROUP_NAME 
\
    --region REGION 
\
    --size TARGET_SIZE 
\
    --template INSTANCE_TEMPLATE 
\
    --target-distribution-shape SHAPE 
\
    --instance-redistribution-type none \
    --instance-selection "name= INSTANCE_SELECTION_1 
,machine-type= MACHINE_TYPE_1 
,machine-type= MACHINE_TYPE_2 
,rank= RANK_1 
" \
    --instance-selection "name= INSTANCE_SELECTION_2 
,machine-type= MACHINE_TYPE_3 
,machine-type= MACHINE_TYPE_4 
,rank= RANK_2 
"

Zonal MIGs don't support instance flexibility. However, if you want to create a MIG in a single zone, set the target distribution shape to any-single-zone . Furthermore, if you want a specific zone, use the any-single-zone distribution shape and include the --zones ZONE flag.

Replace the following:

  • INSTANCE_GROUP_NAME : the name of the MIG.
  • REGION : the region in which you want to create the MIG.
  • TARGET_SIZE : the number of VMs that you want the MIG to create and maintain.
  • INSTANCE_TEMPLATE : the name of the instance template.
  • SHAPE : the target distribution shape. The value can be balanced or any-single-zone . The other target distribution shapes are not supported.
  • INSTANCE_SELECTION : the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG.

REST

To create a regional MIG with multiple machine types, make a POST request to the beta regionInstanceGroupManagers.insert method .

POST https://compute.googleapis.com/compute/beta/projects/ PROJECT_ID 
/regions/ REGION 
/instanceGroupManagers

{
  "name": " INSTANCE_GROUP_NAME 
",
  "targetSize": TARGET_SIZE 
,
  "instanceTemplate": " INSTANCE_TEMPLATE_URL 
",
  "distributionPolicy": {
    "targetShape": " SHAPE 
"
  },
  "updatePolicy": {
    "instanceRedistributionType": "NONE"
  },
  "instanceFlexibilityPolicy": {
    "instanceSelections": {
      " INSTANCE_SELECTION_1 
": {
        "machineTypes": [
          " MACHINE_TYPE_1 
",
          " MACHINE_TYPE_2 
",
          ...
        ],
        "rank": RANK_1 
},
      " INSTANCE_SELECTION_2 
": {
        "machineTypes": [
          " MACHINE_TYPE_3 
",
          " MACHINE_TYPE_4 
",
          ...
        ],
        "rank": RANK_2 
},
      ...
    }
  }
}

Zonal MIGs don't support instance flexibility. However, if you want to create a MIG in a single zone, set the target distribution shape to ANY_SINGLE_ZONE . Furthermore, if you want a specific zone, use the ANY_SINGLE_ZONE distribution shape and include the distributionPolicy.zones[].zone field in the request.

Replace the following:

  • PROJECT_ID : your project ID .
  • REGION : the region where you want to create the MIG.
  • INSTANCE_GROUP_NAME : the name of the MIG.
  • TARGET_SIZE : the number of VMs you want the MIG to create and maintain.
  • INSTANCE_TEMPLATE_URL : the URL of the instance template.
  • SHAPE : the target distribution shape. The value can be BALANCED or ANY_SINGLE_ZONE .
  • ZONE : the zone where you want to create the MIG. Use this field if you want to create a regional MIG in a particular zone. Otherwise, you can skip this field.
  • INSTANCE_SELECTION : the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG.
  • RANK : the number representing the order of preference. A lower value means higher preference.

Add instance flexibility to an existing MIG

You can add instance flexibility to an existing MIG. If the MIG already has VMs, you must remove them by resizing the MIG to 0. Then, you can set multiple machine types in the MIG. You can also set preferences for certain machine types.

If you want to add an instance selection to an existing instance flexibility policy, see Change instance flexibility configuration .

Set multiple machine types

gcloud

To set multiple machine types in a MIG, use the beta instance-groups managed update command .

gcloud beta compute instance-groups managed update INSTANCE_GROUP_NAME 
\
    --region REGION 
\
    --size 0 \
    --instance-selection-machine-types MACHINE_TYPE,MACHINE_TYPE,... 

Replace the following:

  • INSTANCE_GROUP_NAME : the name of the MIG in which you want to add instance flexibility.
  • REGION : the region where the MIG is located.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG—for example, n1-standard-16,n2-standard-16,e2-standard-16 .

REST

To set multiple machine types in a MIG, make a PATCH request to the beta regionInstanceGroupManagers.patch method .

PATCH https://compute.googleapis.com/compute/beta/projects/ PROJECT_ID 
/regions/ REGION 
/instanceGroupManagers/ INSTANCE_GROUP_NAME 
{
  "targetSize": 0,
  "instanceFlexibilityPolicy": {
    "instanceSelections": {
    " INSTANCE_SELECTION 
": {
        "machineTypes": [
          " MACHINE_TYPE_1 
",
          " MACHINE_TYPE_2 
"
          ...
        ]
      }
    }
  }
}

Replace the following:

  • PROJECT_ID : your project ID .
  • REGION : the region where the MIG is located.
  • INSTANCE_GROUP_NAME : the name of the MIG in which you want to add instance flexibility.
  • INSTANCE_SELECTION : a name for the list of machine types.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG—for example, "n1-standard-16","n2-standard-16","e2-standard-16" .
  • RANK : the number representing the order of preference. A lower value means higher preference.

Set multiple machine types and preferences

gcloud

To set multiple machine types with preferences in a MIG, use the beta instance-groups managed update command

gcloud beta compute instance-groups managed update INSTANCE_GROUP_NAME 
\
    --region REGION 
\
    --size 0 \
    --instance-selection "name= INSTANCE_SELECTION_1 
,machine-type= MACHINE_TYPE_1 
,machine-type= MACHINE_TYPE_2 
,rank= RANK_1 
" \
    --instance-selection "name= INSTANCE_SELECTION_2 
,machine-type= MACHINE_TYPE_3 
,machine-type= MACHINE_TYPE_4 
,rank= RANK_2 
"

Replace the following:

  • INSTANCE_GROUP_NAME : the name of the MIG in which you want to add instance flexibility.
  • REGION : the region where the MIG is located.
  • INSTANCE_SELECTION : the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG.

REST

To set multiple machine types with preferences in a MIG, use the beta regionInstanceGroupManagers.patch method .

PATCH https://compute.googleapis.com/compute/beta/projects/ PROJECT_ID 
/regions/ REGION 
/instanceGroupManagers/ INSTANCE_GROUP_NAME 
{
  "targetSize": 0,
  "instanceFlexibilityPolicy": {
    "instanceSelections": {
      " INSTANCE_SELECTION_1 
": {
        "machineTypes": [
          " MACHINE_TYPE_1 
",
          " MACHINE_TYPE_2 
"
        ],
        "rank": RANK_1 
},
      " INSTANCE_SELECTION_2 
": {
        "machineTypes": [
          " MACHINE_TYPE_3 
",
          " MACHINE_TYPE_4 
"
        ],
        "rank": RANK_2 
}
    }
  }
}

Replace the following:

  • PROJECT_ID : your project ID .
  • REGION : the region where the MIG is located.
  • INSTANCE_GROUP_NAME : the name of the MIG.
  • INSTANCE_SELECTION : the name of the list of machine types. You can add multiple lists with different preferences.
  • MACHINE_TYPE : the machine types that you want to configure in the MIG.

What's next