Create a MIG that uses preemptible VMs


This document describes how to create a managed instance group (MIG) that uses preemptible virtual machine (VM) instances. Preemptible VMs are useful if your workload can tolerate disruptions and you want to take advantage of the cost-savings associated with preemptible VMs.

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

Before you begin

  • 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

    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 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. To initialize the gcloud CLI, run the following command:

      gcloud init
    3. 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 an instance template with preemptible VMs

You can use a zonal or regional MIG to quickly create multiple preemptible VMs, which can reduce the costs of the VMs in your managed instance groups. For example, you can create a group of preemptible VMs, use them to run a batch processing task, and then delete the group when the task is complete.

To create a group of preemptible VMs, set the preemptible option in an instance template, and then use the template to create the MIG.

Console

  1. In the console, go to the Instance templates page.

    Go to Instance templates

    The remaining steps appear in the Google Cloud console.

  2. Click Create instance template.
  3. Fill in the properties that you want for your instance template.
  4. Click Advanced options and expand the Management section.
  5. Under Availability policy, in the VM provision model list, choose Spot.
  6. Click Create to create the template.

gcloud

Create an instance template by using the instance-templates create command. Include the --preemptible flag.

gcloud compute instance-templates create INSTANCE_TEMPLATE \
    --preemptible

Terraform

The following sample creates a global instance template. To provide the preemptible option, include the scheduling block. For more information about the resource used in the sample, see google_compute_instance_template resource. To create a regional instance template, use the google_compute_region_instance_template resource.

resource "google_compute_instance_template" "default" {
  name         = "preemptible-template"
  machine_type = "n1-standard-1"
  disk {
    source_image = "debian-cloud/debian-11"
  }
  network_interface {
    network = "default"
  }
  scheduling {
    preemptible       = "true"
    automatic_restart = "false"
  }
}

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

REST

Call the instanceTemplates.insert method to create a new instance template. Include the scheduling.preemptible property and set it to true.

{
"name": "INSTANCE_TEMPLATE",
"properties": {
  "machineType": "zones/ZONE/machineTypes/MACHINE_TYPE",
  "networkInterfaces": [
    {
      "network": "global/networks/default",
      "accessConfigs":
      [
        {
          "name": "external-IP",
          "type": "ONE_TO_ONE_NAT"
        }
      ]
    }
  ],
  "scheduling":
  {
    "preemptible": true
  },
  "disks":
  [
    {
      "type": "PERSISTENT",
      "boot": true,
      "mode": "READ_WRITE",
      "initializeParams":
      {
        "sourceImage": "projects/debian-cloud/global/images/family/debian-9"
      }
    }
  ]
  }
}

After you create the instance template, use it to create a MIG with VMs confined to a single zone or with VMs spread across multiple zones in a region.

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.