Hostnames in HAProxy configuration file

haproxyhostnamehostsload balancing

My haproxy.cfg file has two backend servers using hostnames:

server ops-ca-revealv2e-prod-1 ops-ca-revealv2e-prod-1:443 cookie ops-ca-revealv2e-prod-1 ssl weight 1 maxconn 512 check

server ops-ca-revealv2e-prod-2 ops-ca-revealv2e-prod-2:443 cookie ops-ca-revealv2e-prod-2 ssl weight 1 maxconn 512 check

These hostnames are part of Amazon OpsWorks and are injected into /etc/hosts automatically whenever an instance comes up or down. If I attempt to restart HAProxy when one of the instances is down, I receive the error:

[ALERT] 362/225440 (27202) : parsing [/opt/haproxy-ssl/haproxy.cfg:42] : 'server ops-ca-revealv2e-prod-2' : invalid address: 'ops-ca-revealv2e-prod-2' in 'ops-ca-revealv2e-prod-2:443'
[ALERT] 362/225440 (27202) : Error(s) found in configuration file : /opt/haproxy-ssl/haproxy.cfg
[ALERT] 362/225440 (27202) : Fatal errors found in configuration.

Is there a way to tell HAProxy to check if a hostname is valid? If it is valid, use it, if not, ignore it.

Best Answer

In haproxy >= 1.7, you should be able to use the init-addr option, specifying none to prevent DNS resolution at startup.

From the docs:

init-addr {last | libc | none | <ip>},[...]*

Indicate in what order the server's address should be resolved upon startup if it uses an FQDN. Attempts are made to resolve the address by applying in turn each of the methods mentionned in the comma-delimited list. The first method which succeeds is used. If the end of the list is reached without finding a working method, an error is thrown. Method "last" suggests to pick the address which appears in the state file (see "server-state-file"). Method "libc" uses the libc's internal resolver (gethostbyname() or getaddrinfo() depending on the operating system and build options). Method "none" specifically indicates that the server should start without any valid IP address in a down state. It can be useful to ignore some DNS issues upon startup, waiting for the situation to get fixed later. Finally, an IP address (IPv4 or IPv6) may be provided.

So your config line could be:

server s1 myhostname init-addr none

Related Topic