GCS Stateful Managed Instance Group (MIG) - ghdrako/doc_snipets GitHub Wiki

A managed instance group is a feature of GCE that allows you to group VM instances together as a cluster, and associate autoscaling policies to that group so that new instances can be added (scaled out) or removed (scaled in) as needed. This capability is also referred to as horizontal scalability. You can configure scaling events so that they happen automatically based on load, and they can be one of CPU utilization, load balancing service capacity, or monitoring metrics (which can be any custom monitoring metrics you create). Instance groups can be load balanced with a GCP load balancing service just like VMs can, and, with the load balancing service capacity option as an autoscaling policy, you can define the capacity of an instance in the load balancer's backend service, which can be based on backend utilization or requests per second.

Stateful MIGs

If the application you're working with relies on having the unique state of each instance that hosts it preserved (including things such as instance name, data on attached persistent disks, and metadata) upon a restart, recreation, or update event, then stateful MIGs will have you covered. Instances that run stateful workloads can't simply be discarded and recreated in the same way that stateless MIGs can. Therefore, Google Cloud delivers the stateful MIG as a service that allows you to preserve instance state. Stateful workloads can't be easily scaled horizontally since scaling, in this case, could require data replication, creation or deletion of data shards, or changes to the overall application configuration. However, with stateful MIGs, you can still achieve autohealing features and manual horizontal scaling while preserving state. In addition, you also have the capability of applying controlled updates (rolling updates) to the instances. Examples of stateful applications that could benefit from this include ElasticSearch, Kafka, and Jenkins or database applications such as Cassandra, mongoDB, and MySQL.

Stateful MIGs are not separate services in GCP, but rather, a MIG is considered stateful if you have created a stateful configuration. This means that you can convert a "regular" stateless MIG into a stateful MIG after its creation by adding stateful configuration to it. You do that by setting a non-empty stateful policy and/or one or more non-empty per-instance configs. A stateful policy defines items that you want to preserve for all instances, whereas a per-instance config defines instance-specific items to preserve. GCE will apply your stateful policy configuration to new and existing instances in the MIG automatically, without disrupting running VMs. Converting an existing MIG into a stateful MIG is as simple as running a gcloud command, as follows:

$ gcloud compute instance-groups managed update MIG_NAME \
--stateful-disk device-name=DEVICE_NAME
Stateful MIG Stateless MIG
Workloads Stateful workloads where disks, IP addresses, and/or metadata are preserved on VM recreate operations. Highly available and scalable stateless workloads, where disks and IP addresses are recreated from scratch on horizontal scaling, autohealing, auto-updating, and VM recreation.
MIG features Autohealing; Automated rolling updates; Multi-zone deployments Autohealing; Automated rolling updates; Multi-zone deployments; Autoscaling
Preservable items Instance names; Persistent disks, including support for disks that are not defined in the instance template Instance-specific metadata ; IP addresses (Preview) Instance names

When to use stateful MIGs

Stateful MIGs are intended for applications with stateful data or configuration, such as:

  • Databases: Cassandra, ElasticSearch, mongoDB, and ZooKeeper
  • Data processing applications: Kafka and Flink.
  • Other stateful applications: TeamCity, Jenkins, Bamboo, DNS servers with stateful IP address, and custom stateful workloads.
  • Legacy monolith applications: These applications store application state on a boot disk or additional persistent disks, or they rely on stateful configuration, such as specific VM instance names, IP addresses, or metadata key values.
  • Batch workloads with checkpointing. With this configuration, you can preserve checkpointed results of long-running computation in anticipation of workload or VM failure or instance preemption. Stateful MIGs can recreate a failed machine, while preserving its data disk, so that your computation can continue from the last checkpoint.

Created a stateful configuration

You create a stateful configuration by setting a non-empty stateful policy and/or one or more non-empty per-instance configurations:

  • A stateful policy defines items that you want to preserve for all instances in your MIG.
  • A per-instance configuration defines items to preserve for a specific VM instance.

Stateful policy

Stateful disk

Configuring stateful disks on MIG creation

gcloud compute instance-groups managed create INSTANCE_GROUP_NAME \
    --template INSTANCE_TEMPLATE \
    --size SIZE \
    --stateful-disk device-name=DEVICE_NAME[,auto-delete=DELETE_RULE]

gcloud compute instance-groups managed create example-database-group \
  --template example-database-template-v01 \
  --base-instance-name shard \
  --size 12 \
  --stateful-disk device-name=data-disk,auto-delete=on-permanent-instance-deletion

gcloud compute instance-groups managed describe example-database-group

Setting and updating stateful configuration for disks in an existing MIG

gcloud compute instance-groups managed update NAME \
  --stateful-disk device-name=DEVICE_NAME[,auto-delete=DELETE_RULE]

gcloud compute instance-groups managed update example-database-group \
  --stateful-disk device-name=data-disk,auto-delete=never

gcloud compute instance-groups managed describe example-database-group

Declaring previously stateful persistent disks as stateless

gcloud compute instance-groups managed update NAME \
  --remove-stateful-disks DEVICE_NAME[,DEVICE_NAME,...]
gcloud compute instance-groups managed update example-legacy-group \
  --remove-stateful-disks boot-disk

stateful metadata

Setting stateful metadata on instance creation

gcloud compute instance-groups managed create-instance NAME \
  --instance INSTANCE_NAME \
  --stateful-metadata KEY=VALUE[,KEY=VALUE,...]
gcloud compute instance-groups managed create-instance example-cluster \
  --instance node-12 \
  --stateful-metadata mode=active,logging=elaborate

gcloud compute instance-groups managed instance-configs update NAME \
  --instance INSTANCE_NAME \
  [--stateful-metadata KEY=VALUE[,KEY=VALUE,...]] \
  [--remove-stateful-metadata KEY[,KEY,...]] \
  [--no-update-instance | --update-instance] \
  [--instance-update-minimal-action MINIMAL_ACTION]

gcloud compute instance-groups managed instance-configs update example-cluster \
  --instance node-12 \
  --stateful-metadata mode=standby \
  --remove-stateful-metadata logging

stateful IP addresses

  • For new instances, the MIG automatically assigns and reserves static IP addresses.
  • For existing instances, the MIG promotes in-use ephemeral internal or external IP addresses to static addresses
  • For existing instances without external IP addresses, the MIG assigns and reserves static IP addresses, and adds access configuration to the corresponding network interface
gcloud beta compute instance-groups managed create INSTANCE_GROUP_NAME \
    --template INSTANCE_TEMPLATE \
    --size SIZE \
    --instance-redistribution-type NONE \
    --stateful-internal-ip [enabled | interface-name=NI_NAME][,auto-delete=DELETE_RULE] \
    --stateful-external-ip [enabled | interface-name=NI_NAME][,auto-delete=DELETE_RULE]