From d2eaa6f8e064f5cc9b7c0b22053b4baa158606ac Mon Sep 17 00:00:00 2001 From: Riley Karson Date: Tue, 16 May 2017 13:16:27 -0700 Subject: [PATCH] provider/google: Google Cloud content-based load balancing example. (#14448) --- .../.gitignore | 3 + .../README.md | 35 ++++ .../main.tf | 159 ++++++++++++++++++ .../output.tf | 3 + .../scripts/install-video.sh | 9 + .../scripts/install-www.sh | 7 + .../terraform.tfvars.example | 4 + .../variables.tf | 16 ++ 8 files changed, 236 insertions(+) create mode 100644 examples/google-content-based-load-balancing/.gitignore create mode 100644 examples/google-content-based-load-balancing/README.md create mode 100644 examples/google-content-based-load-balancing/main.tf create mode 100644 examples/google-content-based-load-balancing/output.tf create mode 100644 examples/google-content-based-load-balancing/scripts/install-video.sh create mode 100644 examples/google-content-based-load-balancing/scripts/install-www.sh create mode 100644 examples/google-content-based-load-balancing/terraform.tfvars.example create mode 100644 examples/google-content-based-load-balancing/variables.tf diff --git a/examples/google-content-based-load-balancing/.gitignore b/examples/google-content-based-load-balancing/.gitignore new file mode 100644 index 000000000..16791642b --- /dev/null +++ b/examples/google-content-based-load-balancing/.gitignore @@ -0,0 +1,3 @@ +terraform.tfstate +terraform.tfstate.backup +terraform.tfvars diff --git a/examples/google-content-based-load-balancing/README.md b/examples/google-content-based-load-balancing/README.md new file mode 100644 index 000000000..54b18e61d --- /dev/null +++ b/examples/google-content-based-load-balancing/README.md @@ -0,0 +1,35 @@ +# Content Based Load Balancing in Google Cloud + +This provides a template for running an HTTP load balancer that distributes traffic to different instances based on the +path in the request URL. It is based on the tutorial at [https://cloud.google.com/compute/docs/load-balancing/http/content-based-example](https://cloud.google.com/compute/docs/load-balancing/http/content-based-example). + +To start, [download your credentials from Google Cloud Console](https://www.terraform.io/docs/providers/google/#credentials); suggested path for downloaded file is `~/.gcloud/Terraform.json`. + +Optionally update `variables.tf` to specify a default value for the `project_name` variable, and check other variables. + +After you run `terraform apply` on this configuration, it will +automatically output the public IP address of the load balancer. +After your instance registers, the LB should respond with the following at its root: + +```html +

www

+``` + +And the following at the /video/ url: +```html +

www-video

+``` + +To run, configure your Google Cloud provider as described in + +https://www.terraform.io/docs/providers/google/index.html + +Run with a command like this: + +``` +terraform apply \ + -var="region=us-central1" \ + -var="region_zone=us-central1-f" \ + -var="project_name=my-project-id-123" \ + -var="credentials_file_path=~/.gcloud/Terraform.json" \ +``` \ No newline at end of file diff --git a/examples/google-content-based-load-balancing/main.tf b/examples/google-content-based-load-balancing/main.tf new file mode 100644 index 000000000..d9934c433 --- /dev/null +++ b/examples/google-content-based-load-balancing/main.tf @@ -0,0 +1,159 @@ +# https://cloud.google.com/compute/docs/load-balancing/http/content-based-example + +provider "google" { + region = "${var.region}" + project = "${var.project_name}" + credentials = "${file("${var.credentials_file_path}")}" +} + +resource "google_compute_instance" "www" { + name = "tf-www-compute" + machine_type = "f1-micro" + zone = "${var.region_zone}" + tags = ["http-tag"] + + disk { + image = "projects/debian-cloud/global/images/family/debian-8" + } + + network_interface { + network = "default" + + access_config { + // Ephemeral IP + } + } + + metadata_startup_script = "${file("scripts/install-www.sh")}" + + service_account { + scopes = ["https://www.googleapis.com/auth/compute.readonly"] + } +} + +resource "google_compute_instance" "www-video" { + name = "tf-www-video-compute" + machine_type = "f1-micro" + zone = "${var.region_zone}" + tags = ["http-tag"] + + disk { + image = "projects/debian-cloud/global/images/family/debian-8" + } + + network_interface { + network = "default" + + access_config { + // Ephemeral IP + } + } + + metadata_startup_script = "${file("scripts/install-video.sh")}" + + service_account { + scopes = ["https://www.googleapis.com/auth/compute.readonly"] + } +} + +resource "google_compute_global_address" "external-address" { + name = "tf-external-address" +} + +resource "google_compute_instance_group" "www-resources" { + name = "tf-www-resources" + zone = "${var.region_zone}" + + instances = ["${google_compute_instance.www.self_link}"] + + named_port { + name = "http" + port = "80" + } +} + +resource "google_compute_instance_group" "video-resources" { + name = "tf-video-resources" + zone = "${var.region_zone}" + + instances = ["${google_compute_instance.www-video.self_link}"] + + named_port { + name = "http" + port = "80" + } +} + +resource "google_compute_health_check" "health-check" { + name = "tf-health-check" + + http_health_check { + } +} + +resource "google_compute_backend_service" "www-service" { + name = "tf-www-service" + protocol = "HTTP" + + backend { + group = "${google_compute_instance_group.www-resources.self_link}" + } + + health_checks = ["${google_compute_health_check.health-check.self_link}"] +} + +resource "google_compute_backend_service" "video-service" { + name = "tf-video-service" + protocol = "HTTP" + + backend { + group = "${google_compute_instance_group.video-resources.self_link}" + } + + health_checks = ["${google_compute_health_check.health-check.self_link}"] +} + +resource "google_compute_url_map" "web-map" { + name = "tf-web-map" + default_service = "${google_compute_backend_service.www-service.self_link}" + + host_rule { + hosts = ["*"] + path_matcher = "tf-allpaths" + } + + path_matcher { + name = "tf-allpaths" + default_service = "${google_compute_backend_service.www-service.self_link}" + + path_rule { + paths = ["/video", "/video/*",] + service = "${google_compute_backend_service.video-service.self_link}" + } + } +} + +resource "google_compute_target_http_proxy" "http-lb-proxy" { + name = "tf-http-lb-proxy" + url_map = "${google_compute_url_map.web-map.self_link}" +} + +resource "google_compute_global_forwarding_rule" "default" { + name = "tf-http-content-gfr" + target = "${google_compute_target_http_proxy.http-lb-proxy.self_link}" + ip_address = "${google_compute_global_address.external-address.address}" + port_range = "80" +} + +resource "google_compute_firewall" "default" { + name = "tf-www-firewall-allow-internal-only" + network = "default" + + allow { + protocol = "tcp" + ports = ["80"] + } + + source_ranges = ["130.211.0.0/22", "35.191.0.0/16"] + target_tags = ["http-tag"] +} diff --git a/examples/google-content-based-load-balancing/output.tf b/examples/google-content-based-load-balancing/output.tf new file mode 100644 index 000000000..f9b443672 --- /dev/null +++ b/examples/google-content-based-load-balancing/output.tf @@ -0,0 +1,3 @@ +output "application_public_ip" { + value = "${google_compute_global_forwarding_rule.default.ip_address}" +} diff --git a/examples/google-content-based-load-balancing/scripts/install-video.sh b/examples/google-content-based-load-balancing/scripts/install-video.sh new file mode 100644 index 000000000..37faffba9 --- /dev/null +++ b/examples/google-content-based-load-balancing/scripts/install-video.sh @@ -0,0 +1,9 @@ +#!/bin/bash -xe +sudo apt-get update +sudo apt-get install apache2 -y +sudo a2ensite default-ssl +sudo a2enmod ssl +sudo service apache2 restart +echo '

www-video

' | sudo tee /var/www/html/index.html +sudo mkdir /var/www/html/video +echo '

www-video

' | sudo tee /var/www/html/video/index.html diff --git a/examples/google-content-based-load-balancing/scripts/install-www.sh b/examples/google-content-based-load-balancing/scripts/install-www.sh new file mode 100644 index 000000000..b0722bbe9 --- /dev/null +++ b/examples/google-content-based-load-balancing/scripts/install-www.sh @@ -0,0 +1,7 @@ +#!/bin/bash -xe +sudo apt-get update +sudo apt-get install apache2 -y +sudo a2ensite default-ssl +sudo a2enmod ssl +sudo service apache2 restart +echo '

www

' | sudo tee /var/www/html/index.html diff --git a/examples/google-content-based-load-balancing/terraform.tfvars.example b/examples/google-content-based-load-balancing/terraform.tfvars.example new file mode 100644 index 000000000..78b1d2a1c --- /dev/null +++ b/examples/google-content-based-load-balancing/terraform.tfvars.example @@ -0,0 +1,4 @@ +region = "us-central1" +region_zone = "us-central1-b" +project_name = "my-project-id-123" +credentials_file_path = "~/.gcloud/Terraform.json" diff --git a/examples/google-content-based-load-balancing/variables.tf b/examples/google-content-based-load-balancing/variables.tf new file mode 100644 index 000000000..f33f20427 --- /dev/null +++ b/examples/google-content-based-load-balancing/variables.tf @@ -0,0 +1,16 @@ +variable "region" { + default = "us-central1" +} + +variable "region_zone" { + default = "us-central1-f" +} + +variable "project_name" { + description = "The ID of the Google Cloud project" +} + +variable "credentials_file_path" { + description = "Path to the JSON file used to describe your account credentials" + default = "~/.gcloud/Terraform.json" +}