A managed instance is a data entity that represents the intended state for an actual virtual machine (VM) instance in a managed instance group (MIG). Read this document to learn how to reliably work with the managed instances in a MIG. For example, you might need to add, remove, update, or get information about a specific instance.
To ensure that your configuration changes aren't reverted by the MIG, it's important to use the methods that are described in this document. If you work outside the group's methods, the MIG will detect changes and might attempt to repair the group. For example, if you delete an instance in a group, the MIG might automatically recreate that instance according to the managed instance's specification.
Before you begin
- If you want to use the command-line examples in this guide:
- Install or update to the latest version of the gcloud command-line tool.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
- Create an instance group based on an instance template.
What is a managed instance?
A managed instance is a data entity within a MIG that contains the current status and intended state for an actual VM instance.
The current status of a managed instance includes the following fields:
- Instance lifecycle status—for
example,
RUNNING
,STOPPING
. - Current action
performed by the MIG on an instance—for example:
RESTARTING
,VERIFYING
,NONE
. - Instance
health state—for
example,
HEALTHY
,UNHEALTHY
.
A managed instance's intended state for an actual VM instance includes the following:
- Intended version, that is, the version name and instance template to be used for the instance.
- Preserved state of items (such as disks or metadata) to be preserved.
As shown in figure 1, for each managed instance in a MIG, the MIG keeps an actual VM up and running according to the managed instance's specification.
Figure 1. Relationship between managed instances and corresponding VMs.
To view the specifications of a managed instance, see Getting information about the managed instances in a MIG.
Adding instances to a MIG
To add more instances to a MIG, you can:
- Manually set the size of the MIG.
- Use autoscaling for stateless applications.
- Create instances with specific names.
To verify that newly added instances are up and running, verify the status of the group or verify the status of the managed instances.
Manual resizing
If a managed instance group isn't already set to automatically scale, you can resize the group manually to change the number of instances in the group. See Manually resizing a MIG.
Autoscaling
You can configure managed instance groups to automatically add or remove instances based on their workloads. Your applications can gracefully handle increases in traffic, and you can reduce your costs when the need for compute resources is lower. For more information about automatically scaling your MIG, see Autoscaling groups of instances.
Adding instances with specific names
If you have a system that depends on specific names, use the
gcloud
tool
or API
to add VMs with specific names to an
existing
MIG.
The names that you assign to these managed instances persist if the MIG recreates the VM. For more information about preserving the state of MIG instances, see stateful MIGs.
Limitations
If you create instances with specific names:
- You must turn off autoscaling during the instance creation process.
- You must set the MIG's update policy to be opportunistic.
- If you are updating a regional MIG:
- You must disable proactive instance redistribution.
- You can't select which zone to create the VM in.
- You can't use duplicate VM instance names within a project.
gcloud
Use the create-instances
subcommand:
gcloud compute instance-groups managed create-instance instance-group-name \ --instance managed-instance-name \ [--region region | --zone zone]
Replace the following:
- instance-group-name is the name of the regional or zonal MIG to add a new instance to.
- managed-instance-name is the name you want to assign to the new instance.
API
In the API, make a POST request to the
regionInstanceGroupManagers.createInstances
method. For a zonal managed instance group, use the
instanceGroupManagers.createInstances
method. In the request body, include the instances
field with names for
one or more instances.
POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/instanceGroupManagers/instance-group-name/createInstances { "instances": [ { "name": "instance-name-1" }, { "name": "instance-name-2" } ] }
Updating selected instances in a group
See Rolling out updates to MIGs.
Deleting instances from a MIG
When you delete managed instances, the MIG reduces the
targetSize
of the group and deletes the corresponding VM instances. The MIG
removes the instances from any
target pools
of which they are a member.
Deleting instances from a managed instance group doesn't change any specified autoscaler settings. If you delete instances from a managed instance group, the autoscaler might detect an increase in the workload on the other instances in the group and increase the group size back to its previous level. To prevent this, stop the autoscaler before attempting to delete the instances.
If the group is part of a backend service that has enabled connection draining, it can take up to an additional 60 seconds after the connection draining duration has elapsed before the VM instance is removed or deleted.
To delete a managed instance, and therefore delete its corresponding VM instance, you can:
- Resize the MIG. The MIG chooses which instances to delete.
- Manually resize the MIG.
- Use autoscaling for stateless applications.
- Delete the whole group.
- Delete or abandon specific instances explicitly.
Deleting specific instances
You can delete specific instances in a managed instance group. Deleting
instances reduces the specified targetSize
of the instance group and removes
the instances from any
target pools
of which they are a member. If you set up a
stateful MIG, then the MIG also
deletes the instances' preserved state configurations.
Deleting instances from a managed instance group doesn't change any specified autoscaler settings. If you delete instances from a managed instance group, the autoscaler might detect an increase in the workload on the other instances in the group and increase the group size back to its previous level. To prevent this, stop the autoscaler before attempting to delete the instances.
If the group is part of a backend service that has enabled connection draining, it can take up to an additional 60 seconds after the connection draining duration has elapsed before the VM instance is removed or deleted.
Delete instances from a managed instance group using the
Google Cloud Console, the
gcloud
command-line tool, or the
API.
Console
- In the Cloud Console, go to the Instance Groups page.
- Under the Name column of the list, click the name of the instance group where you want to delete individual instances. A page opens with the instance group properties and a list of instances that are included in the group.
- On the list of instances, select one or more instances that you want to delete.
- Click Delete. The selected instances are deleted.
gcloud
To delete an instance with the gcloud
tool, use the
instance-groups managed delete-instances
subcommand:
gcloud compute instance-groups managed delete-instances instance-group-name \ --instances example-i3n2,example-z2x9 \ [--region region | --zone zone]
API
In the API, make a POST request to the
regionInstanceGroupManagers.deleteInstances
method. For a zonal managed instance group, use the
instanceGroupManagers.deleteInstances
method.
POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/instanceGroupManagers/instance-group-name/deleteInstances { "instances": [ "zones/zone/instances/example-instance-i3n2", "zones/zone/instances/example-instance-l6n1" ] }
After you make a request to delete instances in a managed instance group, the instances stop as soon as the system can delete them. This process can take a significant amount of time depending on the number of instances that you delete from the group. Verify the status of the group or verify the status of the managed instances.
Abandoning instances from a MIG
You can separate a VM instance from a managed instance group to more easily debug issues with individual instances without affecting the group as a whole. Abandoning an instance from a group also removes the instance from load balancers that were assigned to the managed instance group. Target pools that were manually assigned to specific individual instances are not removed.
Abandoning instances reduces the specified targetSize
of the instance group,
but doesn't change any specified autoscaler settings. Managed instance groups
with an autoscaler continue to add or remove instances automatically as
necessary.
If the group is part of a backend service that has enabled connection draining, it can take up to an additional 60 seconds after the connection draining duration has elapsed before the VM instance is removed or deleted.
Abandon instances from a managed instance group using the
Google Cloud Console, the
gcloud
tool, or the
API.
Console
- In the Cloud Console, go to the Instance Groups page .
- Under the Name column of the list, click the name of the instance group from which you want to remove instances. A page opens with the instance group properties and a list of instances that are included in the group.
- On the list of instances, select one or more instances that you want to remove from the group.
- Click Remove from group. The selected instances leave the group, but continue to run outside of the group.
gcloud
To remove an instance from the instance group
without deleting the VM, use the
abandon-instances
subcommand.
gcloud compute instance-groups managed abandon-instances instance-group-name \ --instances example-i3n2,example-z2x9 \ [--region region | --zone zone]
API
In the API, make a POST request to the
regionInstanceGroupManagers.abandonInstances
method. For a zonal managed instance group, use the
instanceGroupManagers.abandonInstances
method.
POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/instanceGroupManagers/instance-group-name/abandonInstances { "instances": [ "zones/zone/instances/example-instance-i3n2", "zones/zone/instances/example-instance-l6n1" ] }
After you make a request to abandon instances from a managed instance group, the group removes the instances as soon as possible. Verify the status of the group or verify the status of the managed instances.
Recreating instances in a MIG
Recreating an instance deletes the specified VM and creates a new VM using the configuration that is assigned to the managed instance.
Use this method to update selected VMs so that they use the latest instance template and stateful configuration, if any. If you need to recreate all of the VMs in a managed instance group, start a rolling update instead.
If the group is part of a backend service that has enabled connection draining, it can take up to an additional 60 seconds after the connection draining duration has elapsed before the VM instance is removed or deleted.
Recreate selected VM instances in managed instance group using the
gcloud
tool,
or the
API.
gcloud
Use the
instance-groups managed recreate-instances
subcommand.
gcloud compute instance-groups managed recreate-instances instance-group-name \ --instances example-i3n2,example-z2x9 \ [--region region | --zone zone]
API
In the API, make a POST request to the
regionInstanceGroupManagers.recreateInstances
method. For a zonal managed instance group, use the
instanceGroupManagers.recreateInstances
method.
POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/instanceGroupManagers/instance-group-name/recreateInstances { "instances": [ "zones/zone/instances/example-instance-i3n2", "zones/zone/instances/example-instance-l6n1" ] }
After you make a request to recreate VM instances in a managed instance group, the new VMs start as soon as the system can provision them. This process can take a significant amount of time depending on the number of instances that you recreate. Verify the status of the group or verify the status of the managed instances.
Getting information about instances in a MIG
See Getting info about the managed instances in a MIG.
Configuring stateful instances in a MIG
For more information, see Configuring stateful MIGs.
What's next
- Learn about Rolling out updates to managed instance groups.
- Learn about Applying machine type recommendations for MIGs.
- Try a tutorial: