Skip to content

Commit

Permalink
Add example optional timescaledb and promscale job file
Browse files Browse the repository at this point in the history
  • Loading branch information
picatz committed May 31, 2021
1 parent 64da5ec commit a5dc5cf
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ DNS_ENABLED ?= false
PUBLIC_DOMAIN ?= ""
GRAFANA_LOAD_BALANCER_ENABLED ?= false
GRAFANA_PUBLIC_DOMAIN ?= ""
PROMSCALE_ENABLED ?= false

.PHONY: help
help: ## Print this help menu
Expand Down Expand Up @@ -173,7 +174,7 @@ consul/metrics/acls: ## Create a Consul policy, role, and token to use with prom

.PHONY: nomad/metrics
nomad/metrics: ## Runs a Prometheus and Grafana stack on Nomad
@nomad run -var='consul_targets=[$(shell terraform output -json | jq -r '(.server_internal_ips.value + .client_internal_ips.value) | map(.+":8501") | @csv')]' -var="consul_acl_token=$(consul_acl_token)" -var="consul_lb_ip=$(shell terraform output load_balancer_ip)" jobs/metrics/metrics.hcl
@nomad run -var='promscale=$(PROMSCALE_ENABLED)' -var='consul_targets=[$(shell terraform output -json | jq -r '(.server_internal_ips.value + .client_internal_ips.value) | map(.+":8501") | @csv')]' -var="consul_acl_token=$(consul_acl_token)" -var="consul_lb_ip=$(shell terraform output load_balancer_ip)" jobs/metrics/metrics.hcl

.PHONY: nomad/logs
nomad/logs: ## Runs a Loki and Promtail jobs on Nomad
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ Logs can also be collected within the cluster using Promtail and Loki, then visu
```console
$ DNS_ENABLED=true PUBLIC_DOMAIN="nomad.your-domain.com" make terraform/apply
...
$ export NOMAD_TOKEN=...
$ export CONSUL_HTTP_TOKEN=...
$ export CONSUL_HTTP_TOKEN=$(terraform output -json | jq -r .consul_master_token.value)
$ make consul/metrics/acls
...
🔑 Creating Consul ACL Token to Use for Prometheus Consul Service Discovery
Expand All @@ -105,6 +104,7 @@ $ consul_acl_token=2a1c7926-b6e3-566e-ddf5-b19279fa134e make nomad/metrics
$ make nomad/logs
$ make nomad/ingress
$ GRAFANA_PUBLIC_DOMAIN="grafana.your-domain.com" GRAFANA_LOAD_BALANCER_ENABLED=true DNS_ENABLED=true PUBLIC_DOMAIN="nomad.your-domain.com" make terraform/apply
$ open http://public.grafana.your-domain.com:3000/login
```

## Bootstrap ACL Token
Expand Down
98 changes: 98 additions & 0 deletions jobs/db/timescale.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// docker run -d --name timescaledb -p 5432:5432 -e POSTGRES_PASSWORD=password timescale/timescaledb:latest-pg12
variable "datacenters" {
type = list(string)
default = ["dc1"]
}

job "timescaledb" {
datacenters = var.datacenters

group "timescaledb" {
network {
mode = "bridge"
}

service {
name = "timescaledb"
port = "5432"

connect {
sidecar_service {}
}
}

ephemeral_disk {
size = 10240 # 10 GB
migrate = true
sticky = true
}

task "timescaledb" {
driver = "docker"

# Note, configuration is found at:
# /var/lib/postgresql/data/postgresql.conf

env {
POSTGRES_PASSWORD = "password"
}

config {
image = "timescale/timescaledb:latest-pg12"
}
}
}

group "promscale" {
network {
mode = "bridge"
}

service {
name = "promscale"
port = "9201"

connect {
sidecar_service {
proxy {
upstreams {
destination_name = "timescaledb"
local_bind_port = 5432
}
}
}
}
}

ephemeral_disk {
size = 10240 # 10 GB
migrate = true
sticky = true
}

task "promscale" {
driver = "docker"

env {
POSTGRES_PASSWORD = "password"

// PROMSCALE_WEB_TELEMETRY_PATH = "/metrics"
// PROMSCALE_DB_CONNECT_RETRIES = 10
// PROMSCALE_LOG_LEVEL = "info"
// PROMSCALE_DB_NAME = "timescale"
// PROMSCALE_DB_PORT = 5432
// PROMSCALE_DB_SSL_MODE = "allow"
// PROMSCALE_DB_HOST="127.0.0.1"
// PROMSCALE_DB_URI = ""
}

config {
image = "timescale/promscale:latest"

args = [
"-db-uri", "postgres://postgres:[email protected]:5432/postgres?sslmode=allow",
]
}
}
}
}
41 changes: 35 additions & 6 deletions jobs/metrics/metrics.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ variable "consul_cli_key" {
default = "consul-cli-key.pem"
}

// Consul services are registered as 8300, and only for servers by default. This makes it
// not easy/possible(?) to use Prometheus Consul SD to scrape Consul.
variable "promscale" {
type = bool
default = false
}

variable "consul_targets" {
type = list(string)
}
Expand All @@ -56,12 +59,23 @@ job "metrics" {
}

service {
name = "prometheus"
port = "9090"
name = "prometheus"
port = "9090"

connect {
sidecar_service {}
connect {
sidecar_service {
proxy {
dynamic "upstreams" {
for_each = var.promscale ? [1] : []

content {
destination_name = "promscale"
local_bind_port = "9201"
}
}
}
}
}
}

ephemeral_disk {
Expand Down Expand Up @@ -149,6 +163,13 @@ scrape_configs:
credentials: '${var.consul_acl_token}'
static_configs:
- targets: ${jsonencode(var.consul_targets)}
{{ if eq ${var.promscale} true }}
remote_write:
- url: "http://127.0.0.1:9201/write"
remote_read:
- url: "http://127.0.0.1:9201/read"
read_recent: true
{{ end }}
EOH
}

Expand Down Expand Up @@ -225,6 +246,14 @@ EOH
destination_name = "loki-http"
local_bind_port = 3100
}
dynamic "upstreams" {
for_each = var.promscale ? [1] : []

content {
destination_name = "promscale"
local_bind_port = "9201"
}
}
}
}
}
Expand Down

0 comments on commit a5dc5cf

Please sign in to comment.