10 min read

    12 - Managed Instance Groups and Autoscaling

    gcpcloudcomputeautoscaling

    Welcome to Day 12 of Learn GCP in 30 Days! Yesterday, you learned how to pick cost-effective machine sizes and save 90% with Spot VMs. Today, we learn how to make your application un-crashable using GCP Managed Instance Groups (MIGs) and Autoscaling.

    🎯

    Today's Goal Today, you will learn how Google Cloud automatically replaces crashed servers (Autohealing) and automatically adds extra servers when thousands of customers visit your site, then removes them when traffic slows down (Autoscaling)!


    The Core Problem: Single Server Crashes & Traffic Spikes

    Imagine you launch an online store. On a normal day, 100 customers visit your website. But suddenly, an influencer posts a video about your product, and 50,000 customers rush to your site at once!

    What Existed Previously:

    In the past, companies ran their website on 1 single physical server computer.

    If that 1 server crashed at 2:00 AM, the entire website went offline until a human technician woke up, drove to the office, and manually fixed the hardware. Or, if 50,000 users visited at once, the single server ran out of memory and froze completely!

    Problems Faced:

    • πŸ’₯ Single Point of Failure: Running 1 standalone server means a single software glitch or hardware failure destroys your entire business.
    • 😱 Impossible Manual Scaling: Human engineers cannot buy, plug in, and configure 20 extra computers in 2 minutes during a sudden viral traffic surge.
    • πŸ’Έ Wasted Money Off-Peak: Paying for 20 servers 24 hours a day when you only need 1 server at night wastes thousands of dollars.

    How Present Technology Solves It:

    Google Cloud provides Managed Instance Groups (MIGs) & Autoscaling:

    1. Instance Template: A master blueprint cookie-cutter stamp of your server (CPU, RAM, OS, website code).
    2. Managed Instance Group (MIG): A team of identical clone servers built from your Instance Template.
    3. Autohealing (Health Checks): GCP constantly checks your servers' heartbeats. If Server A crashes, GCP automatically deletes the broken server and creates a fresh clone in seconds!
    4. Autoscaling: Automatically adds extra servers when CPU load crosses e.g. 60%, and shrinks back down when traffic leaves!
    mermaid

    Real-World Analogy: The Supermarket Manager & Checkout Lanes

    Imagine managing a busy Supermarket:

    mermaid
    • πŸ“‹ Instance Template = Cashier Lane Blueprint: The exact cash register, scanner, and desk setup used for every cashier lane.
    • πŸͺ Managed Instance Group (MIG) = The Row of Lanes: A group of identical checkout lanes working together.
    • 🩺 Autohealing = Security Guard: If a cashier faints or stops working, the guard immediately calls a fresh replacement cashier to open the lane.
    • πŸ“ˆ Autoscaling = Supermarket Manager: When 100 customers queue up, the manager opens 5 extra checkout lanes automatically! When the crowd leaves at night, the manager closes the extra lanes so you don't waste money paying extra salaries!

    πŸ”‘ Everyday Cloud Terms Explained in Plain English

    Let's break down 5 essential high-availability terms into everyday concepts:


    1. Instance Template (The Master Cookie Cutter)

    An Instance Template is a read-only blueprint configuration.

    • It defines the exact machine size (e2-micro), region (asia-south1), boot disk OS (Debian Linux), firewall tags (http-server), and startup script (Nginx).
    • Key Rule: You cannot launch or SSH directly into a templateβ€”it is just the saved recipe used to bake identical server clones!
    πŸ’°

    Does creating an Instance Template cost money? No! Instance Templates are 100% FREE ($0.00). An Instance Template is simply a saved text blueprint configuration stored in GCP. You are only billed when a Managed Instance Group actually uses that template to launch running VM servers!


    2. Managed Instance Group (MIG)

    A Managed Instance Group (MIG) is a pool of virtual machines created from the same Instance Template that are managed as a single team.

    • If you need 5 identical web servers, you don't create them 1 by 1. You tell the MIG: "Keep 5 servers running," and it manages all 5 automatically!

    3. Autohealing & Health Checks (The Digital Heartbeat)

    A Health Check is a periodic test (e.g. every 5 seconds) where GCP sends a quiet message to Door 80 of your web server: "Are you alive?"

    • If your web server responds HTTP 200 OK, it is healthy.
    • If your web server crashes or freezes (fails 3 checks in a row), Autohealing triggers! GCP automatically deletes the frozen VM and provisions a brand-new identical VM from the template.

    4. Autoscaling Policy (Automatic Growth & Shrinkage)

    An Autoscaling Policy contains the rules that tell GCP when to add or remove servers:

    • Min Instances: Minimum number of servers to keep running during quiet hours (e.g. 1 VM).
    • Max Instances: Maximum limit of servers allowed during a massive rush (e.g. 5 VMs).
    • Target CPU Metric: If average CPU usage across servers exceeds 60%, GCP adds another server. When CPU drops below 60%, GCP safely deletes the extra server.

    5. Managed vs. Unmanaged Instance Groups

    • Managed Instance Groups (MIGs): Made of identical clone VMs created from a template. Supports Autohealing, Autoscaling, and rolling updates. (100% recommended for cloud apps!).
    • Unmanaged Instance Groups: A collection of different, non-identical VMs grouped together. Does NOT support autoscaling or autohealing.

    πŸ§ͺ Hands-on Lab Activity: Creating an Autoscaling MIG

    You can create an Instance Template and Managed Instance Group using either the Web Console Website (UI) or Cloud Shell (CLI)!


    Activity 1: Create an Instance Template (my-app-template)

    Method A: Web Console Website (UI Click-by-Click)

    1. Log into console.cloud.google.com.
    2. Open left menu ☰ β†’\rightarrow Click Compute Engine β†’\rightarrow Click Instance templates.
    3. Click Create Instance Template at the top.
    4. Fill in basic details:
      • Name: my-app-template
      • Location: Global
      • Machine Configuration: Series E2, Machine type e2-micro.
    5. Open Firewall Port 80:
      • Click Networking in the left sub-menu β†’\rightarrow Under Firewall, check Allow HTTP traffic.
    6. Paste Startup Script:
      • Click Advanced options β†’\rightarrow Management (or Automation).
      • Under Startup script (or Metadata Key = startup-script), paste:
        bash
        #!/bin/bash
        apt-get update
        apt-get install -y nginx
        echo "<h1>Hello World from Managed Instance Group in Mumbai!</h1>" > /var/www/html/index.html
        
    7. Click Create!

    Method B: Cloud Shell CLI (Command Terminal)

    1. Click Activate Cloud Shell (>_) at the top right of your GCP Console.
    2. Run this command to create the master blueprint:
      bash
      gcloud compute instance-templates create my-app-template \
          --region=asia-south1 \
          --machine-type=e2-micro \
          --tags=http-server \
          --metadata=startup-script='#!/bin/bash
      apt-get update
      apt-get install -y nginx
      echo "<h1>Hello World from Managed Instance Group in Mumbai!</h1>" > /var/www/html/index.html'
      

    Activity 2: Create a Managed Instance Group with Autoscaling

    Method A: Web Console Website (UI Click-by-Click)

    1. In Compute Engine menu, click Instance groups.
    2. Click Create Instance Group at the top.
    3. Select New managed instance group (stateless).
    4. Fill in details:
      • Name: my-mig
      • Instance template: Select my-app-template.
      • Location: Single zone β†’\rightarrow Region asia-south1 (Mumbai), Zone asia-south1-a.
    5. Autoscaling Configuration:
      • Minimum number of instances: 1
      • Maximum number of instances: 3
      • Autoscaling signal: CPU utilization β†’\rightarrow Target CPU 60%.
    6. Click Create! In 30 seconds, GCP will automatically launch 1 server clone from your template.

    Method B: Cloud Shell CLI (Command Terminal)

    1. Create the Managed Instance Group:
      bash
      gcloud compute instance-groups managed create my-mig \
          --zone=asia-south1-a \
          --template=my-app-template \
          --size=1
      
    2. Enable Autoscaling (Min 1, Max 3, CPU 60%):
      bash
      gcloud compute instance-groups managed set-autoscaling my-mig \
          --zone=asia-south1-a \
          --min-num-replicas=1 \
          --max-num-replicas=3 \
          --target-cpu-utilization=0.60
      

    Activity 3: Inspect Running VMs & Test SSH Access

    1. Go to Compute Engine β†’\rightarrow Instance groups β†’\rightarrow Click my-mig.
    2. Look under the VMs in this group tab. You will see 1 auto-created VM instance (e.g. my-mig-xxxx).
    3. Click the SSH button next to that VM instance to open the terminal inside the server clone!
    4. Type sudo systemctl status nginx to verify Nginx is active (active (running) in green text), then type exit.

    🧹 Clean Up Step (Credit Safety Guarantee)

    Delete your Managed Instance Group and Instance Template so your running costs drop back to $0.00:

    • Via Web Console UI:
      1. Go to Instance groups β†’\rightarrow Select my-mig β†’\rightarrow Click Delete.
      2. Go to Instance templates β†’\rightarrow Select my-app-template β†’\rightarrow Click Delete.
    • Via Cloud Shell CLI:
      bash
      gcloud compute instance-groups managed delete my-mig --zone=asia-south1-a --quiet
      gcloud compute instance-templates delete my-app-template --quiet
      

    Signal vs. Noise: Key Concepts & Noise Filter

    🧠

    Good to Know (Key Concepts)

    • Instance Template: Read-only blueprint recipe for making identical server clones ($0.00 to store).
    • Managed Instance Group (MIG): Pool of identical server clones managed as a single team.
    • Autohealing: Health checks ping servers every few seconds; crashed VMs are replaced automatically.
    • Autoscaling: Adds extra VMs during high CPU traffic (Min 1, Max 3), removes extra VMs when quiet.
    • Rolling Updates: Updating a MIG by replacing VMs one by one with a new template version.
    ℹ️

    Noise Filter (Don't Memorize)

    • Do NOT memorize advanced predictive AI autoscaling algorithmsβ€”standard CPU-based autoscaling is used by 90% of cloud apps.
    • Do NOT try to memorize health check TCP probe packet header formats.

    Common Doubts & Interview Traps

    Q1: Does creating or storing an Instance Template cost money?

    • Answer: No! Instance Templates are 100% free ($0.00). You are only billed for running VM instances created by the MIG using that template.

    Q2: Can I edit an existing Instance Template directly after creating it?

    • Answer: No! Instance Templates are immutable (cannot be changed). To update your website code, you create a new template (my-app-template-v2) and update the MIG to use the new template!

    Q3: What is the difference between Managed Instance Groups (MIGs) and Unmanaged Instance Groups?

    • Answer: MIGs contain identical clone VMs built from a template and support autohealing, autoscaling, and rolling updates. Unmanaged groups contain different, non-identical VMs and cannot autoscale.

    Daily Practice Drill & Self-Check

    Test your understanding of today's lesson:

    text
    // Try answering these:
    1. Does creating or storing an Instance Template cost any money?
    2. What is the master blueprint called that defines the CPU, RAM, OS, and startup script for a Managed Instance Group: Instance Template or Instance Group?
    3. If a server in a MIG freezes and stops responding to health checks, what GCP feature automatically deletes the broken server and creates a fresh clone?
    
    πŸ’‘ Click for Solutions
    1. No! Instance Templates are 100% free ($0.00).
    2. Instance Template! It is the master cookie-cutter recipe used to bake identical VM clones.
    3. Autohealing!

    πŸŽ‰ Awesome job! You have completed Day 12.

    You now understand master Instance Templates, Managed Instance Groups (MIGs), Autohealing, and Autoscaling! Take a break, let today's concepts sink in, and come back tomorrow fresh!

    Tomorrow on Day 13, we will explore Global and Internal Load Balancing so a single website URL can distribute traffic smoothly across your MIG servers!


    ← 11 - Machine Types and Cost Optimization | Next Topic β†’ 13 - Global and Internal Load Balancing