Skip to content

Instantly share code, notes, and snippets.

@mr-karan
Last active December 26, 2023 10:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr-karan/b1bb4f65ae31d91985e6a64451b79f6e to your computer and use it in GitHub Desktop.
Save mr-karan/b1bb4f65ae31d91985e6a64451b79f6e to your computer and use it in GitHub Desktop.
Single Node nomad and consul
job "hello-world" {
datacenters = ["dc1"]
namespace = "default"
type = "service"
group "redis" {
# Specify number of replicas of redis needed.
count = 1
# Specify networking for the group, port allocs.
network {
# Bridge is an isolated network namespace created for this group.
# Applications outside this mode don't have access to this namespace.
mode = "bridge"
port "redis-tcp" {
# Map the port to task.
to = 6379
host_network = "private"
}
}
# If unhealthy, restart 3 times in 2 minutes with a delay of 15s between each restart.
# If it still fails, it'll be marked as unhealthy and won't be restarted again.
restart {
attempts = 3
interval = "2m"
delay = "15s"
mode = "fail"
}
# If alloc is failed, the group will be rescheduled with a preference to a node that
# hasn't been used previously.
reschedule {
attempts = 15
interval = "1h"
delay = "30s"
delay_function = "exponential"
max_delay = "120s"
unlimited = false
}
# Register a service with Consul.
service {
name = "redis"
port = "6379"
tags = ["cache", "redis"]
# Specify a health check. Since Consul Connect cannot do a TCP check
# we need to add a script check here. This check is executed from the task
# namespace itself and each check takes about a full second to come up and tear
# down, so be mindful when keeping timeouts.
check {
name = "ping-pong"
type = "script"
command = "redis-cli"
args = ["ping"]
task = "redis"
interval = "15s"
timeout = "7s"
}
# Expose an envoy proxy.
# No upstream is defined here, this is just for Envoy proxy in app to communicate with Envoy proxy in redis.
connect {
sidecar_service {
}
}
}
task "redis" {
driver = "docker"
config {
image = "redis:6"
ports = ["redis-tcp"]
}
resources {
cores = 1
memory = 256
}
}
}
group "app" {
network {
port "http" {
to = 8080
}
mode = "bridge"
}
service {
name = "app-proxy-sidecar"
port = "3000"
check {
name = "check healthcheck endpoint"
type = "http"
port = "http"
path = "/healthz"
interval = "30s"
timeout = "7s"
}
# Expose an envoy proxy. Define upstream as redis.
connect {
sidecar_service {
proxy {
config {
protocol = "tcp"
}
upstreams {
destination_name = "redis"
local_bind_port = 6379
}
}
}
}
}
restart {
attempts = 3
interval = "2m"
delay = "15s"
mode = "fail"
}
task "app" {
driver = "docker"
env {
DEMO_REDIS_ADDR = "localhost:6379"
}
config {
image = "registry.service.consul:32000/hello-app:1.1.0"
ports = ["http"]
// command = "sleep"
// args = [
// "infinity"
// ]
}
resources {
cores = 1
memory = 512
}
}
}
}
  • Get Nomad from here.

  • Install CNI plugin from and ensure it's available inside /opt/cni/bin. Follow docs for more details.

  • Run the local agent:

sudo nomad agent -config=nomad.hcl
  • Check node status
nomad node status
  • Run example deployment
nomad init
nomad run example.nomad
datacenter = "dc1"
data_dir = "/opt/nomad/data"
log_level = "DEBUG"
bind_addr = "0.0.0.0"
server {
enabled = true
bootstrap_expect = 1
}
client {
enabled = true
meta {
env = "dev"
stack = "localtests"
}
}
plugin "docker" {
config {
allow_privileged = true
volumes {
enabled = true
}
extra_labels = ["job_name", "job_id", "task_group_name", "task_name", "namespace", "node_name", "node_id"]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment