Compute Engine and Instance Groups
VMs, disks and autoscaling groups
Compute Engine is GCP's IaaS: machine families, images, disks and instance groups with autoscaling. Managed instance groups autoheal and spread across zones; autoscaling scales on utilization.
What you will learn
- Pick a machine family and shape
- Use managed instance groups with health checks
- Configure autoscalers and spot/preemptible usage
Compute Engine VMs are virtual machines; machine families (E2, N2, C3, M3, GPU) tune cost vs resources. Managed instance groups auto-create/replace instances from an instance template and autoscale on utilization.
What is this?
Compute Engine lets you run virtual machines driven by a machine type (vCPU+RAM), boot/attached disks (zonal or regional persistent, SSD, local SSD), an image (Linux/Windows/custom), and network config. Instance templates define a VM spec; Managed Instance Groups (MIGs) keep the desired count, autoheal unhealthy instances, and spread across zones (regional MIGs). Autoscalers add/remove instances based on CPU, load balancer utilization or custom metrics. Spot/preemptible VMs give big discounts for interruptible work.
Why do you need it?
- MIGs replace the VM-as-a-pet problem: define the template, the group maintains the herd.
- Autoscaling unit economics beat over-provisioning when traffic is spiky.
- Spot/ preemptible VMs are the cheapest compute path for batch, CI and stateless scale-out.
- Regional disks + regional MIGs get real HA without a cluster stack.
Mental model
A trucking company with a fleet policy
Compute Engine + MIG
One truck (VM) you drive yourself. A fleet (MIG): the yard keeps a set number of trucks by model (template), rotates out broken ones (autoheal), and dispatches more during peak (autoscale). Spot trucks are the cheap booking for non-critical hauls.
How it works
- Machine type fixes baseline vCPU/RAM; families trade balance (E2) vs compute (C3) vs memory (M3).
- An instance template defines image/disk/network/service account — the immutable blueprint.
- MIG creates instances from the template; a health check marks instances unhealthy → replacement.
- Autoscaler watches utilization metrics and adjusts group size between min/max.
- Spot VMs are cheaper but preemptible; design retries and resumable work for them.
MIG lifecycle with autoscaler.
In production
- A gaming backend runs a regional MIG across 3 zones behind a global LB with autoscaling on request rate.
- A batch platform escalates to spot VMs for nightly ETL, retrying interleps on preemption.
- A CI farm uses a MIG with a custom image baking pipelines, autohealing dead agents.
Best practices
- Model instances as templates + MIGs, never flip individual VMs by hand.
- Right-size machine type before autoscaling — scaling a fat VM is waste.
- Use regional MIGs + health checks for zone-failure tolerance.
- Reserve leader/traffic-sensitive work for regular VMs; push stateless batch onto spot.
Common mistakes
- Autoscaling with no max → surprise bill on a traffic spike.
- No health check → the group keeps serving unhealthy instances.
- Using preemptible VMs for stateful or deadline-bound work.
- Template drift — hand-edited VMs ignore the template, and group replace reverts your fixes.
Common issues
MIG can't create the desired countRegion/zone quota, or the template references removed resources
Check quota (instance/CPU) and that the template's disk/image still exists.
Instances cycling in a restart loopHealth check failing due to port/path mismatch
Verify the app listens on the health-check path; review the health check's port & proxy protocol.
Autoscaler removes instances during a spikeAggregation/cooldown settings cause early scale-in
Lengthen scale-in cooldown and adjust over-utilization window before it cuts.
Debugging tips
- MIG not full? Check instance template validity and quota; read group status and instance health.
- Autoscale stall: verify the metric, target utilization and min/max values.
- Vm count jumps? Look at autohealing replacements vs autoscaler churn (cooldowns).
- Preemption storms: use consumption/spot config and retry loops for the interrupted batch.
Interview checkpoint
Hands-on lab
- Create an instance template
- Create a managed instance group with a health check
- Add an autoscaler and observe desired size
Create a MIG Behind a Health Check (or Plan)
35 min · intermediate
Quick quiz
What keeps a managed instance group's instance count at its target?
Spot VMs are cheaper than on-demand but can be reclaimed at short notice.