Skip to content

Commit

Permalink
Merge pull request #23 from wenzel-felix/advancedexample
Browse files Browse the repository at this point in the history
Advanced example & dependency fix
  • Loading branch information
wenzel-felix authored Jan 15, 2023
2 parents 7fc1b88 + 0f999c6 commit c47df8a
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 138 deletions.
81 changes: 80 additions & 1 deletion examples/advanced-setup/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
module "hetzner-nomad-consul" {
source = "../../"
hetzner_token = var.hetzner_token
nomad_client_count = 1
}

terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 3.0"
}
hcloud = {
source = "hetznercloud/hcloud"
version = "1.36.2"
}
}
}

provider "hcloud" {
token = var.hetzner_token
}

data "hcloud_server" "client-0" {
name = [for key, value in module.hetzner-nomad-consul.server_info: key if length(regexall("client-0.*", key)) > 0][0]
}

resource "hcloud_firewall" "traefik" {
name = "traefik"

rule {
direction = "in"
protocol = "tcp"
source_ips = [
"0.0.0.0/0",
"::/0"
]
port = "8081"
}
rule {
direction = "in"
protocol = "tcp"
source_ips = [
"0.0.0.0/0",
"::/0"
]
port = "80"
}
}

resource "hcloud_firewall_attachment" "traefik" {
depends_on = [
module.hetzner-nomad-consul
]
firewall_id = hcloud_firewall.traefik.id
server_ids = [
data.hcloud_server.client-0.id
]
}

output "server_info" {
Expand All @@ -13,4 +68,28 @@ output "nomad_token" {

output "nomad_address" {
value = module.hetzner-nomad-consul.nomad_address
}
}

provider "cloudflare" {
api_token = var.cloudflare_token
}

resource "cloudflare_record" "nomad" {
zone_id = var.cloudflare_zone_id
name = "nomad"
type = "A"
proxied = true
value = split(":", split("//", module.hetzner-nomad-consul.nomad_address)[1])[0]
}

locals {
traefik_ip = [for key, value in module.hetzner-nomad-consul.server_info: value.public_ip if length(regexall("client-0.*", key)) > 0][0]
}

resource "cloudflare_record" "traefik" {
zone_id = var.cloudflare_zone_id
name = "traefik"
type = "A"
proxied = true
value = local.traefik_ip
}
43 changes: 43 additions & 0 deletions examples/advanced-setup/nomad/jobs/demo-webapp.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
job "demo-webapp" {
datacenters = ["dc1"]

group "demo" {
count = 2

network {
port "http" {
static = 8888
}
}

service {
name = "demo-webapp"
port = "http"

tags = [
"traefik.enable=true",
"traefik.http.routers.http.rule=Path(`/`)",
]

check {
type = "http"
path = "/health"
interval = "2s"
timeout = "2s"
}
}

task "server" {
driver = "docker"

config {
image = "hashicorp/http-echo"
network_mode = "host"
args = [
"-listen", ":8888",
"-text", "Hello World!",
]
}
}
}
}
79 changes: 79 additions & 0 deletions examples/advanced-setup/nomad/jobs/traefik.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
job "traefik" {
region = "global"
datacenters = ["dc1"]
type = "service"

group "traefik" {
count = 1

network {
port "http" {
static = 80
}

port "api" {
static = 8081
}
}

service {
name = "traefik"

check {
name = "alive"
type = "tcp"
port = "http"
interval = "10s"
timeout = "2s"
}
}

task "traefik" {
constraint {
attribute = "${attr.unique.hostname}"
operator = "regexp"
value = "client-0.*"
}
driver = "docker"

config {
image = "traefik:v2.2"
network_mode = "host"

volumes = [
"local/traefik.toml:/etc/traefik/traefik.toml",
]
}

template {
data = <<EOF
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.traefik]
address = ":8081"
[api]
dashboard = true
insecure = true
# Enable Consul Catalog configuration backend.
[providers.consulCatalog]
prefix = "traefik"
exposedByDefault = false
[providers.consulCatalog.endpoint]
address = "127.0.0.1:8500"
scheme = "http"
EOF

destination = "local/traefik.toml"
}

resources {
cpu = 100
memory = 128
}
}
}
}
124 changes: 4 additions & 120 deletions examples/advanced-setup/nomad/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,128 +7,12 @@ resource "nomad_job" "demo-webapp" {
depends_on = [
nomad_job.traefik
]
jobspec = <<EOT
job "demo-webapp" {
datacenters = ["dc1"]
group "demo" {
count = 1
network {
port "http"{
static = 8888
}
}
service {
name = "demo-webapp"
port = "http"
tags = [
"traefik.enable=true",
"traefik.http.routers.http.rule=Path(`/`)",
]
check {
type = "http"
path = "/health"
interval = "2s"
timeout = "2s"
}
}
task "server" {
driver = "docker"
config {
image = "hashicorp/http-echo"
network_mode = "host"
args = [
"-listen", ":8888",
"-text", "Hello World!",
]
}
}
jobspec = file("jobs/demo-webapp.nomad")
lifecycle {
replace_triggered_by = [nomad_job.traefik]
}
}
EOT
}

resource "nomad_job" "traefik" {
jobspec = <<EOT
job "traefik" {
region = "global"
datacenters = ["dc1"]
type = "service"
group "traefik" {
count = 1
network {
port "http" {
static = 8080
}
port "api" {
static = 8081
}
}
service {
name = "traefik"
check {
name = "alive"
type = "tcp"
port = "http"
interval = "10s"
timeout = "2s"
}
}
task "traefik" {
driver = "docker"
config {
image = "traefik:v2.2"
network_mode = "host"
volumes = [
"local/traefik.toml:/etc/traefik/traefik.toml",
]
}
template {
data = <<EOF
[entryPoints]
[entryPoints.http]
address = ":8080"
[entryPoints.traefik]
address = ":8081"
[api]
dashboard = true
insecure = true
# Enable Consul Catalog configuration backend.
[providers.consulCatalog]
prefix = "traefik"
exposedByDefault = false
[providers.consulCatalog.endpoint]
address = "127.0.0.1:8500"
scheme = "http"
EOF
destination = "local/traefik.toml"
}
resources {
cpu = 100
memory = 128
}
}
}
jobspec = file("jobs/traefik.nomad")
}
EOT
}
4 changes: 3 additions & 1 deletion examples/advanced-setup/test.auto.tfvars.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
hetzner_token = "<your token>"
hetzner_token = "<your token>"
cloudflare_token = "<your token>"
cloudflare_zone_id = "<your zone id>"
10 changes: 10 additions & 0 deletions examples/advanced-setup/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
variable "hetzner_token" {
type = string
description = "Hetzner Cloud API Token"
}

variable "cloudflare_token" {
type = string
description = "Cloudflare API Token"
}

variable "cloudflare_zone_id" {
type = string
description = "Cloudflare Zone ID"
}
7 changes: 5 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,12 @@ resource "hcloud_firewall" "default" {
}
}

resource "hcloud_firewall_attachment" "fw_ref" {
resource "hcloud_firewall_attachment" "default" {
depends_on = [
hcloud_server.main
]
firewall_id = hcloud_firewall.default.id
label_selectors = ["nomad-server", "nomad-client"]
label_selectors = [ "nomad-server", "nomad-client" ]
}

resource "hcloud_load_balancer" "app_load_balancer" {
Expand Down
Loading

0 comments on commit c47df8a

Please sign in to comment.