Lesson 7 / 25

Compute Engine and Managed Instance Groups

Choose machine types and run resilient VM fleets.

Virtual machines and fleets of them

Compute Engine provides VMs with your choice of OS. Machine families target different needs: E2 for cost-efficient general purpose, N2/N4/C3/C4 for balanced or high-performance general purpose, compute-optimised, memory-optimised and accelerator (GPU/TPU) families for specialised work, and custom machine types let you pick vCPU and memory exactly. Disks are Persistent Disk or Hyperdisk block storage, zonal or regional. For fleets, write an instance template and create a managed instance group (MIG): it keeps a target number of identical VMs, autoheals them using health checks, autoscales on CPU, load-balancer utilisation or custom metrics, and performs rolling updates to a new template. A regional MIG spreads VMs across zones. Spot VMs offer deep discounts for interruptible work but can be stopped whenever Compute Engine needs the capacity.

A regional managed instance group

One template, many identical VMs across zones, kept healthy and sized by the group.

A template card on the left feeding three zone columns of identical server blocks, with a heart-shaped health check and up-down scaling arrows.
Figure 3.1 — A regional MIG built from an instance template.

Template, regional MIG and autoscaling

The group recreates unhealthy VMs and adds VMs when CPU rises.

gcloud compute instance-templates create web-tpl-v1 \
  --machine-type=e2-medium --image-family=debian-12 --image-project=debian-cloud \
  --metadata-from-file=startup-script=startup.sh --tags=web

gcloud compute instance-groups managed create web-mig \
  --template=web-tpl-v1 --size=2 --region=asia-south1

gcloud compute instance-groups managed set-autoscaling web-mig --region=asia-south1 \
  --min-num-replicas=2 --max-num-replicas=8 --target-cpu-utilization=0.65

Treat VMs as cattle

Configure VMs from the template and a startup script or image, not by SSHing in and editing files. A MIG will recreate VMs during autohealing and updates, and hand-made changes disappear.

Quick check: What does a managed instance group do when a VM fails its health check?

  • Recreates the VM from the instance template
  • Sends an email and waits
  • Deletes the whole group
  • Converts it to a Spot VM
Answer

Recreates the VM from the instance template — Autohealing recreates unhealthy instances from the template.