provider/google: Google Cloud content-based load balancing example. (#14448)
This commit is contained in:
parent
2fbfdd4ab6
commit
d2eaa6f8e0
|
@ -0,0 +1,3 @@
|
|||
terraform.tfstate
|
||||
terraform.tfstate.backup
|
||||
terraform.tfvars
|
|
@ -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
|
||||
<h1>www</h1>
|
||||
```
|
||||
|
||||
And the following at the /video/ url:
|
||||
```html
|
||||
<h1>www-video</h1>
|
||||
```
|
||||
|
||||
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" \
|
||||
```
|
|
@ -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"]
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
output "application_public_ip" {
|
||||
value = "${google_compute_global_forwarding_rule.default.ip_address}"
|
||||
}
|
|
@ -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 '<!doctype html><html><body><h1>www-video</h1></body></html>' | sudo tee /var/www/html/index.html
|
||||
sudo mkdir /var/www/html/video
|
||||
echo '<!doctype html><html><body><h1>www-video</h1></body></html>' | sudo tee /var/www/html/video/index.html
|
|
@ -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 '<!doctype html><html><body><h1>www</h1></body></html>' | sudo tee /var/www/html/index.html
|
|
@ -0,0 +1,4 @@
|
|||
region = "us-central1"
|
||||
region_zone = "us-central1-b"
|
||||
project_name = "my-project-id-123"
|
||||
credentials_file_path = "~/.gcloud/Terraform.json"
|
|
@ -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"
|
||||
}
|
Loading…
Reference in New Issue