5.3. Server IP address resolution using DNS
- HAProxy allows using a host name on the server line to retrieve its IP address
- using name servers. By default, HAProxy resolves the name when parsing the
- configuration file, at startup and cache the result for the process' life.
- This is not sufficient in some cases, such as in Amazon where a server's IP
- can change after a reboot or an ELB Virtual IP can change based on current
- workload.
- This chapter describes how HAProxy can be configured to process server's name
- resolution at run time.
- Whether run time server name resolution has been enable or not, HAProxy will
- carry on doing the first resolution when parsing the configuration.
5.3.1. Global overview
- As we've seen in introduction, name resolution in HAProxy occurs at two
- different steps of the process life:
- 1. when starting up, HAProxy parses the server line definition and matches a
- host name. It uses libc functions to get the host name resolved. This
- resolution relies on /etc/resolv.conf file.
- 2. at run time, HAProxy performs periodically name resolutions for servers
- requiring DNS resolutions.
- A few other events can trigger a name resolution at run time:
- - when a server's health check ends up in a connection timeout: this may be
- because the server has a new IP address. So we need to trigger a name
- resolution to know this new IP.
- When using resolvers, the server name can either be a hostname, or a SRV label.
- HAProxy considers anything that starts with an underscore as a SRV label. If a
- SRV label is specified, then the corresponding SRV records will be retrieved
- from the DNS server, and the provided hostnames will be used. The SRV label
- will be checked periodically, and if any server are added or removed, haproxy
- will automatically do the same.
- A few things important to notice:
- - all the name servers are queried in the meantime. HAProxy will process the
- first valid response.
- - a resolution is considered as invalid (NX, timeout, refused), when all the
- servers return an error.
5.3.2. The resolvers section
- This section is dedicated to host information related to name resolution in
- HAProxy. There can be as many as resolvers section as needed. Each section can
- contain many name servers.
- When multiple name servers are configured in a resolvers section, then HAProxy
- uses the first valid response. In case of invalid responses, only the last one
- is treated. Purpose is to give the chance to a slow server to deliver a valid
- answer after a fast faulty or outdated server.
- When each server returns a different error type, then only the last error is
- used by HAProxy. The following processing is applied on this error:
- 1. HAProxy retries the same DNS query with a new query type. The A queries are
- switch to AAAA or the opposite. SRV queries are not concerned here. Timeout
- errors are also excluded.
- 2. When the fallback on the query type was done (or not applicable), HAProxy
- retries the original DNS query, with the preferred query type.
- 3. HAProxy retries previous steps <resolve_retires> times. If no valid
- response is received after that, it stops the DNS resolution and reports
- the error.
- For example, with 2 name servers configured in a resolvers section, the
- following scenarios are possible:
- - First response is valid and is applied directly, second response is
- ignored
- - First response is invalid and second one is valid, then second response is
- applied
- - First response is a NX domain and second one a truncated response, then
- HAProxy retries the query with a new type
- - First response is a NX domain and second one is a timeout, then HAProxy
- retries the query with a new type
- - Query timed out for both name servers, then HAProxy retries it with the
- same query type
- As a DNS server may not answer all the IPs in one DNS request, haproxy keeps
- a cache of previous answers, an answer will be considered obsolete after
- <hold obsolete> seconds without the IP returned.
- Creates a new name server list labeled <resolvers id>
- A resolvers section accept the following parameters:
- Defines the maximum payload size accepted by HAProxy and announced to all the
- name servers configured in this resolvers section.
- <nb> is in bytes. If not set, HAProxy announces 512. (minimal value defined
- by RFC 6891)
- Note: the maximum allowed value is 8192.
- DNS server description:
- <id> : label of the server, should be unique
- <ip> : IP address of the server
- <port> : port where the DNS service actually runs
- Adds all nameservers found in /etc/resolv.conf to this resolvers nameservers
- list. Ordered as if each nameserver in /etc/resolv.conf was individually
- placed in the resolvers section in place of this directive.
- Defines <period> during which the last name resolution should be kept based
- on last resolution <status>
- <status> : last name resolution status. Acceptable values are "nx",
- "other", "refused", "timeout
", "valid", "obsolete".
- This keyword is available in sections :
- Mailers
- Alphabetically sorted keywords reference
- The resolvers section
- <period> : interval between two successive name resolution when the last
- answer was in <status>. It follows the HAProxy time format.
- <period> is in milliseconds by default.
- Default value is 10s for "valid", 0s for "obsolete" and 30s for others.
- Defines the number <nb> of queries to send to resolve a server name before
- giving up.
- Default value: 3
- A retry occurs on name server timeout or when the full sequence of DNS query
- type failover is over and we need to start up from the default ANY query
- type.
- Defines timeouts related to name resolution
- <event> : the event on which the <time> timeout period applies to.
- events available are:
- - resolve : default time to trigger name resolutions when no
- other time applied.
- Default value: 1s
- - retry : time between two DNS queries, when no valid response
- have been received.
- Default value: 1s
- <time> : time related to the event. It follows the HAProxy time format.
- <time> is expressed in milliseconds.
Example:
resolvers mydns
nameserver dns1 10.0.0.1:53
nameserver dns2 10.0.0.2:53
parse-resolv-conf
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold other 30s
hold refused 30s
hold nx 30s
hold timeout 30s
hold valid 10s
hold obsolete 30s