0
0
GCPcloud~5 mins

Managed instance groups in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to run many copies of the same virtual machine to handle more users or tasks, managed instance groups help by automatically creating, updating, and fixing these copies for you.
When you need to run multiple identical servers to share the work of a busy website.
When you want your servers to automatically restart if they crash or become unhealthy.
When you want to update all your servers with new software without downtime.
When you want to automatically add or remove servers based on how busy your app is.
When you want Google Cloud to handle balancing traffic between your servers.
Config File - instance_group.yaml
instance_group.yaml
apiVersion: compute.googleapis.com/v1
kind: InstanceGroupManager
metadata:
  name: example-instance-group
  region: us-central1
spec:
  baseInstanceName: example-instance
  instanceTemplate: projects/my-project/global/instanceTemplates/example-template
  targetSize: 3
  autoHealingPolicies:
  - healthCheck: projects/my-project/global/healthChecks/example-health-check
    initialDelaySec: 300
  updatePolicy:
    type: PROACTIVE
    minimalAction: RESTART
    maxSurge: 1
    maxUnavailable: 1

This file defines a managed instance group named example-instance-group in the us-central1 region.

baseInstanceName sets the prefix for VM names.

instanceTemplate points to the VM setup to use for each instance.

targetSize tells how many VM copies to run.

autoHealingPolicies uses a health check to restart unhealthy VMs after 5 minutes.

updatePolicy controls how updates happen, restarting one VM at a time to avoid downtime.

Commands
This command creates a managed instance group named example-instance-group with 3 instances based on the example-template in the us-central1 region.
Terminal
gcloud compute instance-groups managed create example-instance-group --base-instance-name example-instance --size 3 --template example-template --region us-central1
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/regions/us-central1/instanceGroupManagers/example-instance-group].
--base-instance-name - Sets the prefix for the names of the VM instances.
--size - Specifies how many VM instances to run.
--template - Specifies the VM template to use for instances.
This command lists all managed instance groups in the us-central1 region to verify the group was created.
Terminal
gcloud compute instance-groups managed list --region us-central1
Expected OutputExpected
NAME REGION BASE_INSTANCE_NAME SIZE example-instance-group us-central1 example-instance 3
--region - Limits the list to the specified region.
This command starts a rolling update to replace instances with a new template version example-template-v2, updating one instance at a time.
Terminal
gcloud compute instance-groups managed rolling-action start-update example-instance-group --version template=example-template-v2 --region us-central1
Expected OutputExpected
Started rolling update for instance group [example-instance-group].
--version - Specifies the new instance template version to use.
This command lists all instances in the managed instance group to check their status after the update.
Terminal
gcloud compute instance-groups managed list-instances example-instance-group --region us-central1
Expected OutputExpected
INSTANCE_NAME ZONE STATUS example-instance-abc us-central1-a RUNNING example-instance-def us-central1-b RUNNING example-instance-ghi us-central1-c RUNNING
--region - Specifies the region of the instance group.
Key Concept

If you remember nothing else from this pattern, remember: managed instance groups keep many copies of your VM running and healthy automatically.

Common Mistakes
Creating an instance group without specifying the instance template.
Without a template, the group does not know what VM setup to use, so creation fails.
Always specify a valid instance template with the --template flag when creating the group.
Not specifying the region or zone when managing regional instance groups.
Commands may fail or affect the wrong resources if the location is not specified.
Always include the --region or --zone flag matching your instance group location.
Updating all instances at once during a rolling update.
This causes downtime because all VMs restart simultaneously.
Use rolling updates with maxSurge and maxUnavailable settings to update instances one at a time.
Summary
Create a managed instance group with a VM template and desired size.
List instance groups to verify creation and status.
Use rolling updates to safely update VM instances without downtime.
List instances in the group to check their running status.