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]

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:


    Multiple IP addresses should return for the same hostname.

    $ dig @ -p 53 A myservice.example.local
    ;myservice.example.local.    IN  A
    myservice.example.local. 90  IN  A
    myservice.example.local. 90  IN  A
    myservice.example.local. 90  IN  A

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.

  1. Edit the HAProxy configuration file hapee-lb.cfg

  2. Add a resolvers section.

  3. Add one or more nameserver lines to specify the IP addresses and ports of your DNS nameservers.

  4. 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
    accepted_payload_size 8192

Add a server-template

  • Use a server-template in a backend to set the template for the server lines when HAProxy queries your DNS servers, as follows:

    server-template <prefix> <num | range> <fqdn>[:<port>] [params*]



    A string used as a prefix for each server name. (e.g. web)


    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.


    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.


    The fully-qualified domain name used to query the nameservers.


    A hardcoded port number.


    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 check
        server web2 check
        server web3 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.

Check server status

You can monitor the status of these servers on the HAProxy Stats page, which shows the following:

[Stats page]
  • Only one server is up (green) and actually listening.

  • The next two servers are down (red) due to failed health checks.

  • The last two servers, which don't have assigned IP addresses, are in maintenance mode and can't receive traffic.


To display a tooltip to indicate the server's IP address and port, add a line with stats show legends to the frontend section of the configuration file.

Inspect servers with HAProxy's Runtime API

  1. To enable the HAProxy Runtime API, add a stats socket directive to the global section of your configuration file:

        log stdout local0
        stats socket :9000 mode 660 level admin
  2. To see the servers loaded into memory, run the command show servers state servers, as follows:

    $ echo "show servers state servers" | nc localhost 9000
    # be_id  be_name  srv_id  srv_name  srv_addr       srv_fqdn                srv_port srvrecord
    3        servers  1       web1  myservice.example.local 80       -
    3        servers  2       web2  myservice.example.local 80       -
    3        servers  3       web3  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.

    • However, 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.

    • These servers start in maintenance mode and will pick up IP addresses as more DNS records return.