Discovery with SRV Records
DNS SRV records specify the host and port that a service listens on. You can configure HAProxy Enterprise to query for these records and populate the IP addresses and ports of server
lines in a backend
section.
![[DNS SRV records]](https://cdn.haproxy.com/documentation/hapee/2-0r1/assets/dns_srv_records-c9060267f6fd3cdd7d9f19134226b32612cc97f3d6e231e96cbf4512d3012982.png)
About DNS SRV records
DNS SRV records are resources used to identify computers that host specific services. They are contained in the ANSWER section of DNS responses and have the following structure:
_service._proto.name. TTL class SRV priority weight port target
where:
| Standard network service name (taken from /etc/services) or a port number |
| Standard protocol name ("tcp" or "udp") |
| Name of the service, i.e. the name used in the query |
| Validity period for the response (HAProxy Enterprise ignores this field because it maintains its own expiry data defined in the configuration) |
| DNS class ("IN") |
| DNS record type ("SRV") |
| Priority of the target host. Lower value = higher preference (HAProxy Enterprise ignores this field but may use it later to indicate active / backup state) |
| Relative weight in case of records with the same priority. Higher number = higher preference |
| Port where the service is configured |
| Hostname of the machine providing the service, ending in a dot |
Configure service discovery
-
Update your DNS nameserver to resolve a service name, such as myservice.example.local, to one or more hostnames and ports via SRV records. Those hostnames should resolve to IP addresses using A records.
Add DNS A records that resolve multiple hostnames, such as host1, host2 and host3, to different IP addresses.
Add the same number of SRV records that resolve a service name, such as _myservice._tcp.example.local, to the hostnames you defined and the port on the host where the service listens.
-
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 SRV _myservice._tcp.example.local ;; QUESTION SECTION: ;_myservice._tcp.example.local. IN ;; ANSWER SECTION: _myservice._tcp.example.local. 0 IN SRV 0 0 8080 host1. _myservice._tcp.example.local. 0 IN SRV 0 0 8081 host2. _myservice._tcp.example.local. 0 IN SRV 0 0 8082 h ;; ADDITIONAL SECTION: host1. 0 IN A 192.168.50.31 host2. 0 IN A 192.168.50.32 host3. 0 IN A 192.168.50.33
-
Add a
resolvers
section in your HAProxy Enterprise configuration file to set the DNS nameservers to watch for changes.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, as follows:backend webservers balance roundrobin server-template
web5 _myservice._tcp.example.local resolvers mydns check init-addr noneIn this example, the
server-template
directive:Adds the specified number of servers (5) to the backend.
Appends "web" as a prefix to their names.
Queries the service name _myservice._tcp.example.local.
Have the SRV records fill in the ports.
Specifies the
resolvers
sectionmydns
The
init-addr none
argument means that HAProxy Enterprise can initialize without having to resolve the IP addresses at startup. It can resolve them during runtime.
This is equivalent to adding a
backend
to HAProxy Enterprise that looks like this:backend webservers balance roundrobin server web1 192.168.50.31:8080 check server web2 192.168.50.32:8081 check server web3 192.168.50.33:8082 check server web4 192.168.50.34:80 check disabled server web5 192.168.50.35:80 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.
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 unix-connect:/var/run/hapee-2.0/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 host3 8082 _myservice._tcp.example.local 3 servers 2 web2 192.168.50.32 host2 8081 _myservice._tcp.example.local 3 servers 3 web3 192.168.50.31 host1 8080 _myservice._tcp.example.local 3 servers 4 web4 - - 0 _myservice._tcp.example.local 3 servers 5 web5 - - 0 _myservice._tcp.example.local
Next up
Multi-threading