Last active
April 21, 2024 16:05
-
-
Save m1keil/d0ef68c4277712a5b0ce2cf74743f18e to your computer and use it in GitHub Desktop.
DNS based service discovery for Nomad (Using CoreDNS). Workaround for https://github.com/hashicorp/nomad/issues/12588
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.: { | |
log | |
loadbalance | |
# auto: automatically pick up file changes | |
auto service.nomad { | |
directory local/zones | |
reload 1s | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$TTL 0 | |
$ORIGIN service.nomad. | |
@ IN SOA ns1.service.nomad. hostmaster.service.nomad.( | |
{{ timestamp "unix" }} ; Serial number | |
28800 ; Refresh period (8 hours) | |
7200 ; Retry interval (2 hour) | |
864000 ; Expiration time (10 days) | |
3600 ; Minimum TTL (1 hour) | |
) | |
IN NS ns1.service.nomad. | |
{{- $rr_a := sprig_list -}} | |
{{- $rr_srv := sprig_list -}} | |
{{- /* Iterate over all of the registered Nomad services */ -}} | |
{{- range nomadServices -}} | |
{{ $service := . }} | |
{{- /* Iterate over all of the instances of a services */ -}} | |
{{- range nomadService $service.Name -}} | |
{{ $svc := . }} | |
{{- /* Generate a uniq label for IP */ -}} | |
{{- $node := $svc.Address | md5sum | sprig_trunc 8 }} | |
{{- /* Record A & SRV RRs */ -}} | |
{{- $rr_a = sprig_append $rr_a (sprig_list $svc.Name $svc.Address) -}} | |
{{- $rr_a = sprig_append $rr_a (sprig_list $node $svc.Address) -}} | |
{{- $rr_srv = sprig_append $rr_srv (sprig_list $svc.Name $svc.Port $node) -}} | |
{{- /* Iterate over tags */ -}} | |
{{- range $svc.Tags -}} | |
{{- $tag := . -}} | |
{{- /* Record A & SRV RRs */ -}} | |
{{- $rr_a = sprig_append $rr_a (sprig_list (printf "%s.%v" $tag $svc.Name) $svc.Address) -}} | |
{{- $rr_srv = sprig_append $rr_srv (sprig_list (printf "%s.%v" $tag $svc.Name) $svc.Port $node) -}} | |
{{- end -}} | |
{{- end -}} | |
{{- end -}} | |
{{- /* Iterate over lists and print everything */ -}} | |
{{- range $rr_a | sprig_uniq -}} | |
{{ printf "%-45s %s %4s %s" (index . 0) "IN" "A" (sprig_last . ) }} | |
{{ end }} | |
{{ range $rr_srv -}} | |
{{ printf "%-45s %s %4s %d %d %6d %s" (index . 0) "IN" "SRV" 0 0 (index . 1) (index . 2) }} | |
{{ end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$TTL 0 | |
$ORIGIN service.nomad. | |
@ IN SOA ns1.service.nomad. hostmaster.service.nomad.( | |
1681488575 ; Serial number | |
28800 ; Refresh period (8 hours) | |
7200 ; Retry interval (2 hour) | |
864000 ; Expiration time (10 days) | |
3600 ; Minimum TTL (1 hour) | |
) | |
IN NS ns1.service.nomad. | |
dc865212 IN A 172.30.28.31 | |
6c7eb8e0 IN A 172.30.59.68 | |
b8d6f155 IN A 172.30.46.148 | |
whoami IN A 172.30.46.148 | |
bar.whoami IN A 172.30.46.148 | |
foo.whoami IN A 172.30.46.148 | |
baz.whoami IN A 172.30.46.148 | |
whoami IN A 172.30.59.68 | |
bar.whoami IN A 172.30.59.68 | |
foo.whoami IN A 172.30.59.68 | |
baz.whoami IN A 172.30.59.68 | |
whoami IN SRV 0 0 23396 b8d6f155 | |
bar.whoami IN SRV 0 0 23396 b8d6f155 | |
foo.whoami IN SRV 0 0 23396 b8d6f155 | |
whoami IN SRV 0 0 31486 b8d6f155 | |
baz.whoami IN SRV 0 0 31486 b8d6f155 | |
foo.whoami IN SRV 0 0 31486 b8d6f155 | |
whoami IN SRV 0 0 23242 6c7eb8e0 | |
bar.whoami IN SRV 0 0 23242 6c7eb8e0 | |
foo.whoami IN SRV 0 0 23242 6c7eb8e0 | |
whoami IN SRV 0 0 28488 6c7eb8e0 | |
baz.whoami IN SRV 0 0 28488 6c7eb8e0 | |
foo.whoami IN SRV 0 0 28488 6c7eb8e0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ nomad service info whoami | |
Job ID Address Tags Node ID Alloc ID | |
whoami 172.30.46.148:23396 [foo,bar] 3b23e0a5 45d301b5 | |
whoami 172.30.46.148:31486 [foo,baz] 3b23e0a5 45d301b5 | |
whoami 172.30.59.68:23242 [foo,bar] aa074fe2 506a2bf0 | |
whoami 172.30.59.68:28488 [foo,baz] aa074fe2 506a2bf0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've had a hard time with some empty service names breaking the DNS file. If anyone else comes accross this, simple add an
if
statement around the record block – or do whatever you want to skip the service with an empty name 😁