Skip to content

Commit ca67d66

Browse files
committed
import of examples
1 parent 94e783a commit ca67d66

File tree

7 files changed

+296
-0
lines changed

7 files changed

+296
-0
lines changed

Diff for: examples/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/.terraform
2+
**/terraform.tfstate*

Diff for: examples/simple/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Internal Load Balancer Example
2+
3+
This example creates 3 instance groups. The first group is in us-central1-b and uses the internal load balancer to proxy access to services running in instance groups 2 and 3 which exist in us-central1-c and us-central1-f respectively. A regional TCP load balancer is also used to forward external traffic to the instances in group 1.
4+
5+
**Figure 1.** *diagram of Google Cloud resources*
6+
7+
![architecture diagram](./diagram.png)
8+
9+
## Set up the environment
10+
11+
```
12+
gcloud auth application-default login
13+
export GOOGLE_PROJECT=$(gcloud config get-value project)
14+
```
15+
16+
## Run Terraform
17+
18+
```
19+
terraform init
20+
terraform plan
21+
terraform apply
22+
```
23+
24+
Open URL of load balancer in browser:
25+
26+
```
27+
EXTERNAL_IP=$(terraform output -module gce-lb-fr | grep external_ip | cut -d = -f2 | xargs echo -n)
28+
open http://${EXTERNAL_IP}
29+
```
30+
31+
You should see the instance details from `group2` proxied through `group1` and the internal load balancer.

Diff for: examples/simple/diagram.png

107 KB
Loading

Diff for: examples/simple/gceme.sh.tpl

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash -xe
2+
3+
apt-get update
4+
apt-get install -y apache2 php5
5+
6+
cat > /var/www/html/index.php <<'EOF'
7+
<?php
8+
function metadata_value($value) {
9+
$opts = [
10+
"http" => [
11+
"method" => "GET",
12+
"header" => "Metadata-Flavor: Google"
13+
]
14+
];
15+
$context = stream_context_create($opts);
16+
$content = file_get_contents("http://metadata/computeMetadata/v1/$value", false, $context);
17+
return $content;
18+
}
19+
?>
20+
21+
<!doctype html>
22+
<html>
23+
<head>
24+
<!-- Compiled and minified CSS -->
25+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css">
26+
27+
<!-- Compiled and minified JavaScript -->
28+
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script>
29+
<title>Frontend Web Server</title>
30+
</head>
31+
<body>
32+
<div class="container">
33+
<div class="row">
34+
<div class="col s2">&nbsp;</div>
35+
<div class="col s8">
36+
37+
38+
<div class="card blue">
39+
<div class="card-content white-text">
40+
<div class="card-title">Backend that serviced this request</div>
41+
</div>
42+
<div class="card-content white">
43+
<table class="bordered">
44+
<tbody>
45+
<tr>
46+
<td>Name</td>
47+
<td><?php printf(metadata_value("instance/name")) ?></td>
48+
</tr>
49+
<tr>
50+
<td>ID</td>
51+
<td><?php printf(metadata_value("instance/id")) ?></td>
52+
</tr>
53+
<tr>
54+
<td>Hostname</td>
55+
<td><?php printf(metadata_value("instance/hostname")) ?></td>
56+
</tr>
57+
<tr>
58+
<td>Zone</td>
59+
<td><?php printf(metadata_value("instance/zone")) ?></td>
60+
</tr>
61+
<tr>
62+
<td>Machine Type</td>
63+
<td><?php printf(metadata_value("instance/machine-type")) ?></td>
64+
</tr>
65+
<tr>
66+
<td>Project</td>
67+
<td><?php printf(metadata_value("project/project-id")) ?></td>
68+
</tr>
69+
<tr>
70+
<td>Internal IP</td>
71+
<td><?php printf(metadata_value("instance/network-interfaces/0/ip")) ?></td>
72+
</tr>
73+
<tr>
74+
<td>External IP</td>
75+
<td><?php printf(metadata_value("instance/network-interfaces/0/access-configs/0/external-ip")) ?></td>
76+
</tr>
77+
</tbody>
78+
</table>
79+
</div>
80+
</div>
81+
82+
<div class="card blue">
83+
<div class="card-content white-text">
84+
<div class="card-title">Proxy that handled this request</div>
85+
</div>
86+
<div class="card-content white">
87+
<table class="bordered">
88+
<tbody>
89+
<tr>
90+
<td>Address</td>
91+
<td><?php printf($_SERVER["HTTP_HOST"]); ?></td>
92+
</tr>
93+
</tbody>
94+
</table>
95+
</div>
96+
97+
</div>
98+
</div>
99+
<div class="col s2">&nbsp;</div>
100+
</div>
101+
</div>
102+
</html>
103+
EOF
104+
sudo mv /var/www/html/index.html /var/www/html/index.html.old
105+
106+
[[ -n "${PROXY_PATH}" ]] && mkdir -p /var/www/html/${PROXY_PATH} && cp /var/www/html/index.php /var/www/html/${PROXY_PATH}/index.php
107+
108+
systemctl enable apache2
109+
systemctl restart apache2

Diff for: examples/simple/main.tf

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable region {
18+
default = "us-central1"
19+
}
20+
21+
variable network {
22+
default = "default"
23+
}
24+
25+
variable zone {
26+
default = "us-central1-b"
27+
}
28+
29+
provider google {
30+
region = "${var.region}"
31+
}
32+
33+
module "gce-lb-fr" {
34+
source = "github.com/GoogleCloudPlatform/terraform-google-lb"
35+
region = "${var.region}"
36+
network = "${var.network}"
37+
name = "group1-lb"
38+
service_port = "${module.mig1.service_port}"
39+
target_tags = ["${module.mig1.target_tags}"]
40+
}
41+
42+
module "gce-ilb" {
43+
source = "../../"
44+
region = "${var.region}"
45+
name = "group-ilb"
46+
ports = ["${module.mig2.service_port}"]
47+
health_port = "${module.mig2.service_port}"
48+
source_tags = ["${module.mig1.target_tags}"]
49+
target_tags = ["${module.mig2.target_tags}", "${module.mig3.target_tags}"]
50+
51+
backends = [
52+
{
53+
group = "${module.mig2.instance_group}"
54+
},
55+
{
56+
group = "${module.mig3.instance_group}"
57+
},
58+
]
59+
}

Diff for: examples/simple/mig.tf

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
data "template_file" "group1-startup-script" {
18+
template = "${file("${format("%s/nginx_upstream.sh.tpl", path.module)}")}"
19+
20+
vars {
21+
UPSTREAM = "${module.gce-ilb.ip_address}"
22+
}
23+
}
24+
25+
data "template_file" "group2-startup-script" {
26+
template = "${file("${format("%s/gceme.sh.tpl", path.module)}")}"
27+
28+
vars {
29+
PROXY_PATH = ""
30+
}
31+
}
32+
33+
data "template_file" "group3-startup-script" {
34+
template = "${file("${format("%s/gceme.sh.tpl", path.module)}")}"
35+
36+
vars {
37+
PROXY_PATH = ""
38+
}
39+
}
40+
41+
module "mig1" {
42+
source = "github.com/GoogleCloudPlatform/terraform-google-managed-instance-group"
43+
region = "${var.region}"
44+
zone = "${var.zone}"
45+
name = "group1"
46+
size = 2
47+
target_tags = ["allow-group1"]
48+
target_pools = ["${module.gce-lb-fr.target_pool}"]
49+
service_port = 80
50+
service_port_name = "http"
51+
startup_script = "${data.template_file.group1-startup-script.rendered}"
52+
}
53+
54+
module "mig2" {
55+
source = "github.com/GoogleCloudPlatform/terraform-google-managed-instance-group"
56+
region = "${var.region}"
57+
zone = "us-central1-c"
58+
name = "group2"
59+
size = 2
60+
target_tags = ["allow-group2"]
61+
service_port = 80
62+
service_port_name = "http"
63+
startup_script = "${data.template_file.group2-startup-script.rendered}"
64+
}
65+
66+
module "mig3" {
67+
source = "github.com/GoogleCloudPlatform/terraform-google-managed-instance-group"
68+
region = "${var.region}"
69+
zone = "us-central1-f"
70+
name = "group3"
71+
size = 2
72+
target_tags = ["allow-group3"]
73+
service_port = 80
74+
service_port_name = "http"
75+
startup_script = "${data.template_file.group3-startup-script.rendered}"
76+
}

Diff for: examples/simple/nginx_upstream.sh.tpl

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash -xe
2+
3+
apt-get update
4+
apt-get install -y nginx
5+
6+
cat - > /etc/nginx/sites-available/upstream <<EOF
7+
server {
8+
listen 80;
9+
location / {
10+
proxy_pass http://${UPSTREAM};
11+
}
12+
}
13+
EOF
14+
15+
unlink /etc/nginx/sites-enabled/default
16+
ln -sf /etc/nginx/sites-available/upstream /etc/nginx/sites-enabled/upstream
17+
18+
systemctl enable nginx
19+
systemctl reload nginx

0 commit comments

Comments
 (0)