From 28e9ead743064d0db2cabcd4e6228ed7b559c2ac Mon Sep 17 00:00:00 2001 From: Ben Martin Date: Mon, 2 Jun 2025 19:11:02 +0100 Subject: [PATCH] chore(n8n): add module and job specification for Nomad deployment --- main.tf | 4 ++ modules/n8n/jobspec.nomad.hcl | 97 +++++++++++++++++++++++++++++++++++ modules/n8n/main.tf | 35 +++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 modules/n8n/jobspec.nomad.hcl create mode 100644 modules/n8n/main.tf diff --git a/main.tf b/main.tf index ed38e11..46052f5 100644 --- a/main.tf +++ b/main.tf @@ -50,3 +50,7 @@ module "jayne-martin-counselling" { module "monica" { source = "./modules/monica" } + +module "n8n" { + source = "./modules/n8n" +} diff --git a/modules/n8n/jobspec.nomad.hcl b/modules/n8n/jobspec.nomad.hcl new file mode 100644 index 0000000..e91812d --- /dev/null +++ b/modules/n8n/jobspec.nomad.hcl @@ -0,0 +1,97 @@ +job "n8n" { + + group "n8n" { + + network { + mode = "bridge" + port "http" { + to = 5678 + } + port "envoy_metrics" { + to = 9102 + } + } + + service { + provider = "consul" + port = "5678" + + meta { + envoy_metrics_port = "${NOMAD_HOST_PORT_envoy_metrics}" + } + + connect { + sidecar_service { + proxy { + config { + protocol = "http" + } + expose { + path { + path = "/metrics" + protocol = "http" + local_path_port = 9102 + listener_port = "envoy_metrics" + } + } + transparent_proxy {} + } + } + } + + tags = [ + "traefik.enable=true", + + "traefik.http.routers.n8n.rule=Host(`n8n.brmartin.co.uk`)", + "traefik.http.routers.n8n.entrypoints=websecure", + "traefik.consulcatalog.connect=true", + ] + } + + volume "data" { + type = "csi" + read_only = false + source = "martinibar_prod_n8n_data" + attachment_mode = "file-system" + access_mode = "multi-node-single-writer" + } + + task "n8n" { + driver = "docker" + + config { + image = "docker.n8n.io/n8nio/n8n:1.97.0" + } + + env = { + DB_TYPE = "postgresdb" + DB_POSTGRESDB_DATABASE = "n8n" + DB_POSTGRESDB_HOST = "martinibar.lan" + DB_POSTGRESDB_PORT = "5433" + DB_POSTGRESDB_USER = "n8n" + DB_POSTGRESDB_SCHEMA = "n8n" + } + + template { + data = <<-EOF + {{ with nomadVar "nomad/jobs/n8n/n8n/n8n" }} + DB_POSTGRESDB_PASSWORD={{.db_password}} + {{ end }} + EOF + + destination = "secrets/file.env" + env = true + } + + resources { + cpu = 500 + memory = 1024 + } + + volume_mount { + volume = "data" + destination = "/home/node/.n8n" + } + } + } +} diff --git a/modules/n8n/main.tf b/modules/n8n/main.tf new file mode 100644 index 0000000..f9872ae --- /dev/null +++ b/modules/n8n/main.tf @@ -0,0 +1,35 @@ +resource "nomad_job" "n8n" { + depends_on = [ + nomad_csi_volume_registration.nfs_volume, + ] + + jobspec = file("${path.module}/jobspec.nomad.hcl") +} + +data "nomad_plugin" "nfs" { + plugin_id = "nfs" + wait_for_healthy = true +} + +resource "nomad_csi_volume_registration" "nfs_volume" { + depends_on = [data.nomad_plugin.nfs] + + lifecycle { + prevent_destroy = true + } + + plugin_id = "nfs" + name = "martinibar_prod_n8n_data" + volume_id = "martinibar_prod_n8n_data" + external_id = "martinibar_prod_n8n_data" + + capability { + access_mode = "multi-node-single-writer" + attachment_mode = "file-system" + } + + context = { + "server" = "martinibar.lan", + "share" = "/volume1/csi/n8n/data", + } +}