Searching ALOHA 12.5
Service Discovery with A Records
Service Discovery with A Records
DNS A records map hostnames to IP addresses. You can configure HAProxy to query for these records and populate server
lines from the information that returns.
![[DNS A records]](/documentation/aloha/latest/assets/images/dns_a_records-f1f066e8cf100138e454a40fb9ab06e61b8d5fa9bd15cba9ea71bc607ab0e500.png)
Configure DNS A records
Update your DNS nameserver to resolve a hostname, such as myservice.example.local, to one or more IP addresses using A records.
Query the nameserver
-
Query the nameserver directly with the
dig
tool to ensure that it returns the correct records, as follows:Note
Multiple IP addresses should return for the same hostname.
$ dig @192.168.50.30 -p 53 A myservice.example.local
;; QUESTION SECTION: ;myservice.example.local. IN A ;; ANSWER SECTION: myservice.example.local. 90 IN A 192.168.50.31 myservice.example.local. 90 IN A 192.168.50.32 myservice.example.local. 90 IN A 192.168.50.33
Configure a resolvers section
You add a resolvers
section in your HAProxy configuration file to set the DNS nameservers for HAProxy to watch for changes.
Edit HAProxy's configuration file: browse the LB Layer 7 tab from the GUI.
Add a
resolvers
section.Add one or more
nameserver
lines to specify the IP addresses and ports of your DNS nameservers.Set the
accepted_payload_size
to 8192 to allow larger DNS payloads, which is required to receive more server IPs within a single DNS result.
resolvers mydns
nameserver dns1 192.168.50.30:53
accepted_payload_size 8192
Add a server-template
-
Use a
server-template
in abackend
to set the template for theserver
lines when HAProxy queries your DNS servers, as follows:server-template <prefix> <num | range> <fqdn>[:<port>] [params*]
where:
prefix
A string used as a prefix for each server name. (e.g. web)
num
The number of servers you want to generate automatically. This number will increment and append to the
prefix
of each server when forming its name.range
The number of servers you want to generate automatically, specified as starting and ending numbers to append to the
prefix
of each server when forming its name.fqdn
The fully-qualified domain name used to query the nameservers.
port
A hardcoded port number.
params*
Additional
server
parameters.backend webservers balance roundrobin server-template web 5 myservice.example.local:80 check resolvers mydns init-addr none
In this example, the
server-template
directive:Adds the specified number of servers (5) to the backend.
Their names will have "web" as a prefix.
The host name myservice.example.local is queried.
Port 80 is hardcoded.
The
resolvers
section mydns is specified.The
init-addr none
argument means that HAProxy can initialize without having to resolve the IP addresses at startup. They can be resolved during runtime.
When HAProxy reads this DNS result, it is equivalent to defining a
backend
statically that looks like this:backend webservers balance roundrobin server web1 192.168.50.31:80 check server web2 192.168.50.32:80 check server web3 192.168.50.33:80 check server web4 check disabled server web5 check disabled
When you add more records to your nameserver, they will automatically go into the backend to fill in the web4 and web5 slots.