This is a simple helper utility that performs a DNS lookup of a list of hostnames and uses them to instantiate a handlebars template.
See handlebars-rust for details of the supported helpers.
Pair this with a HAProxy reload script and you can have a HAProxy docker image automatically discover docker-compose
scaled services.
If we have a handlebars template file like this
frontend app
use_backend api if { path_beg /api }
use_backend ui if { path_beg /ui }
backend api
mode http
{{#each backend}}
server {{{this}}} {{{this}}}:8080 check
{{/each}}
backend ui
mode http
{{#each frontend}}
server {{{this}}} {{{this}}}:8888 check
{{/each}}
Then running:
$ dns-watch -v frontend:frontend-service -v backend:backend-service -o haproxy.conf haproxy.conf.mustache
Will perform two DNS lookups, one for frontend-service
and one for backend-service
and write haproxy.conf
and we will end up with something that looks like:
frontend app
use_backend api if { path_beg /api }
use_backend ui if { path_beg /ui }
backend api
mode http
server 10.0.0.4 10.0.0.4:8080 check
server 10.0.0.7 10.0.0.7:8080 check
server 10.0.0.8 10.0.0.8:8080 check
backend ui
mode http
server 10.0.0.5 10.0.0.5:8888 check
server 10.0.0.6 10.0.0.6:8888 check
If you want to watch for changes then you can specify a command to execute every time there is a change:
$ dns-watch -v frontend:frontend-service -v backend:backend-service -o haproxy.conf -w reload_haproxy.sh haproxy.conf.hbs
Which watch for DNS changes every second and run the reload_haproxy.sh
if the template is written.