0
0
GCPcloud~5 mins

High availability configuration in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
High availability means making sure your app or service keeps working even if some parts fail. It solves the problem of downtime by spreading resources across multiple places so if one fails, others take over.
When you want your website to stay online even if one server crashes
When you run a database that must never lose data or stop responding
When you deploy an app that users rely on 24/7 without interruptions
When you want to avoid losing customer trust due to service outages
When you need to handle sudden spikes in traffic without slowing down
Config File - main.tf
main.tf
provider "google" {
  project = "example-project"
  region  = "us-central1"
}

resource "google_compute_instance_group_manager" "ha_instance_group" {
  name               = "ha-instance-group"
  base_instance_name = "ha-instance"
  zone               = "us-central1-a"
  version {
    instance_template = google_compute_instance_template.ha_template.self_link
  }
  target_size        = 2
  auto_healing_policies {
    health_check      = google_compute_health_check.ha_health_check.self_link
    initial_delay_sec = 300
  }
}

resource "google_compute_instance_template" "ha_template" {
  name         = "ha-instance-template"
  machine_type = "e2-medium"
  disk {
    source_image = "projects/debian-cloud/global/images/family/debian-11"
    auto_delete  = true
    boot         = true
  }
  network_interface {
    network = "default"
    access_config {}
  }
}

resource "google_compute_health_check" "ha_health_check" {
  name               = "ha-health-check"
  check_interval_sec = 5
  timeout_sec        = 5
  healthy_threshold  = 2
  unhealthy_threshold = 2
  tcp_health_check {
    port = 80
  }
}

resource "google_compute_region_backend_service" "ha_backend" {
  name                  = "ha-backend-service"
  region                = "us-central1"
  protocol              = "HTTP"
  health_checks         = [google_compute_health_check.ha_health_check.self_link]
  backend {
    group = google_compute_instance_group_manager.ha_instance_group.instance_group
  }
}

resource "google_compute_url_map" "ha_url_map" {
  name            = "ha-url-map"
  default_service = google_compute_region_backend_service.ha_backend.self_link
}

resource "google_compute_target_http_proxy" "ha_http_proxy" {
  name    = "ha-http-proxy"
  url_map = google_compute_url_map.ha_url_map.self_link
}

resource "google_compute_global_forwarding_rule" "ha_forwarding_rule" {
  name       = "ha-forwarding-rule"
  target     = google_compute_target_http_proxy.ha_http_proxy.self_link
  port_range = "80"
}

This Terraform file creates a high availability setup on GCP.

It defines an instance template for VM setup, then an instance group manager to run two VMs for redundancy.

A health check monitors VM health to replace unhealthy ones automatically.

A backend service connects the instance group to a load balancer.

The URL map and HTTP proxy route traffic to the backend.

The global forwarding rule listens on port 80 and sends traffic to the proxy.

Commands
This command initializes Terraform in the current directory. It downloads the Google provider plugin and prepares Terraform to manage resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/google... - Installing hashicorp/google v4.0.0... - Installed hashicorp/google v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command shows what Terraform will create or change in your GCP project based on the configuration file.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # google_compute_instance_template.ha_template will be created + resource "google_compute_instance_template" "ha_template" { + id = (known after apply) + name = "ha-instance-template" ... } # google_compute_instance_group_manager.ha_instance_group will be created + resource "google_compute_instance_group_manager" "ha_instance_group" { + id = (known after apply) + name = "ha-instance-group" ... } Plan: 7 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
This command applies the configuration, creating all resources in GCP automatically without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
google_compute_instance_template.ha_template: Creating... google_compute_health_check.ha_health_check: Creating... google_compute_instance_template.ha_template: Creation complete after 10s [id=ha-instance-template] google_compute_health_check.ha_health_check: Creation complete after 8s [id=ha-health-check] google_compute_instance_group_manager.ha_instance_group: Creating... google_compute_region_backend_service.ha_backend: Creating... google_compute_instance_group_manager.ha_instance_group: Creation complete after 15s [id=ha-instance-group] google_compute_region_backend_service.ha_backend: Creation complete after 12s [id=ha-backend-service] google_compute_url_map.ha_url_map: Creating... google_compute_target_http_proxy.ha_http_proxy: Creating... google_compute_url_map.ha_url_map: Creation complete after 7s [id=ha-url-map] google_compute_target_http_proxy.ha_http_proxy: Creation complete after 6s [id=ha-http-proxy] google_compute_global_forwarding_rule.ha_forwarding_rule: Creating... google_compute_global_forwarding_rule.ha_forwarding_rule: Creation complete after 5s [id=ha-forwarding-rule] Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This command lists forwarding rules in your GCP project to verify the load balancer is set up and listening on port 80.
Terminal
gcloud compute forwarding-rules list
Expected OutputExpected
NAME REGION IP_ADDRESS IP_PROTOCOL PORT_RANGE TARGET ha-forwarding-rule global 34.123.45.67 TCP 80 target-http-proxy/ha-http-proxy
Key Concept

If you remember nothing else from this pattern, remember: spreading your app across multiple servers with health checks and a load balancer keeps it running even if one server fails.

Common Mistakes
Setting target_size to 1 in the instance group manager
Having only one instance means no backup if it fails, so no real high availability.
Set target_size to at least 2 to have multiple instances for redundancy.
Not configuring a health check for the instance group
Without health checks, unhealthy instances won't be detected or replaced automatically.
Always configure a health check and link it to the instance group manager for auto healing.
Forgetting to create a forwarding rule for the load balancer
Without a forwarding rule, traffic won't reach your load balancer and your app won't be accessible.
Create a global forwarding rule that listens on the desired port and points to the HTTP proxy.
Summary
Initialize Terraform to prepare the environment with 'terraform init'.
Use 'terraform plan' to preview the resources Terraform will create.
Apply the configuration with 'terraform apply -auto-approve' to create a high availability setup.
Verify the load balancer forwarding rule with 'gcloud compute forwarding-rules list' to ensure traffic routing.