Skip to content

Commit 346b8c2

Browse files
committedFeb 19, 2017
Add Terraform deployment scripts for GCE. (heroiclabs#33)
1 parent 4e13710 commit 346b8c2

File tree

8 files changed

+302
-0
lines changed

8 files changed

+302
-0
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ data/*
88
.idea
99
*.iml
1010
install/cloud/**/*.json
11+
install/cloud/**/*.tfvars
1112

1213
### Go ###
1314
# Compiled Object files, Static and Dynamic libs (Shared Objects)

‎install/cloud/gce/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
## Run Nakama with Google Compute Engine
2+
3+
These instructions show how to deploy Nakama and CockroachDB in Compute Engine on Google Cloud. The provisioner scripts are written in [Terraform](https://www.terraform.io) and automate the setup and deployment of the server resources.
4+
5+
The scripts define variables which must be configured for your deployment. These variables should be configured in a file you'd create called `myproject.tfvars`:
6+
7+
```
8+
gce_project_name = "myproject"
9+
gce_region = "us-east1"
10+
gce_region_zone = "us-east1-b"
11+
gce_ssh_user = "root"
12+
gce_ssh_public_key_file = "your/id_rsa.pub"
13+
gce_ssh_private_key_file = "your/id_rsa"
14+
app_nakama_version = "0.11.2"
15+
app_cockroachdb_version = "beta-20170209"
16+
app_machine_type = "g1-small"
17+
```
18+
19+
You'll also need an `account.json` used to describe your account credentials downloaded from Google Cloud Console. Have a look at the [configuration reference](https://www.terraform.io/docs/providers/google/index.html#configuration-reference) in Terraform's provider docs for more info.
20+
21+
If you need any help or have any questions join our [community channel](https://gitter.im/heroiclabs/nakama) and speak to an engineer or [open an issue](https://github.com/heroiclabs/nakama).
22+
23+
### Full workflow
24+
25+
To provision and deploy a minimal cluster:
26+
27+
1. Create a file named `myproject.tfvars` with the content above.
28+
29+
Update `"gce_project_name"`, `"gce_ssh_public_key_file"`, and `"gce_ssh_private_key_file"` with your settings.
30+
31+
2. Set the rest of the variables to the values you'd like to use to provision resources in Google Cloud. For example you might want to use an "n1-standard-1" instance rather than "g1-small".
32+
33+
3. You can inspect the resources which will be provisioned:
34+
35+
```
36+
terraform plan --var-file myproject.tfvars
37+
```
38+
39+
4. You can apply the resources which will be provisioned:
40+
41+
```
42+
terraform apply --var-file myproject.tfvars
43+
```
44+
45+
5. When complete it will include output which shows the public IP of your provisioned Nakama and CockroachDB instance:
46+
47+
```
48+
Outputs:
49+
50+
instance_ips = 10.100.40.100
51+
public_ip = 10.100.39.110
52+
```
53+
54+
6. The `instance_ips` contain the list of IP addresses which can be reached via a [Nakama client](https://heroiclabs.com/docs/clients/).

‎install/cloud/gce/main.tf

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2017 The Nakama Authors
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+
terraform {
18+
required_version = ">= 0.8, < 0.9"
19+
}
20+
21+
provider "google" {
22+
project = "${var.gce_project_name}"
23+
region = "${var.gce_region}"
24+
credentials = "${file("account.json")}"
25+
}
26+
27+
resource "google_compute_address" "api" {
28+
name = "api-address"
29+
}
30+
31+
resource "google_compute_target_pool" "api" {
32+
name = "api-target-pool"
33+
instances = ["${google_compute_instance.api.*.self_link}"]
34+
health_checks = ["${google_compute_http_health_check.healthcheck.name}"]
35+
}
36+
37+
resource "google_compute_http_health_check" "healthcheck" {
38+
name = "api-healthcheck"
39+
port = 8081
40+
request_path = "/v0/health"
41+
check_interval_sec = 5
42+
healthy_threshold = 1
43+
unhealthy_threshold = 3
44+
timeout_sec = 2
45+
}
46+
47+
resource "google_compute_firewall" "api" {
48+
name = "api-firewall"
49+
network = "default"
50+
51+
allow {
52+
protocol = "icmp"
53+
}
54+
55+
allow {
56+
protocol = "tcp"
57+
ports = ["22", "80", "443"]
58+
}
59+
60+
source_ranges = ["0.0.0.0/0"]
61+
target_tags = ["api-node"]
62+
}
63+
64+
resource "google_compute_disk" "default" {
65+
name = "api-disk"
66+
type = "pd-ssd"
67+
zone = "${var.gce_region_zone}"
68+
size = 10
69+
}
70+
71+
resource "google_compute_instance" "api" {
72+
count = 1
73+
name = "api-node-${count.index}"
74+
machine_type = "${var.app_machine_type}"
75+
zone = "${var.gce_region_zone}"
76+
tags = ["api-node"]
77+
78+
disk {
79+
image = "ubuntu-os-cloud/ubuntu-1604-lts"
80+
}
81+
82+
disk {
83+
disk = "${google_compute_disk.default.name}"
84+
}
85+
86+
network_interface {
87+
network = "default"
88+
access_config {} # Ephemeral
89+
}
90+
91+
service_account {
92+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
93+
}
94+
95+
metadata {
96+
ssh-keys = "${var.gce_ssh_user}:${file(var.gce_ssh_public_key_file)}"
97+
}
98+
99+
provisioner "file" {
100+
connection {
101+
user = "${var.gce_ssh_user}"
102+
private_key = "${file(var.gce_ssh_private_key_file)}"
103+
agent = false
104+
timeout = "30s"
105+
}
106+
source = "systemd/"
107+
destination = "/etc/systemd/system"
108+
}
109+
110+
provisioner "remote-exec" {
111+
connection {
112+
user = "${var.gce_ssh_user}"
113+
private_key = "${file(var.gce_ssh_private_key_file)}"
114+
agent = false
115+
timeout = "30s"
116+
}
117+
inline = [
118+
"cd /home/ubuntu",
119+
120+
# Setup cockroachdb
121+
"wget --no-verbose https://binaries.cockroachdb.com/cockroach-${var.app_cockroachdb_version}.linux-amd64.tgz",
122+
"tar zxvf cockroach-${var.app_cockroachdb_version}.linux-amd64.tgz",
123+
"chmod +x ./cockroach-${var.app_cockroachdb_version}.linux-amd64/cockroach",
124+
"ln -s ./cockroach-${var.app_cockroachdb_version}.linux-amd64/cockroach /home/ubuntu/cockroach",
125+
"systemctl start cockroach",
126+
127+
# Setup nakama
128+
"wget --no-verbose https://github.com/heroiclabs/nakama/releases/download/v${var.app_nakama_version}/nakama-${var.app_nakama_version}-linux-amd64.tar.gz",
129+
"mkdir -p nakama-${var.app_nakama_version}-linux-amd64",
130+
"tar zxvf nakama-${var.app_nakama_version}-linux-amd64.tar.gz -C nakama-${var.app_nakama_version}-linux-amd64",
131+
"chmod +x ./nakama-${var.app_nakama_version}-linux-amd64/nakama",
132+
"ln -s ./nakama-${var.app_nakama_version}-linux-amd64/nakama",
133+
"./nakama migrate up --db root@127.0.0.1:26257",
134+
"systemctl start nakama"
135+
]
136+
}
137+
}

‎install/cloud/gce/output.tf

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2017 The Nakama Authors
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+
output "public_ip" {
18+
value = "${google_compute_address.api.address}"
19+
}
20+
21+
output "instance_ips" {
22+
value = "${join(" ", google_compute_instance.api.*.network_interface.0.access_config.0.assigned_nat_ip)}"
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[Unit]
2+
Description=CockroachDB server
3+
ConditionPathExists=/home/ubuntu/cockroach
4+
Wants=network.target
5+
After=network.target
6+
7+
[Service]
8+
ExecStart=/home/ubuntu/cockroach start --insecure --store=attrs=ssd,path=/home/ubuntu/cockroach-store
9+
Restart=always
10+
RestartSec=3
11+
TimeoutSec=6
12+
LimitNOFILE=1048576:1048576
13+
LimitNPROC=1048576:1048576
14+
15+
[Install]
16+
WantedBy=multi-user.target
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[Service]
2+
# Needed to workaround issue - cockroachdb/cockroach#12675
3+
Environment="COCKROACH_METRICS_SAMPLE_INTERVAL=1000h"
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Nakama server
3+
ConditionPathExists=/home/ubuntu/nakama
4+
Requires=cockroach.service
5+
Wants=network.target
6+
After=network.target
7+
8+
[Service]
9+
ExecStart=/home/ubuntu/nakama --port 80 --ops-port 8081 --db root@127.0.0.1:26257 --data-dir /home/ubuntu/nakama-data
10+
Restart=always
11+
RestartSec=3
12+
TimeoutSec=6
13+
LimitNOFILE=1048576:1048576
14+
LimitNPROC=1048576:1048576
15+
16+
[Install]
17+
WantedBy=multi-user.target

‎install/cloud/gce/variables.tf

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2017 The Nakama Authors
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+
# The name of the project in GCP.
18+
variable "gce_project_name" {
19+
}
20+
21+
# The region in GCP to provision resources.
22+
variable "gce_region" {
23+
}
24+
25+
# The region zone in GCP to provision resources in.
26+
variable "gce_region_zone" {
27+
}
28+
29+
# The SSH user configuration to access compute instances.
30+
variable "gce_ssh_user" {
31+
}
32+
33+
# The SSH public key file to access compute instances.
34+
variable "gce_ssh_public_key_file" {
35+
}
36+
37+
# The SSH private key file to access the compute instances.
38+
variable "gce_ssh_private_key_file" {
39+
}
40+
41+
# The version of Nakama which will be deployed.
42+
variable "app_nakama_version" {
43+
}
44+
45+
# The version of CockroachDB which will be deployed.
46+
variable "app_cockroachdb_version" {
47+
}
48+
49+
# The machine type to provision in GCP.
50+
variable "app_machine_type" {
51+
}

0 commit comments

Comments
 (0)
Please sign in to comment.