Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add consul-nomad setup #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions consul-nomad/frontend.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
job "frontend" {
datacenters = ["dc1"]

type = "service"

group "frontend" {
count = 1

scaling {
enabled = true
min = 1
max = 3
}

consul {
namespace = "hashicups"
}

update {
max_parallel = 1
min_healthy_time = "30s"
healthy_deadline = "1m"
}

network {
mode = "bridge"
port "http" {
to = 3000
}
port "envoy_metrics" {
to = 9102
}
}

service {
name = "frontend"
tags = ["hashicups", "demo"]

port = "http"

meta {
envoy_metrics_port = "${NOMAD_HOST_PORT_envoy_metrics}"
}

connect {
sidecar_service {
proxy {
upstreams {
destination_name = "public-api"
local_bind_port = 8080
}
config {
envoy_prometheus_bind_addr = "0.0.0.0:9102"
}
}
}
}
}

task "frontend" {
driver = "docker"

config {
// https://hub.docker.com/r/hashicorpdemoapp/frontend/tags
image = "hashicorpdemoapp/frontend:v1.0.3"
ports = ["http"]
volumes = [
"local:/etc/nginx/conf.d",
]
}

template {
data = <<EOH
{{- range service "nginx" }}
NEXT_PUBLIC_PUBLIC_API_URL="http://{{ .Address }}:{{ .Port }}"
{{- end }}
EOH

destination = "local/file.env"
env = true
}

env {
// NEXT_PUBLIC_PUBLIC_API_URL = "/"
PORT = "${NOMAD_PORT_http}"
}

resources {
cpu = 200
memory = 512
}

template {
data = <<EOH
# /etc/nginx/conf.d/default.conf
server {
listen {{ env "NOMAD_PORT_http" }};
server_name {{ env "NOMAD_IP_http" }};
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# Proxy pass the api location to save CORS
# Use location exposed by Consul connect
location /api {
proxy_pass http://127.0.0.1:{{ env "NOMAD_UPSTREAM_PORT_public_api" }};
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
EOH
destination = "local/default.conf"
change_mode = "signal"
change_signal = "SIGHUP"
}

logs {
max_files = 10
max_file_size = 10
}
}
}
}
105 changes: 105 additions & 0 deletions consul-nomad/nginx.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
job "nginx" {
datacenters = ["dc1"]

group "nginx" {

// consul {
// namespace = "hashicups"
// }

count = 1

network {
port "http" {}
}

service {
name = "nginx"
port = "http"
}

task "nginx" {
driver = "docker"

config {
image = "nginx"
ports = ["http"]
volumes = [
"local:/etc/nginx/conf.d",
]
}

artifact {
source = "https://img.icons8.com/color/2x/nginx.png"
destination = "local/upload"
}

// env {
// CONSUL_NAMESPACE = "hashicups"
// }

template {
data = <<EOF
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;

upstream frontend {
{{- range service "frontend" }}
server {{ .Address }}:{{ .Port }};
{{- else }}# server 127.0.0.1:65535; # force a 502
{{- end }}
}

upstream api {
{{- range service "public-api" }}
server {{ .Address }}:{{ .Port }};
{{- else }}# server 127.0.0.1:65535; # force a 502
{{- end }}
}

server {
listen {{ env "NOMAD_PORT_http" }};

location / {
proxy_pass http://frontend;
}
location /api {
proxy_pass http://api;
}
location /status {
stub_status on;
}
location /_next/static {
proxy_cache STATIC;
proxy_pass http://frontend;
# For testing cache - remove before deploying to production
add_header X-Cache-Status $upstream_cache_status;
}
location /static {
proxy_cache STATIC;
proxy_ignore_headers Cache-Control;
proxy_cache_valid 60m;
proxy_pass http://frontend;
# For testing cache - remove before deploying to production
add_header X-Cache-Status $upstream_cache_status;
}

server_tokens off;
gzip on;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css application/javascript image/svg+xml;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
EOF

destination = "local/load-balancer.conf"
change_mode = "signal"
change_signal = "SIGHUP"
}
}
}
}
79 changes: 79 additions & 0 deletions consul-nomad/payments.nomad
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
job "payments-api" {
datacenters = ["dc1"]

type = "service"

group "payments-api" {

// consul {
// namespace = "hashicups"
// }

network {
mode = "bridge"
port "api" {
to = 8080
}
port "envoy_metrics" {
to = 9102
}
}

service {
name = "payments-api"
tags = ["hashicups", "demo"]

port = "api"

meta {
envoy_metrics_port = "${NOMAD_HOST_PORT_envoy_metrics}"
}

connect {
sidecar_service {
proxy {
config {
envoy_prometheus_bind_addr = "0.0.0.0:9102"
}
}
}
}
}

task "payments-api" {
driver = "docker"

config {
// https://hub.docker.com/r/hashicorpdemoapp/frontend/tags
image = "hashicorpdemoapp/payments:v0.0.12"
ports = ["api"]
mount {
type = "bind"
source = "local/application.properties"
target = "/application.properties"
}
}

template {
data = "server.port={{ env \"NOMAD_PORT_api\"}}"
destination = "local/application.properties"
}

resources {
cpu = 200
memory = 512
}

logs {
max_files = 10
max_file_size = 10
}
}

scaling {
enabled = true
min = 1
max = 3
}
}
}
Loading