How some operations affect preserved state of managed instances

Read the following sections to learn how various configurations, managed instance group (MIG) actions, or instance lifecycle events affect the preserved state of a managed instance, in a stateful MIG:

How autohealing handles preserved state

When a virtual machine (VM) instance becomes unhealthy, autohealing recreates the VM and keeps the preserved state for items that you have configured:

  • The MIG preserves stateful disks and reattaches them on VM recreation.
  • The MIG preserves stateful metadata, configured in per-instance config, and sets it on VM recreation.

To avoid VM instance recreation failures due to a corrupt stateful boot disk, keep the boot disk stateless so that autohealing can recreate that disk from scratch by using the original image.

How updating instances handles preserved state

When you update an instance, the MIG maintains the instance's preserved state (disks, metadata):

  • The MIG preserves stateful disks and reattaches them, if the VM instance is recreated or restarted during the update.
  • The MIG preserves stateful metadata, configured in a per-instance config, and sets it on the instance during the update.

When setting a new instance template, you must define all disks that you specified in your stateful policy. Setting a new instance template that omits a disk that is defined in a stateful policy is not allowed. This helps prevent accidental deletion of stateful disks.

To remove stateful disks from a MIG when those disks are defined in a stateful policy, use the following procedure:

  1. Remove the disk configuration from your stateful policy.
  2. (Optional.) Detach the disks from the VM instances if you still want to keep them.
  3. Rollout a new instance template that no longer defines the disks.

You cannot update stateful disks to a new image because these disks must be preserved during the update, and updating to a new image requires recreation of a disk.

Google recommends that you keep boot disks and any disks with binaries or temporary files stateless, while keeping your data on stateful disks. Such a configuration supports the following behavior:

  • You can easily and automatically update the boot disk and the disks with binaries to newer images that contain new versions and security patches. You can use auto-updating, or you can manually update the instances to recreate such stateless disks, while keeping your data intact on separate stateful disks.
  • You can preserve the data on stateful disks when rolling out other updates to your instances.

You can configure a boot disk to be stateful, for example, to host a legacy application that keeps binaries and data on the same disk. This lets you move an application to a MIG to benefit from autohealing. But, in this scenario, you must perform software and operating system updates yourself, for example, by updating individual packages using a package manager such as apt on Debian systems, or by using configuration management tools.

If you only configured custom instance names and did not configure stateful disks or metadata, then you can use automated rolling updates. For automated rolling updates, you must set the Updater's replacement policy to RECREATE. You cannot use the SUBSTITUTE replacement method for auto-updating instances in stateful MIGs because this method substitutes each existing VM with a new one that has a different name and preserved state.

How group resizing affects preserved state

Decreasing group size

Google does not recommend decreasing the size of a stateful MIG because the MIG picks the VM instances for deletion and might pick VMs that you need to preserve. You can remove MIG VM instances in a controlled way by deleting specific instances that you no longer need.

If you do decrease MIG size, the MIG deletes all extra VM instances, together with their associated preserved states. To prevent this, you can configure the MIG to detach and preserve stateful disks on permanent VM instance deletion. Stateful metadata is deleted together with preserved state. For more information, see how deleting an instance affects preserved state.

Increasing group size

When you increase the size of a stateful MIG, the group creates VMs from the current instance template with auto-generated names (base instance name + suffix). You can see the applied stateful configuration in the corresponding managed instance's preservedStateFromPolicy. After the MIG creates the instances, you can define stateful metadata and additional stateful disks in the per-instance configs for these instances.

You can pick custom instance names and increase group size by creating instances manually, with the option to bootstrap their state by providing per-instance configs with stateful metadata and disks for each instance.

How deleting an instance affects preserved state

A VM in a MIG is permanently deleted when:

  • You reduce the size of the group, and the MIG picks this VM instance for deletion, or
  • You delete the whole group, or
  • You specifically delete the instance from the MIG.

When a VM is permanently deleted, the MIG also deletes the corresponding per-instance config and managed instance, including its preserved state configuration.

Permanent VM deletion results in the loss of all stateful metadata key-value pairs.

You can configure whether to keep or delete stateful disks on permanent instance deletion by setting the autoDelete flag for each resource either in the stateful policy or in a per-instance config. The flag supports two options:

  • NEVER: (Default.) The MIG never deletes the disk.
  • ON_PERMANENT_INSTANCE_DELETION: The MIG deletes the disk when the instance is permanently deleted.

The MIG does not delete stateful resources when autohealing, updating, or recreating instances.

In the following example, the MIG has a single VM node-1 with a preserved state defined by a per-instance config. The preserved state includes two disks (blue & green) and id:xyz273 metadata. If you resize the MIG to zero, the MIG triggers permanent deletion of the instance, node-1, which causes the following effects:

  • The MIG deletes the managed instance and its preserved state configuration.
  • The MIG deletes the per-instance config for the instance.
  • The MIG deletes the actual VM instance resource.
  • Metadata id:xyz273 is lost because the VM instance and its preserved state configuration are deleted.
  • The stateful blue disk is deleted because, for this disk, the per-instance config has autoDelete: ON_PERMANENT_INSTANCE_DELETION.
  • The stateful green disk is detached because, for this disk, the per-instance config has autoDelete:NEVER.

Deleting an instance from a stateful MIG.

How abandoning an instance affects preserved state

When you abandon a VM instance from a MIG, the state of the VM, including stateful metadata and disks, remains on the instance outside of the MIG. Because the VM is no longer managed by the MIG, the MIG deletes the corresponding per-instance config and managed instance, including the instance's preserved state configuration.

In the following example, the VM node-1 has preserved state, defined by a stateful policy (blue disk) and a per-instance config (green disk and metadata id:xyz273). If you abandon the instance, node-1, from the MIG, here is what happens to its preserved state:

  • The standalone VM instance, node-1, preserves its state: all its disks remain attached and metadata, id:xyz273, remains set on the VM.
  • The MIG deletes the managed instance and its preserved state configuration.
  • The MIG deletes the per-instance config for the instance.
  • The stateful policy remains unchanged because it is applicable to all instances in the MIG.

Abandoning an instance from a stateful MIG.

How regional groups handle preserved state

Stateful regional MIGs handle the preserved states of their instances in the same way as zonal MIGs except that regional MIGs create VM instances in multiple zones:

  • When creating instances, a regional MIG evenly distributes VMs across zones in the region to maximize the availability of your app in the event of a zone-level failure.
  • For existing instances, a stateful regional MIG cannot redistribute or move existing VMs across zones automatically because the preserved state is stored in a specific zone and cannot be moved. For that reason, stateful regional MIGs only support an instance redistribution type setting of NONE.

Feedback

We want to learn about your use cases, challenges, and feedback about stateful MIGs. Please share your feedback with our team at mig-discuss@google.com.

What's next