Instance Templates, Managed Instance Groups and Autoscaling: Self-Healing, Regional Groups and Rolling Updates
The Idea
One VM can fail or get overloaded. A managed instance group (MIG) runs many identical VMs from a template, replaces broken ones, adds or removes VMs with demand and updates them without downtime.
Instance Template
bash
gcloud compute instance-templates create web-tpl-v1 --machine-type=e2-small --image-family=debian-12 --image-project=debian-cloud \
--tags=http-server --metadata-from-file=startup-script=startup.sh --service-account=web-app@my-proj.iam.gserviceaccount.com --scopes=cloud-platform
- A template is immutable. To change anything, create a new version
- Pre-baking an image with software installed makes new VMs ready faster than long startup scripts
Create the Group
| Type | Spreads over | Use |
|---|---|---|
| Zonal MIG | One zone | Simple or test |
| Regional MIG | Several zones in a region | Production (survives a zone failure) |
bash
gcloud compute health-checks create http web-hc --port=80 --request-path=/health --check-interval=10s --unhealthy-threshold=3
gcloud compute instance-groups managed create web-mig --region=asia-south1 --template=web-tpl-v1 --size=3 \
--health-check=web-hc --initial-delay=120
gcloud compute instance-groups managed set-named-ports web-mig --region=asia-south1 --named-ports=http:80
Autohealing
- A health check probes each VM. If it fails repeatedly, the MIG recreates the VM
- Set an initial delay so new VMs have time to start
- Use an application-level check such as
/health, not just that the VM is on
Autoscaling
| Signal | Example |
|---|---|
| CPU utilization | Keep average CPU near 60 percent |
| Load balancing capacity | Scale on requests per instance |
| Cloud Monitoring metric | Queue length or custom metric |
| Schedule | More VMs on weekday mornings |
| Predictive autoscaling | Scales ahead using past patterns |
bash
gcloud compute instance-groups managed set-autoscaling web-mig --region=asia-south1 \
--min-num-replicas=3 --max-num-replicas=10 --target-cpu-utilization=0.6 --cool-down-period=120
- Cool-down period is how long to wait before trusting a new VM's metrics
- Minimum 3 in a regional MIG keeps at least one per zone
- Scale-in can be slowed with a scale-in control to avoid flapping
Rolling Updates and Canary
bash
gcloud compute instance-templates create web-tpl-v2 --source-instance-template=web-tpl-v1 --image-family=debian-12 --image-project=debian-cloud
# Rolling update to v2, one extra VM at a time, never fewer than all serving
gcloud compute instance-groups managed rolling-action start-update web-mig --region=asia-south1 \
--version=template=web-tpl-v2 --max-surge=1 --max-unavailable=0
# Canary: 20 percent on v2, rest on v1
gcloud compute instance-groups managed rolling-action start-update web-mig --region=asia-south1 \
--version=template=web-tpl-v1 --canary-version=template=web-tpl-v2,target-size=20%
| Setting | Meaning |
|---|---|
max-surge | How many extra VMs may be created during the update |
max-unavailable | How many VMs may be offline during the update |
replacement method | substitute (new VM) or recreate (same name) |
Stateful MIGs and Load Balancing
- Stateful MIGs keep chosen disks, names or IPs when a VM is recreated (for databases or licensed apps)
- A MIG becomes a backend of a load balancer. The balancer checks health and sends traffic only to healthy VMs
- Use connection draining so users finish before a VM is removed
Design Tip
Stateless wins Keep VMs stateless: store sessions in Memorystore, files in Cloud Storage and data in a database. Then any VM can be replaced at any time.
Practice
- Create a template and a regional MIG of 3 VMs
- Stop nginx on one VM and watch autohealing recreate it
- Add CPU load and watch autoscaling add VMs
- Roll out v2 with a 20 percent canary, then complete it
Conclusion
A MIG runs identical VMs from an immutable template, heals them with health checks, scales on load and updates them with rolling or canary strategies. Use regional MIGs and stateless design for production.
Quick Summary: idea; template; group; autohealing; autoscaling; updates; stateful; tip.

