Skip to content

Instantly share code, notes, and snippets.

@carljavier
Last active February 22, 2022 04:06
Show Gist options
  • Save carljavier/3d397e2643af2fb37daf50c1a036d780 to your computer and use it in GitHub Desktop.
Save carljavier/3d397e2643af2fb37daf50c1a036d780 to your computer and use it in GitHub Desktop.
Nomad Demo Job
variable "public_api_ip" {
type = string
description = "Public Host IP Address"
default = "localhost"
}
job "hashicups" {
datacenters = ["dc1"]
group "frontend" {
network {
mode = "bridge"
port "http" {
static = 80
}
}
service {
name = "frontend"
port = "${NOMAD_PORT_http}"
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "product-public-api"
local_bind_port = 8080
}
}
}
}
}
task "frontend" {
driver = "docker"
env {
PORT = "${NOMAD_PORT_http}"
NEXT_PUBLIC_PUBLIC_API_URL="http://${var.public_api_ip}:8080"
}
config {
image = "hashicorpdemoapp/frontend:v1.0.1"
ports = ["http"]
}
}
}
group "product-public-api" {
network {
mode = "bridge"
port "http" {
static = 8080
}
}
service {
name = "product-public-api"
port = "${NOMAD_PORT_http}"
tags = ["urlprefix-/"]
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "product-api"
local_bind_port = 5000
}
upstreams {
destination_name = "payment-api"
local_bind_port = 5001
}
}
}
}
}
task "product-public-api" {
driver = "docker"
config {
image = "hashicorpdemoapp/public-api:v0.0.6"
}
env {
PRODUCT_API_URI = "http://localhost:5000"
PAYMENT_API_URI = "http://localhost:5001"
}
}
}
group "payment-api" {
network {
mode = "bridge"
}
service {
name = "payment-api"
port = "8080"
connect {
sidecar_service {}
}
}
task "payment-api" {
driver = "docker"
config {
image = "hashicorpdemoapp/payments:v0.0.16"
}
}
}
group "product-api" {
network {
mode = "bridge"
port "healthcheck" {
to = -1
}
}
service {
name = "product-api"
port = "9090"
check {
type = "http"
path = "/health"
interval = "5s"
timeout = "2s"
expose = true
port = "healthcheck"
}
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "product-db"
local_bind_port = 5000
}
}
}
}
}
task "product-api" {
driver = "docker"
config {
image = "hashicorpdemoapp/product-api:v0.0.20"
}
env {
CONFIG_FILE = "/config/config.json"
DB_CONNECTION = "host=localhost port=5000 user=postgres password=password dbname=products sslmode=disable"
BIND_ADDRESS = "0.0.0.0:9090"
}
}
}
group "product-db" {
network {
mode = "bridge"
}
service {
name = "product-db"
port = "5432"
connect {
sidecar_service {}
}
}
task "db" {
driver = "docker"
config {
image = "hashicorpdemoapp/product-api-db:v0.0.20"
}
env {
POSTGRES_DB = "products"
POSTGRES_USER = "postgres"
POSTGRES_PASSWORD = "password"
}
}
}
}
job "hashicups" {
datacenters = ["dc1"]
group "frontend" {
network {
mode = "bridge"
port "http" {
static = 80
}
}
service {
name = "frontend"
port = "80"
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "product-public-api"
local_bind_port = 8080
}
}
}
}
}
task "frontend" {
driver = "docker"
config {
image = "hashicorpdemoapp/frontend:v0.0.8.static"
ports = ["http"]
}
}
}
group "product-public-api" {
network {
mode = "bridge"
}
service {
name = "product-public-api"
port = "8080"
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "product-api"
local_bind_port = 5000
}
upstreams {
destination_name = "payment-api"
local_bind_port = 5001
}
}
}
}
}
task "product-public-api" {
driver = "docker"
config {
image = "hashicorpdemoapp/public-api:v0.0.19"
}
env {
PRODUCT_API_URI = "http://localhost:5000"
PAYMENT_API_URI = "http://localhost:5001"
}
}
}
group "payment-api" {
network {
mode = "bridge"
}
service {
name = "payment-api"
port = "8080"
connect {
sidecar_service {}
}
}
task "payment-api" {
driver = "docker"
config {
image = "hashicorpdemoapp/payments:v0.0.16"
}
}
}
group "product-api" {
network {
mode = "bridge"
port "healthcheck" {
to = -1
}
}
service {
name = "product-api"
port = "9090"
check {
type = "http"
path = "/health"
interval = "5s"
timeout = "2s"
expose = true
port = "healthcheck"
}
connect {
sidecar_service {
proxy {
upstreams {
destination_name = "product-db"
local_bind_port = 5000
}
}
}
}
}
task "product-api" {
driver = "docker"
config {
image = "hashicorpdemoapp/product-api:v0.0.19"
}
env {
CONFIG_FILE = "/config/config.json"
DB_CONNECTION = "host=localhost port=5000 user=postgres password=password dbname=products sslmode=disable"
BIND_ADDRESS = "0.0.0.0:9090"
}
}
}
group "product-db" {
network {
mode = "bridge"
}
service {
name = "product-db"
port = "5432"
connect {
sidecar_service {}
}
}
task "db" {
driver = "docker"
config {
image = "hashicorpdemoapp/product-api-db:v0.0.19"
}
env {
POSTGRES_DB = "products"
POSTGRES_USER = "postgres"
POSTGRES_PASSWORD = "password"
}
}
}
}
@carljavier
Copy link
Author

New frontend container switched the public_api call from being server side to client side.

This means the browser will make the call to the public_api endpoint and therefore we need to expose the public_api service on the nomad host it runs on.

Didn't want to introduce a load balancer like fabio or nginx to create further complexity to this demo nomad job using hashicups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment