From bde9e3e50b372c5480c6df37e15483449d717725 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Sun, 7 Feb 2021 05:26:13 -0800 Subject: [PATCH] initial --- .gitignore | 11 +++++++++++ README.md | 3 +++ jobs/aws_fabio.hcl | 29 ++++++++++++++++++++++++++++ jobs/azure_fabio.hcl | 39 ++++++++++++++++++++++++++++++++++++++ jobs/browserquest.hcl | 44 +++++++++++++++++++++++++++++++++++++++++++ jobs/mongo.hcl | 40 +++++++++++++++++++++++++++++++++++++++ main.tf | 36 +++++++++++++++++++++++++++++++++++ outputs.tf | 3 +++ variables.tf | 7 +++++++ 9 files changed, 212 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 jobs/aws_fabio.hcl create mode 100644 jobs/azure_fabio.hcl create mode 100644 jobs/browserquest.hcl create mode 100644 jobs/mongo.hcl create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b8e400e --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* + +# Crash log files +crash.log +terraform +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..21ce5ff --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# terraform-nomad-mmorpg + +Deploys BrowserQuest, an MMORPG on Azure + AWS using federated Nomad clusters. Mean for use with "Terraform in Action", chapter 8 \ No newline at end of file diff --git a/jobs/aws_fabio.hcl b/jobs/aws_fabio.hcl new file mode 100644 index 0000000..6f5abca --- /dev/null +++ b/jobs/aws_fabio.hcl @@ -0,0 +1,29 @@ +job "fabio" { + datacenters = ["aws"] + region = "${region}" + type = "system" + group "fabio" { + task "fabio" { + driver = "docker" + config { + image = "fabiolb/fabio" + network_mode = "host" + } + + resources { + cpu = 200 + memory = 128 + network { + mbits = 20 + + port "lb" { + static = 9999 + } + port "ui" { + static = 9998 + } + } + } + } + } +} \ No newline at end of file diff --git a/jobs/azure_fabio.hcl b/jobs/azure_fabio.hcl new file mode 100644 index 0000000..917b43d --- /dev/null +++ b/jobs/azure_fabio.hcl @@ -0,0 +1,39 @@ +job "fabio" { + datacenters = ["azure"] + region = "${region}" + type = "system" + group "fabio" { + volume "vol" { + type = "host" + read_only = false + source = "fabio" + } + task "fabio" { + driver = "docker" + config { + image = "fabiolb/fabio" + network_mode = "host" + } + + volume_mount { + volume = "vol" + destination = "/etc/fabio/fabio.properties" + read_only = false + } + + resources { + cpu = 200 + memory = 128 + network { + mbits = 20 + port "db" { + static = 27017 + } + port "ui" { + static = 9998 + } + } + } + } + } +} \ No newline at end of file diff --git a/jobs/browserquest.hcl b/jobs/browserquest.hcl new file mode 100644 index 0000000..807164b --- /dev/null +++ b/jobs/browserquest.hcl @@ -0,0 +1,44 @@ +job "browserquest" { + datacenters = ["aws"] + region = "${region}" + + group "browserquest" { + task "server" { + driver = "docker" + + config { + image = "swinkler/browserquest" + command = "/bin/bash" + args = [ + "-c", + "node server.js --mongoServer ${address}" + ] + + port_map { + http = "8080" + } + + } + + resources { + network { + mbits = 10 + port "http" {} + } + } + + service { + name = "browserquest-aws" + tags = ["urlprefix-/"] + port = "http" + check { + name = "alive" + type = "http" + path = "/" + interval = "10s" + timeout = "2s" + } + } + } + } +} diff --git a/jobs/mongo.hcl b/jobs/mongo.hcl new file mode 100644 index 0000000..5504b78 --- /dev/null +++ b/jobs/mongo.hcl @@ -0,0 +1,40 @@ +job "mongo" { + datacenters = ["azure"] + region = "${region}" + type = "service" + group "mongo" { + ephemeral_disk { + size = 300 + } + + task "server" { + driver = "docker" + + config { + image = "mongo:3.4.4" + port_map = { + mongo = 27017 + } + } + + resources { + network { + mbits = 10 + port "mongo" { + } + } + } + + service { + name = "mongodb-azure" + tags = ["urlprefix-:27017 proto=tcp"] + port = "mongo" + check { + type = "tcp" + interval = "10s" + timeout = "2s" + } + } + } + } +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..a0d9bc1 --- /dev/null +++ b/main.tf @@ -0,0 +1,36 @@ +provider "nomad" { + alias = "aws" +} + +provider "nomad" { + alias = "azure" +} + +data "nomad_regions" "current" { + provider = nomad.aws +} + +locals { + aws_region = sort(data.nomad_regions.current.regions)[1] + azure_region = sort(data.nomad_regions.current.regions)[0] +} + +resource "nomad_job" "aws_fabio" { + jobspec = templatefile("${path.module}/jobs/aws_fabio.hcl",{region = local.aws_region}) + provider = nomad.aws +} + +resource "nomad_job" "azure_fabio" { + jobspec = templatefile("${path.module}/jobs/azure_fabio.hcl",{region = local.azure_region}) + provider = nomad.azure +} + +resource "nomad_job" "azure_mongo" { + jobspec = templatefile("${path.module}/jobs/mongo.hcl",{region = local.azure_region}) + provider = nomad.azure +} + +resource "nomad_job" "aws_browserquest" { + jobspec = templatefile("${path.module}/jobs/browserquest.hcl",{region=local.aws_region,address=replace(var.fabio_db,"tcp://","")}) + provider = nomad.aws +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..f228b70 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,3 @@ +output "browserquest_address" { + value = var.fabio_lb +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..d615b7a --- /dev/null +++ b/variables.tf @@ -0,0 +1,7 @@ +variable "fabio_db" { + type = string +} + +variable "fabio_lb" { + type = string +} \ No newline at end of file