Set VM availability policies


This document describes how you can set a virtual machine (VM) instance's availability policies to configure the behavior of VMs when a maintenance event occurs.

Before you begin

Limitations

  • You can't change the availability policies for a preemptible VM. When there is a maintenance event, the preemptible VM stops and it does not migrate. You must manually restart the preempted VM.
  • After you create a VM using an E2 machine type, you can't change the VM's host maintenance settings from Migrate to Terminate or the other way around.

Setting availability policies

You can change the availability policies of a VM when you first create a VM or after the VM is created, by using the setScheduling method. To configure a VM's maintenance behavior and automatic restart setting, use the onHostMaintenance and automaticRestart properties.

  • onHostMaintenance: determines the behavior when a maintenance event occurs that might cause your VM to reboot.

    • MIGRATE: causes Compute Engine to live migrate an instance when there is a maintenance event. This is the default value.
    • TERMINATE: stops a VM instead of migrating it.
  • automaticRestart: determines the behavior when a VM crashes or is stopped by the system.

    • true: Compute Engine restarts an instance if the instance crashes or is stopped. This is the default value.
    • false: Compute Engine does not restart a VM if the VM crashes or is stopped.

All VMs are configured with default values unless you explicitly specify otherwise.

Set availability policy during VM creation

Console

  1. In the Google Cloud Console, go to the Create an instance page.

    Go to Create an instance

  2. Expand the Networking, disks, security, management, sole tenancy section, and do the following:

    1. Expand the Management section.
    2. In the Automatic restart list, select the option that you want.
    3. In the On host maintenance list, select the option that you want.
  3. To create the VM, click Create.

gcloud

To specify the availability policies of a new VM in gcloud compute, use the --maintenance-policy flag to specify whether the VM is migrated or stopped. By default, VMs are automatically set to restart unless you provide the --no-restart-on-failure flag.

gcloud compute instances create VM_NAME \
    --maintenance-policy MAINTENANCE_POLICY \
    [--no-restart-on-failure]

Replace the following:

  • VM_NAME: the VM name
  • MAINTENANCE_POLICY: the policy for this VM, either TERMINATE or MIGRATE

API

In the API, make a POST request to the following URL:

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

Replace the following:

  • PROJECT_ID: the project ID of the VM
  • ZONE: the zone of the VM

with the onHostMaintenance and automaticRestart parameters as part of the request body:

    {
      "name": "example-instance",
      "description": "Front-end for real-time ingest; don't migrate.",
      ...
      // User options for influencing this VM's life cycle.
      "scheduling": {
        "onHostMaintenance": "MIGRATE",
        "automaticRestart": true
      }
    }

For more information, see the Instances reference documentation.

Set availability policy for an existing VM

Console

  1. In the Google Cloud Console, go to the VM instances page.

    Go to VM instances

  2. Click the VM for which you want to change settings. The VM details page displays.

  3. On the VM details page, complete the following steps:

    1. Click the Edit button at the top of the page.
    2. Under Availability policies, update the policy as needed. From the Availability policies section, you can set the On host maintenance and Automatic restart options.
    3. Click Save.

gcloud

To update the availability policies of a VM, use the gcloud compute instances set-scheduling command with the same parameters and flags used in the VM creation command above:

gcloud compute instances set-scheduling VM_NAME \
    --maintenance-policy MAINTENANCE_POLICY \
    [--no-restart-on-failure | --restart-on-failure]

Replace the following:

  • VM_NAME: the VM name
  • MAINTENANCE_POLICY: the policy for this VM, either TERMINATE or MIGRATE

API

In the API, you can make a request to the following URL, replacing the project ID, zone, and VM name with your own project ID, zone, and VM name:

https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setScheduling

The body of your request must contain the new value for the availability policies:

    {
      "onHostMaintenance": "MIGRATE",
      "automaticRestart": true
    }

For more information, see the instances().setScheduling reference documentation.

What's next