Last active
January 29, 2021 14:35
-
-
Save haproxytechblog/9c3f60236849859d6ed3596a7a0a8335 to your computer and use it in GitHub Desktop.
HAProxy and Consul with DNS for Service Discovery
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
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 |
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
{ | |
"dns_config": { | |
"enable_truncate": true, | |
"udp_answer_limit": 100 | |
} | |
} |
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
{ | |
"service": { | |
"ID": "consul-server", | |
"Name": "gui", | |
"Address": "dashboard.consul.itchy.local", | |
"Port": 80, | |
"check": { | |
"http": "http://dashboard.consul.itchy.local", | |
"interval": "10s", | |
"timeout": "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
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> |
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
consul-template -template="/haproxy.conf.tmpl:/haproxy.conf:/haproxy_reload.sh" -log-level=debug |
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
resolvers consul | |
nameserver consul 127.0.0.1:8600 | |
accepted_payload_size 8192 |
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
{{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}} |
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
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> |
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
{ | |
"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" | |
} | |
} | |
} |
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
use_backend b_%[req.hdr(Host),lower,word(1,:)] |
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
{{range services}}{{$servicename := .Name}} | |
backend b_{{$servicename}}.{{key "service/haproxy/domainname"}} | |
[...] | |
{{end}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you nice tutorial