From 8efadf3d43fda9345dd8215cdfd244045a5c818e Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Sun, 13 Mar 2022 10:13:19 -0700 Subject: [PATCH] Be a bit more dynamic with host names --- nomad/blocky/blocky.nomad | 11 ++--------- nomad/blocky/blocky.tf | 11 +++++++++-- nomad/blocky/config.yml | 3 +-- nomad/metrics/grafana.nomad | 8 ++++++-- nomad/metrics/metrics.tf | 16 +++++++++------- nomad/metrics/prometheus.nomad | 6 +++++- nomad/nextcloud/nextcloud.nomad | 9 +-------- nomad/services.tf | 3 +-- nomad/traefik/traefik.tf | 9 ++++++++- nomad/whoami.nomad | 2 +- 10 files changed, 43 insertions(+), 35 deletions(-) diff --git a/nomad/blocky/blocky.nomad b/nomad/blocky/blocky.nomad index f1e10c3..98af39b 100644 --- a/nomad/blocky/blocky.nomad +++ b/nomad/blocky/blocky.nomad @@ -3,12 +3,6 @@ variable "config_data" { description = "Plain text config file for blocky" } -variable "base_hostname" { - type = string - description = "Base hostname to serve content from" - default = "dev.homelab" -} - job "blocky" { datacenters = ["dc1"] type = "service" @@ -29,19 +23,18 @@ job "blocky" { } service { - name = "dns" + name = "blocky-dns" port = "dns" } service { - name = "web" + name = "blocky-api" port = "web" tags = [ "traefik.enable=true", "traefik.consulcatalog.connect=false", "traefik.http.routers.blocky.entrypoints=web,websecure", - "traefik.http.routers.blocky.rule=Host(`blocky.${var.base_hostname}`)", "traefik.http.routers.blocky.tls=true", ] } diff --git a/nomad/blocky/blocky.tf b/nomad/blocky/blocky.tf index 133aba1..cd578bc 100644 --- a/nomad/blocky/blocky.tf +++ b/nomad/blocky/blocky.tf @@ -5,7 +5,15 @@ variable "base_hostname" { } locals { - config_data = file("${path.module}/config.yml") + config_data = templatefile( + "${path.module}/config.yml", + { + "base_hostname" = "${var.base_hostname}", + # Could get this from consul_service.traefik + # but not sure what happens if it doens't exist yet + "ingress_address" = "192.168.2.106", + } + ) } resource "nomad_job" "blocky" { @@ -13,7 +21,6 @@ resource "nomad_job" "blocky" { enabled = true vars = { "config_data" = "${local.config_data}", - "base_hostname" = "${var.base_hostname}", } } diff --git a/nomad/blocky/config.yml b/nomad/blocky/config.yml index 6f6c4a5..12cbdfa 100644 --- a/nomad/blocky/config.yml +++ b/nomad/blocky/config.yml @@ -13,8 +13,7 @@ blocking: customDNS: customTTL: 1h mapping: - # TODO: Use a variable for this - dev.homelab: 192.168.2.41 + ${base_hostname}: ${ingress_address} port: 53 httpPort: 4000 diff --git a/nomad/metrics/grafana.nomad b/nomad/metrics/grafana.nomad index ef096ce..6e1f0ee 100644 --- a/nomad/metrics/grafana.nomad +++ b/nomad/metrics/grafana.nomad @@ -13,7 +13,13 @@ job "grafana" { } } + ephemeral_disk { + migrate = true + sticky = true + } + service { + name = "grafana" port = "web" connect { @@ -47,8 +53,6 @@ job "grafana" { tags = [ "traefik.enable=true", "traefik.http.routers.grafana.entrypoints=web,websecure", - "traefik.http.routers.grafana.rule=Host(`grafana.dev.homelab`)", - "traefik.subdomain=grafana-sub", "traefik.http.routers.grafana.tls=true", ] } diff --git a/nomad/metrics/metrics.tf b/nomad/metrics/metrics.tf index e35d98d..b2dd2c3 100644 --- a/nomad/metrics/metrics.tf +++ b/nomad/metrics/metrics.tf @@ -1,9 +1,3 @@ -variable "base_hostname" { - type = string - description = "Base hostname to serve content from" - default = "dev.homelab" -} - variable "consul_address" { type = string description = "address of consul server for dynamic scraping" @@ -17,11 +11,19 @@ variable "consul_address" { # jobspec = file("${path.module}/exporters.nomad") # } +data "consul_nodes" "all-nodes" { + query_options { + datacenter = "dc1" + } +} + resource "nomad_job" "prometheus" { hcl2 { enabled = true vars = { - "consul_address" = "${var.consul_address}", + # "consul_address" = "${var.consul_address}", + # TODO: Should this be a list? + "consul_address" = "http://${data.consul_nodes.all-nodes.nodes[0].address}:8500", } } diff --git a/nomad/metrics/prometheus.nomad b/nomad/metrics/prometheus.nomad index c59e463..15e5e79 100644 --- a/nomad/metrics/prometheus.nomad +++ b/nomad/metrics/prometheus.nomad @@ -19,6 +19,11 @@ job "prometheus" { } } + ephemeral_disk { + migrate = true + sticky = true + } + service { name = "prometheus" port = "web" @@ -50,7 +55,6 @@ job "prometheus" { tags = [ "traefik.enable=true", "traefik.http.routers.prometheus.entrypoints=web,websecure", - "traefik.http.routers.prometheus.rule=Host(`prometheus.dev.homelab`)", "traefik.http.routers.prometheus.tls=true", ] } diff --git a/nomad/nextcloud/nextcloud.nomad b/nomad/nextcloud/nextcloud.nomad index 5dff977..36de9d6 100644 --- a/nomad/nextcloud/nextcloud.nomad +++ b/nomad/nextcloud/nextcloud.nomad @@ -1,9 +1,3 @@ -variable "base_hostname" { - type = string - description = "Base hostname to serve content from" - default = "dev.homelab" -} - variable "nextcloud_db" { type = string default = "nextcloud" @@ -64,7 +58,6 @@ job "nextcloud" { tags = [ "traefik.enable=true", "traefik.http.routers.nextcloud.entrypoints=web,websecure", - "traefik.http.routers.nextcloud.rule=Host(`nextcloud.${var.base_hostname}`)", "traefik.http.routers.nextcloud.tls=true", ] @@ -126,4 +119,4 @@ job "nextcloud" { } } } -} \ No newline at end of file +} diff --git a/nomad/services.tf b/nomad/services.tf index ce6cd81..dbd2fdc 100644 --- a/nomad/services.tf +++ b/nomad/services.tf @@ -20,7 +20,7 @@ data "consul_service" "read-nomad-cluster" { } locals { - nomad_node = data.consul_service.read-nomad-cluster.service[0] + nomad_node = data.consul_service.read-nomad-cluster.service[0] nomad_node_address = "http://${local.nomad_node.node_address}:${local.nomad_node.port}" } @@ -55,7 +55,6 @@ module "metrics" { source = "./metrics" consul_address = var.consul_address - base_hostname = var.base_hostname } module "nextcloud" { diff --git a/nomad/traefik/traefik.tf b/nomad/traefik/traefik.tf index dabadd5..db695d2 100644 --- a/nomad/traefik/traefik.tf +++ b/nomad/traefik/traefik.tf @@ -9,11 +9,18 @@ variable "consul_address" { description = "address of consul server for dynamic routes" } +data "consul_nodes" "all-nodes" { + query_options { + datacenter = "dc1" + } +} + resource "nomad_job" "traefik" { hcl2 { enabled = true vars = { - "consul_address" = "${var.consul_address}", + # "consul_address" = "${var.consul_address}", + "consul_address" = "http://${data.consul_nodes.all-nodes.nodes[0].address}:8500", "base_hostname" = "${var.base_hostname}", } } diff --git a/nomad/whoami.nomad b/nomad/whoami.nomad index 398ce1a..96270ce 100644 --- a/nomad/whoami.nomad +++ b/nomad/whoami.nomad @@ -27,6 +27,7 @@ job "whoami" { } service { + name = "whoami" port = "web" connect { @@ -55,7 +56,6 @@ job "whoami" { tags = [ "traefik.enable=true", "traefik.http.routers.whoami.entrypoints=web,websecure", - "traefik.http.routers.whoami.rule=Host(`whoami.${var.base_hostname}`)", "traefik.http.routers.whoami.tls=true", ] }