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
-
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 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
-
In the console, go to the Instance templates page.
The remaining steps appear in the Google Cloud console.
-
Click
Create instance template . - Fill in the properties that you want for your instance template.
-
Click
Advanced options and expand the Management section. -
Under Availability policy, in the
VM provision model list, choose Spot. -
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.
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.