Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active January 29, 2021 14:35
Show Gist options
  • Save haproxytechblog/9c3f60236849859d6ed3596a7a0a8335 to your computer and use it in GitHub Desktop.
Save haproxytechblog/9c3f60236849859d6ed3596a7a0a8335 to your computer and use it in GitHub Desktop.
HAProxy and Consul with DNS for Service Discovery
consul agent -server -ui -bootstrap-expect=1 -data-dir=/var/lib/consul \
-node=server -bind=0.0.0.0 -config-dir=/etc/consul.d -client=0.0.0.0 \
-http-port=80 -domain=consul.itchy.local
{
"dns_config": {
"enable_truncate": true,
"udp_answer_limit": 100
}
}
{
"service": {
"ID": "consul-server",
"Name": "gui",
"Address": "dashboard.consul.itchy.local",
"Port": 80,
"check": {
"http": "http://dashboard.consul.itchy.local",
"interval": "10s",
"timeout": "1s"
}
}
}
consul agent -data-dir=/var/lib/consul -node=$HOSTNAME -node-id=$(uuidgen) \
-bind=0.0.0.0 -config-dir=/etc/consul.d \
-retry-join <IP addr or hostname of consul agent server>
consul-template -template="/haproxy.conf.tmpl:/haproxy.conf:/haproxy_reload.sh" -log-level=debug
resolvers consul
nameserver consul 127.0.0.1:8600
accepted_payload_size 8192
{{range services}}{{$servicename := .Name}}{{$nbsrvkeyname := printf "service/haproxy/backend/%s/nbsrv" $servicename}}
backend b_{{$servicename}}.{{key "service/haproxy/domainname"}}
server-template {{$servicename}} {{keyOrDefault $nbsrvkeyname "10"}} _{{$servicename}}._tcp.service.consul resolvers consul resolve-prefer ipv4 check
{{end}}
consul agent -data-dir=/var/lib/consul -node=$HOSTNAME -node-id=$(uuidgen) \
-bind=0.0.0.0 -config-dir=/etc/consul.d \
-retry-join <IP addr or hostname of consul agent server>
{
"service": {
"ID": "<your server hostname>",
"Name": "<the name of the service delivered by this server>",
"Address": "<local IP address where the service is available>",
"Port": 80,
"check": {
"http": "http://<local IP address where the service is available>:80",
"interval": "10s",
"timeout": "1s"
}
}
}
use_backend b_%[req.hdr(Host),lower,word(1,:)]
{{range services}}{{$servicename := .Name}}
backend b_{{$servicename}}.{{key "service/haproxy/domainname"}}
[...]
{{end}}
@smilelikeshit
Copy link

thank you nice tutorial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment