Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 45 ยท Compute

Instance Groups, Autoscaling and Load Balancing

Learn instance templates, managed instance groups, autoscaling signals, health checks and load balancer types on Google Cloud, with an autoscaler simulation.

One virtual machine is a single point of failure and a fixed amount of capacity. Real services need many identical machines that grow when traffic rises, shrink when it falls, and replace any machine that breaks. On Google Cloud, the building block for that is the managed instance group (MIG).

In this lesson you will meet the three pieces that work together: an instance template (what a machine looks like), a MIG (how many exist and how they are kept healthy) and a load balancer (how traffic reaches them). The commands are read-only because they need a real project; the TryIt blocks are stand-ins that show the logic inside an autoscaler.

Three pieces of a scalable service

Template A saved recipe for one VM.
Instance group Keeps N identical VMs running.
Load balancer Spreads requests over healthy VMs.

The template describes, the group maintains, the balancer distributes.

1 / 2

Instance templates

An instance template stores everything needed to create a VM: machine type, boot image, disks, network, labels, service account and startup script. Templates are immutable. To change something, you create a new template and point the group at it.

Terminal
gcloud compute instance-templates create web-template-v1 \
  --machine-type=e2-small \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --tags=http-server \
  --metadata=startup-script='#! /bin/bash
apt-get update && apt-get install -y nginx'

A template can be global or regional. A startup script that installs everything on first boot keeps the template small, though baking a custom image makes new machines ready faster.

Managed instance groups

A MIG creates VMs from a template and keeps the group at its target size. It can be zonal (one zone) or regional (spread across several zones in a region). Regional groups survive the loss of a whole zone, so they are the usual choice for production.

Terminal
gcloud compute instance-groups managed create web-mig \
  --template=web-template-v1 \
  --size=3 \
  --region=us-central1

What a MIG gives you:

  • Autohealing. If a VM fails its health check, the group recreates it.
  • Autoscaling. The group size follows a signal.
  • Rolling updates. Switch to web-template-v2 a few machines at a time, with limits on how many may be unavailable.
  • Even spread across zones for regional groups.

Because machines can be deleted and recreated at any time, they must be stateless: keep user data in a database or storage bucket, never on the VM's own disk.

Health checks

A health check probes each VM on a port and path, for example GET /healthz over HTTP every 10 seconds. A VM that fails several checks in a row is unhealthy. Two different systems use the result: the load balancer stops sending traffic to unhealthy backends, and autohealing recreates the VM. Give autohealing an initial delay longer than your boot time, or the group may keep killing machines that are simply still starting.

Terminal
gcloud compute health-checks create http web-check \
  --port=80 --request-path=/healthz \
  --check-interval=10s --unhealthy-threshold=3

Autoscaling signals

An autoscaler adds or removes VMs between a minimum and a maximum. You choose the signal:

Signal Meaning
CPU utilization Keep average CPU near a target such as 60%
Load balancing capacity Keep serving load under a share of what the balancer says each backend can take
Cloud Monitoring metric A custom or built-in metric, such as queue length
Schedule A minimum size during known busy hours
Terminal
gcloud compute instance-groups managed set-autoscaling web-mig \
  --region=us-central1 \
  --min-num-replicas=2 --max-num-replicas=10 \
  --target-cpu-utilization=0.6 \
  --cool-down-period=90

The cool-down period tells the autoscaler how long a new VM needs to start before its numbers count. Without it, the group would see high CPU from machines that are still booting and overshoot. Scaling out is quick; scaling in is deliberately slower, to avoid flapping.

A simplified autoscaler

The real algorithm has more safeguards (stabilization windows, predictive options, per-zone handling). The core idea is a proportional rule: if average CPU is above the target, grow the group by the same ratio. This is a stand-in for that idea, not Google's implementation.

JS Stand-in: proportional CPU autoscaler

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that when load jumps, one CPU reading is capped at 100%, so the group needs a couple of rounds to catch up. Real groups behave the same way, which is why a sensible minimum size and a lower target (60%, not 95%) leave headroom for spikes.

Load balancer types at a glance

A MIG gives you many VMs; a load balancer gives clients one address. Google Cloud has several families, and the choice follows from the protocol and where clients are:

JS Stand-in: pick a load balancer type

Press Run (or Ctrl + Enter) to see the console output here.

No output.

This is a deliberately small decision table. The real products differ in many details (TLS handling, ports, Cloud CDN support), so check the current comparison page before choosing. Load balancing, URL maps and CDN get a full treatment in Load Balancing, Cloud CDN and DNS.

Create firewall rules that allow the load balancer's health-check ranges to reach your VMs. A very common first failure is a perfectly healthy server that is marked unhealthy because the probe is blocked.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should VMs in a managed instance group be stateless?

    Autohealing, autoscaling and rolling updates all replace machines, so durable data belongs in a database or bucket.
  2. 2. What does the autoscaler cool-down period protect against?

    While a new VM starts, its metrics are not representative. The cool-down period ignores them so the group does not overshoot.
  3. 3. An instance template needs to change its machine type. What do you do?

    Instance templates are immutable, so a change means a new template and a rolling update of the group.
  4. 4. Which group type survives the loss of one zone?

    A regional MIG distributes VMs over several zones of a region, so the other zones keep serving.

Summary

  • An instance template is an immutable recipe; a MIG keeps a number of VMs created from it.
  • Regional MIGs spread VMs across zones; keep them stateless.
  • Health checks drive both load balancer routing and autohealing.
  • Autoscalers follow CPU, load balancer capacity, a monitoring metric or a schedule, within a minimum and maximum.
  • Choose the load balancer by protocol and client location.

Next: App Engine