Merge pull request #2914 from TimeIncOSS/google-cloud-2-tier-example
google: Add an example of a two-tier app
This commit is contained in:
commit
8be06a021a
|
@ -0,0 +1 @@
|
||||||
|
terraform.tfvars
|
|
@ -0,0 +1,39 @@
|
||||||
|
# Basic Two-Tier Architecture in Google Cloud
|
||||||
|
|
||||||
|
This provides a template for running a simple two-tier architecture on Google Cloud.
|
||||||
|
The premise is that you have stateless app servers running behind
|
||||||
|
a load balancer serving traffic.
|
||||||
|
|
||||||
|
To simplify the example, this intentionally ignores deploying and
|
||||||
|
getting your application onto the servers. However, you could do so either via
|
||||||
|
[startup script](http://terraform.io/docs/providers/google/r/compute_instance.html#metadata_startup_script) or
|
||||||
|
[provisioners](https://www.terraform.io/docs/provisioners/) and a configuration
|
||||||
|
management tool, or by pre-baking configured images with
|
||||||
|
[Packer](https://packer.io/docs/builders/googlecompute.html).
|
||||||
|
|
||||||
|
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 a simple header:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<h1>Welcome to instance 0</h1>
|
||||||
|
```
|
||||||
|
|
||||||
|
The index may differ once you increase `count` of `google_compute_instance`
|
||||||
|
(i.e. provision more instances).
|
||||||
|
|
||||||
|
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="account_file_path=~/.gcloud/Terraform.json" \
|
||||||
|
-var="public_key_path=~/.ssh/gcloud_id_rsa.pub" \
|
||||||
|
-var="private_key_path=~/.ssh/gcloud_id_rsa"
|
||||||
|
```
|
|
@ -0,0 +1,77 @@
|
||||||
|
# See https://cloud.google.com/compute/docs/load-balancing/network/example
|
||||||
|
|
||||||
|
provider "google" {
|
||||||
|
region = "${var.region}"
|
||||||
|
project = "${var.project_name}"
|
||||||
|
account_file = "${file(var.account_file_path)}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_http_health_check" "default" {
|
||||||
|
name = "tf-www-basic-check"
|
||||||
|
request_path = "/"
|
||||||
|
check_interval_sec = 1
|
||||||
|
healthy_threshold = 1
|
||||||
|
unhealthy_threshold = 10
|
||||||
|
timeout_sec = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_target_pool" "default" {
|
||||||
|
name = "tf-www-target-pool"
|
||||||
|
instances = ["${google_compute_instance.www.*.self_link}"]
|
||||||
|
health_checks = ["${google_compute_http_health_check.default.name}"]
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_forwarding_rule" "default" {
|
||||||
|
name = "tf-www-forwarding-rule"
|
||||||
|
target = "${google_compute_target_pool.default.self_link}"
|
||||||
|
port_range = "80"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_instance" "www" {
|
||||||
|
count = 3
|
||||||
|
|
||||||
|
name = "tf-www-${count.index}"
|
||||||
|
machine_type = "n1-standard-1"
|
||||||
|
zone = "${var.region_zone}"
|
||||||
|
tags = ["www-node"]
|
||||||
|
|
||||||
|
disk {
|
||||||
|
image = "ubuntu-os-cloud/ubuntu-1204-precise-v20150625"
|
||||||
|
}
|
||||||
|
|
||||||
|
network_interface {
|
||||||
|
network = "default"
|
||||||
|
access_config {
|
||||||
|
# Ephemeral
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
metadata {
|
||||||
|
sshKeys = "ubuntu:${file("~/.ssh/gcloud_id_rsa.pub")}"
|
||||||
|
startup-script = <<SCRIPT
|
||||||
|
apt-get -y update
|
||||||
|
apt-get -y install nginx
|
||||||
|
HOSTNAME=$(hostname | tr -d "\n")
|
||||||
|
IP=$(curl -s -H "Metadata-Flavor:Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip)
|
||||||
|
echo "Welcome to ${count.index} - $HOSTNAME ($IP)" > /usr/share/nginx/www/index.html
|
||||||
|
service nginx start
|
||||||
|
SCRIPT
|
||||||
|
}
|
||||||
|
|
||||||
|
service_account {
|
||||||
|
scopes = ["https://www.googleapis.com/auth/compute.readonly"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "google_compute_firewall" "default" {
|
||||||
|
name = "tf-www-firewall"
|
||||||
|
network = "default"
|
||||||
|
|
||||||
|
allow {
|
||||||
|
protocol = "tcp"
|
||||||
|
ports = ["80"]
|
||||||
|
}
|
||||||
|
|
||||||
|
source_ranges = ["0.0.0.0/0"]
|
||||||
|
target_tags = ["www-node"]
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
output "pool_public_ip" {
|
||||||
|
value = "${google_compute_forwarding_rule.default.ip_address}"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "instance_ips" {
|
||||||
|
value = "${join(" ", google_compute_instance.www.*.network_interface.0.access_config.0.nat_ip)}"
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
region = "us-central1"
|
||||||
|
region_zone = "us-central1-a"
|
||||||
|
project_name = "my-project-id-123"
|
||||||
|
account_file_path = "~/.gcloud/Terraform.json"
|
||||||
|
public_key_path = "~/.ssh/gcloud_id_rsa.pub"
|
||||||
|
private_key_path = "~/.ssh/gcloud_id_rsa"
|
|
@ -0,0 +1,15 @@
|
||||||
|
variable "region" {
|
||||||
|
default = "us-central1"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "region_zone" {
|
||||||
|
default = "us-central1-f"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "project_name" {
|
||||||
|
description = "The ID of the Google Cloud project"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "account_file_path" {
|
||||||
|
description = "Path to the JSON file used to describe your account credentials"
|
||||||
|
}
|
Loading…
Reference in New Issue