Discovery with A Records
DNS A records map hostnames to IP addresses. You can configure HAProxy Enterprise to query for these records and populate server
lines from the information that returns.
![[DNS A records]](https://cdn.haproxy.com/documentation/hapee/1-9r1/assets/dns_a_records-717efc4e17f46f28d56f903d8ca1c5cc06566e32d93baf71ecd328ae5e5163a5.png)
Configure service discovery
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 directly with the
dig
tool to ensure that it returns the correct records, as follows:$ 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
Multiple IP addresses should return for the same hostname.
-
In your HAProxy Enterprise configuration, 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
dns1192.168.50.30:53 accepted_payload_size 8192 -
Use a
server-template
in abackend
to set the template for theserver
lines. When HAProxy Enterprise queries your DNS servers, these will be populated with IP addresses.backend webservers balance roundrobin server-template
web5 myservice.example.local:80 check resolvers mydns init-addr noneIn 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 hostname myservice.example.local is queried.
Port 80 is hardcoded.
The
resolvers
section mydns is specified.The
init-addr none
argument means that HAProxy Enterprise can initialize without having to resolve the IP addresses at startup. They can be resolved during runtime.
After converting the DNS records to
server
lines, it is equivalent to defining abackend
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 192.168.50.34:80 check disabled server web5 192.168.50.35:80 check disabled
When you add more DNS records to your nameserver, they will automatically go into the backend to fill in the web4 and web5 slots.
Inspect servers with the Runtime API
Run the command show servers state [backend name]
to see the servers loaded into memory:
$ echo "show servers state webservers" | sudo socat stdio /var/run/hapee-1.9/hapee-lb.sock # be_id be_name srv_id srv_name srv_addr srv_fqdn srv_port srvrecord 3 servers 1 web1 192.168.50.33 myservice.example.local 80 - 3 servers 2 web2 192.168.50.31 myservice.example.local 80 - 3 servers 3 web3 192.168.50.32 myservice.example.local 80 - 3 servers 4 web4 - myservice.example.local 80 - 3 servers 5 web5 - myservice.example.local 80 -
This example shows five servers: web1, web2, web3, web4 and web5.
The DNS records returned IP addresses for only three servers.
When there are more servers generated from the template than there are DNS records, the extra servers do not get an assigned IP address; they show a dash where an IP address would normally go.
The extra server lines start in maintenance mode and will pick up IP addresses as more DNS records return.
Next up
Discovery with SRV Records