DevOps Peach

GCP Basics · Compute Engine – Machine Types, VMs, Disks and Managed Instance Groups · Lesson 4 of 5

Instance Templates, Managed Instance Groups and Autoscaling

An instance template describes a VM; a regional managed instance group runs identical VMs across zones with autoscaling, autohealing and rolling updates.

Practice interactively →

For anything that should scale or heal itself, do not create VMs one by one. Describe them once in an instance template and let a managed instance group (MIG) run them.

flowchart LR
  T["Instance template web-tpl-v2<br/>e2-medium, image family web"] --> MIG["Regional MIG mig-web<br/>asia-southeast1"]
  MIG --> A["VM in zone a"]
  MIG --> B["VM in zone b"]
  MIG --> C["VM in zone c"]
  AS["Autoscaler: CPU 60%, min 3, max 10"] --> MIG
  HC["Health check /healthz"] --> MIG
  %% step T: The template holds machine type, image, disks, network, service account, tags and metadata; it is immutable.
  %% step MIG: A regional MIG spreads identical VMs across the zones of the region.
  %% step A: If a zone fails, the MIG recreates capacity in the remaining zones.
  %% step AS: The autoscaler adds VMs when average CPU is above the 60% target and removes them when it drops.
  %% step HC: Autohealing recreates VMs that fail the application health check.

Instance templates

MIG features

Feature What it does
Zonal vs regional Regional MIGs spread over up to three zones: prefer them for production
Autoscaling On CPU, load-balancer serving capacity, Cloud Monitoring metrics or schedules; with min/max replicas and a cool-down period
Autohealing Health check with an initial delay, so slow-booting VMs are not killed during startup
Rolling updates Replace VMs with a new template using maxSurge and maxUnavailable
Canary Run two versions, e.g. 10% on the new template
Stateful MIG Keep disks and names for stateful apps
Load balancing A MIG is the usual backend of a backend service

Unmanaged instance groups only group different VMs for load balancing; they do not autoscale or autoheal.

gcloud compute instance-templates create web-tpl-v2 --machine-type=e2-medium   --image-family=web --image-project=dl-lab-dev --tags=web --no-address   --service-account=web-sa@dl-lab-dev.iam.gserviceaccount.com --scopes=cloud-platform
gcloud compute instance-groups managed create mig-web --region=asia-southeast1   --template=web-tpl-v2 --size=3 --health-check=hc-web --initial-delay=120
gcloud compute instance-groups managed set-autoscaling mig-web --region=asia-southeast1   --min-num-replicas=3 --max-num-replicas=10 --target-cpu-utilization=0.6
gcloud compute instance-groups managed rolling-action start-update mig-web --region=asia-southeast1   --version=template=web-tpl-v3 --max-surge=3 --max-unavailable=0
Command Meaning
gcloud compute instance-groups managed list-instances mig-web --region=… VMs and their status
gcloud compute instance-groups managed resize mig-web --size=5 --region=… Fixed size (no autoscaler)
gcloud compute instance-groups managed rolling-action replace mig-web --region=… Recreate all VMs

Traps: an initial delay that is too short causes endless recreate loops; editing a VM inside a MIG is lost when the MIG recreates it; maxUnavailable=0 with maxSurge=0 cannot make progress.

Practice interactively →