This document helps you decide how to apply updated instance templates and per-instance configurations to the virtual machine (VM) instances in a managed instance group (MIG).
You might want to apply a new instance template for the following reasons:
- To update your application or the operating system on each instance.
- To perform an A/B test to compare different versions of the same application.
- To perform a canary update to minimize disruption when testing a new version.
- To change other specifications for the instances in your MIG, such as the machine type or disk options.
If you use a stateful MIG, you might also want to apply updated per-instance configurations.
After you update a MIG's configuration, you can apply the updates to each VM in the group by using one of the following methods:
Automated rolling update. The MIG automatically rolls out a new version of an instance template to all or to a random subset of managed instances in the MIG. The MIG also applies any unapplied stateful per-instance configurations to corresponding instances. The level of disruption to running instances depends on the update policy that you configure.
Selective update of specific instances. You can specifically target selected instances for an update. Use this method if you want to orchestrate the update manually.
You can also configure your MIG to use the latest instance template and per-instance configurations during a VM repair. For more information, see Apply configuration updates during repairs.
If you only need to resize a MIG, see the documentation for how to add or remove VMs in a MIG.
If you want to use or update MIG features, see the docs for autohealing, load balancing, autoscaling, auto-updating, and stateful workloads.
Limitations
- If you have a stateful MIG and you want to use automated rolling updates, you
must set the
replacement method
to
RECREATE
.
Choosing between automated and selective updates
To automatically roll out new configuration to all or to a subset of the instances
in a MIG, set the MIG's update type to PROACTIVE
. If an automated update is
potentially too disruptive, or you want more control over the update, set the
MIG's update type to OPPORTUNISTIC
then selectively update specific
instances.
Automated, proactive updates
Setting the MIG's update type to PROACTIVE
offers two primary advantages:
- The rollout of an update happens automatically to your specifications, without the need for additional input after the initial request. You can specify the speed of deployment, the level of disruption to your service, and the scope of the update.
- You can automate partial rollouts, which allows for canary testing.
When you start a proactive rolling update, the MIG actively schedules actions to apply the requested updates to instances as necessary. In many cases, this means deleting and recreating instances proactively.
For more information, see Automatically apply VM configuration updates in a MIG.
Selective updates
Selectively updating specific instances offers the following advantages:
- You can select the instances that you want to update.
- You can control the timing and the sequence of the updates.
- You can use the API to perform an update of all instances immediately.
To prevent a proactive update from competing with your selective updates, set
the MIG's update type to OPPORTUNISTIC
.
Opportunistic updates
When the update type is set to OPPORTUNISTIC
, the MIG applies updates only
when you selectively apply the update to specific instances or when new
instances are created by the MIG. A MIG creates new instances when it is resized
to add instances, either automatically or manually. Compute Engine
does not actively initiate requests to apply opportunistic updates.
In certain scenarios, an opportunistic update is useful because you don't want
to cause instability to the system if it can be avoided. For example, if you
have a non-critical update that can be applied as necessary without any urgency
and you have a MIG that is actively being autoscaled, perform
an opportunistic update so that Compute Engine does not actively tear
down your existing instances to apply the update. When resizing down, the
autoscaler preferentially terminates instances with the old template as well as
instances that are not yet in a RUNNING
state.
For more information, see Selectively apply VM configuration updates in a MIG.
Setting up an opportunistic or proactive update
By default, updates initiated by using the Google Cloud console or the Google Cloud CLI are proactive, which means that configuration updates are applied automatically to VMs in the group. Updates initiated by using the Compute Engine API are opportunistic, which means that the MIG does not proactively apply the new template or per-instance configuration to existing instances.
To choose whether an update is opportunistic or proactive, set the mode to opportunistic or proactive by using the Google Cloud console, the Google Cloud CLI, or the Compute Engine API.
Console
In the Google Cloud console, go to the Instance groups page
Select the MIG you want to update.
At the top of the page, click Update VMs.
To set a different template for the group, under New template, select the instance template that you want to use.
Under Update configuration, choose either automatic or selective update.
gcloud
Use the rolling-action start-update
command
and set the --type
flag to opportunistic
or proactive
.
gcloud compute instance-groups managed rolling-action start-update INSTANCE_GROUP_NAME \ --version=template=INSTANCE_TEMPLATE \ --type=opportunistic|proactive \ [--zone=ZONE | --region=REGION]
API
Call the patch
method on a
zonal or
regional
MIG resource.
PATCH https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/instanceGroupManagers/INSTANCE_GROUP_NAME { "updatePolicy": { "type": "TYPE" # Choose an opportunistic or proactive update }, "versions": [{ "instanceTemplate": "global/instanceTemplates/NEW_TEMPLATE", }] }
Replace the following:
NEW_TEMPLATE
: the name of the new template for the groupTYPE
: the type of update,OPPORTUNISTIC
orPROACTIVE
To learn more about setting a new template and then applying that template to new and existing instances in a MIG, see the following pages:
- Automatically apply VM configuration updates in a MIG
- Selectively apply VM configuration updates in a MIG
Relationship between versions
and instanceTemplate
fields
If you use the Compute Engine API, we recommend using the
instanceGroupManagers.versions
and regionInstanceGroupManagers.versions
fields to configure instance templates for zonal and regional MIGs.
The legacy instanceTemplate
field overlaps in functionality with the
versions
field because both fields let you specify which instance template the
MIG uses to create instances. However, only the versions
field lets you
specify an advanced two-template (canary) configuration.
For backward compatibility, MIGs continue to support
setting the top-level instanceTemplate
field, even though we recommend
that you switch to using only the versions
field. Using both the top-level
instanceTemplate
field and the versions
field at the same time can lead to ambiguity and confusion.
If you specify both the instanceTemplate
field and the versions
field when
calling the update()
or patch()
method, there are three possible outcomes:
You set both fields to the same value.
This is a valid request. In this case, it creates no ambiguity, and the new instance template is applied to the MIG.
For example, in the following request, the top-level
instanceTemplate
and theversions
field specify the same instance template which is different from the existing current template, so the MIG is updated to the new instance template:{ "instanceTemplate": "global/instanceTemplates/NEW_TEMPLATE", "versions": [ { "instanceTemplate": "global/instanceTemplates/NEW_TEMPLATE" } ], "updatePolicy": { "type": "PROACTIVE" } }
You set both fields to values that don't match but only one value differs from the current instance template in the MIG.
This is a valid request. The field that's different from the current setting is taken as the intended value. For example, you call the
update()
method and supply both fields, but only one field is updated:{ "instanceTemplate": "global/instanceTemplates/CURRENT_TEMPLATE", "versions": [ { "instanceTemplate": "global/instanceTemplates/NEW_TEMPLATE" } ], "updatePolicy": { "type": "PROACTIVE" } }
You set both fields to values that don't match, and both values differ from the current instance template in the MIG.
This setting is invalid and returns an error because there is no clear intent.
{ "instanceTemplate": "global/instanceTemplates/NEW_TEMPLATE", "versions": [ { "instanceTemplate": "global/instanceTemplates/A_DIFFERENT_NEW_TEMPLATE" } ], "updatePolicy": { "type": "PROACTIVE" } }
The versions
field, instanceTemplate
field, and the get()
method
If you only specify one instance template, either through the top-level
instanceTemplate
field or through the versions
field or through both, the
get()
method returns both fields in its response. This makes the new
versions
field backward compatible. As long as you specify a single instance
template in either of these fields, there is no change to what the get()
method returns in the instanceTemplate
field.
If the versions
field has two instance templates specified, the get()
method
returns an empty top-level instanceTemplate
field. There is no way to
unambiguously express a canary, two-instance template configuration in
the top-level instanceTemplate
field, so the field is not used during a canary
update.
The versions
field and the setInstanceTemplate()
method
For backward compatibility, the setInstanceTemplate()
method behaves as it
has previously, letting you change the template the MIG
uses to create instances. When you call this method, the
versions
field is overridden with the instance template specified by the
setInstanceTemplate()
method.
The setInstanceTemplate()
method also sets the updatePolicy
to
OPPORTUNISTIC
. This
prevents the MIG from actively deploying an instance template
that isn't explicitly specified in the versions
field.
What's next
- Learn more about automatically rolling out a new instance template to a MIG.
- Learn more about selectively updating instances in a MIG.
- Learn about using image families to perform one-click OS upgrades in a MIG.