Overview and What You Will Learn
In this lab, you will create an Instance Template defining a VM's configuration, build a Managed Instance Group from it, configure an autoscale policy watching CPU utilization, and simulate load to watch the group actually add capacity automatically.
Why This Matters in Production
During a flash sale, an e-commerce app needs to handle a sudden spike in traffic without the engineering team manually launching new VMs at 2 AM, and without paying for that same peak capacity to sit idle for the rest of the year. A properly configured Managed Instance Group with autoscaling handles both sides of this problem automatically.
Core Principles
A Managed Instance Group (MIG) manages a group of identical VMs created from an Instance Template as a single unit - GCP's equivalent of an AWS Auto Scaling Group or Azure VM Scale Set. Instead of manually deciding when to add or remove capacity, an autoscale policy watches a metric and adjusts instance count on its own.
+------------------------------------------+| Instance Template (defines VM config: || machine type, image, startup script) |+------------------------------------------+ | v+------------------------------------------+| Managed Instance Group || Creates identical VMs from the template |+------------------------------------------+ | v+------------------------------------------+| Autoscaler watches a metric (e.g. CPU) || Adds or removes instances automatically || within configured min/max bounds |+------------------------------------------+Detailed Step-by-Step Practical Lab
- Create a project and enable Compute Engine:
gcloud projects create gcp-mig-lab-2026 --name="MIG Lab"gcloud config set project gcp-mig-lab-2026gcloud services enable compute.googleapis.com- Create an Instance Template defining the VM configuration:
gcloud compute instance-templates create web-server-template \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud- Create a Managed Instance Group using that template, starting with 2 instances:
gcloud compute instance-groups managed create web-server-mig \ --template=web-server-template \ --size=2 \ --zone=asia-south1-a- Confirm both instances are running:
gcloud compute instance-groups managed list-instances web-server-mig \ --zone=asia-south1-a- Configure autoscaling based on CPU utilization, with a minimum of 2 and a maximum of 10 instances:
gcloud compute instance-groups managed set-autoscaling web-server-mig \ --zone=asia-south1-a \ --max-num-replicas=10 \ --min-num-replicas=2 \ --target-cpu-utilization=0.7 \ --cool-down-period=90Note
--cool-down-period=90gives each newly created instance 90 seconds before its metrics are factored into scaling decisions - this prevents the autoscaler from reacting to a brand-new instance's initial startup CPU spike as if it were sustained load, avoiding an unnecessary premature scale-out decision.
- Simulate load on the instances (using a tool like
stressinstalled via SSH) and watch the instance count grow:
gcloud compute instance-groups managed list-instances web-server-mig \ --zone=asia-south1-a## Re-run this periodically during the load test to see new instances appear- Check the autoscaler's recent scaling decisions and reasoning:
gcloud compute operations list \ --filter="targetLink~'web-server-mig'"- Clean up:
gcloud compute instance-groups managed delete web-server-mig --zone=asia-south1-a --quietgcloud compute instance-templates delete web-server-template --quietgcloud projects delete gcp-mig-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeSetting the cool-down period too short for an application with a genuinely slow startup process. If new instances take 60 seconds to become fully ready, but the cool-down period is only 30 seconds, the autoscaler may factor in misleading startup-phase metrics and make an inappropriate scaling decision.
TipCombine a Managed Instance Group with a health check tied to a Load Balancer's backend service, so unhealthy instances are automatically recreated - not just scaled based on load, but actively self-healed when an individual instance stops responding correctly.
- Instance Templates are immutable once created - updating a VM's configuration means creating a new template version and performing a rolling update on the Managed Instance Group, rather than editing the existing template in place.
- Regional Managed Instance Groups spread instances across multiple zones within a region automatically, providing resilience against a single zone failure - a zonal MIG, by contrast, keeps every instance in one zone, which is simpler but has no such built-in protection.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute instance-templates create |
Define a reusable VM configuration |
gcloud compute instance-groups managed create |
Create a MIG from a template |
gcloud compute instance-groups managed set-autoscaling |
Configure autoscaling rules |
gcloud compute instance-groups managed list-instances |
List current instances in a MIG |