Skip to content

Instantly share code, notes, and snippets.

@mr-karan
Created December 16, 2022 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mr-karan/13dc277346631ae11d86a0a1dac06a2b to your computer and use it in GitHub Desktop.
Save mr-karan/13dc277346631ae11d86a0a1dac06a2b to your computer and use it in GitHub Desktop.
Nomad template reload with raw_exec

This issue is described in hashicorp/nomad#5459 but this is a really simple to reproduce version of the same.

nomad_sighup is a really simple Go program, which listens for SIGHUP signal to be sent and prints "Received SIGHUP signal". It listens for this signal in an infinite for-loop.

The nomad deployment specs, deploys the same binary with raw_exec driver. There's a template block defined, which is a dummy configuration template. It watches for a Nomad service object doggo-web and updates whenever the Address/IP of this service updates. When that happens, Nomad is configured to send a SIGHUP signal to the underlying process. image

package main
import (
"fmt"
"os"
"os/signal"
"syscall"
)
func main() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP)
for {
sig := <-sigChan
switch sig {
case syscall.SIGHUP:
fmt.Println("Received SIGHUP signal")
}
}
}
job "sighup" {
datacenters = ["dc1"]
type = "service"
namespace = "default"
group "sighup" {
count = 1
restart {
attempts = 3
delay = "15s"
interval = "3m"
mode = "delay"
}
task "app" {
driver = "raw_exec"
# Dummy config which queries nomad services and changes everytime the service registration changes.
template {
data = <<EOF
{{- with nomadService "doggo-web" }}
backend doggo.internal
{{- range nomadService "doggo-web" }}
server "doggo-web-{{ .Address }}-{{ .Port }}" {{ .Address }}:{{ .Port }} check inter 5s weight 1
{{- end }}
{{ end }}
EOF
destination = "$${NOMAD_TASK_DIR}/dummy.conf"
change_mode = "signal"
change_signal = "SIGHUP"
}
# This is the Go program available in `PATH` to the raw_exec driver.
config {
command = "nomad_sighup"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment