Load-Balancing

HAProxy 配置文件中的主機名

  • October 12, 2020

我的 haproxy.cfg 文件有兩個使用主機名的後端伺服器:

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

這些主機名是 Amazon OpsWorks 的一部分,並在實例啟動或關閉時自動注入 /etc/hosts。如果我在其中一個實例關閉時嘗試重新啟動 HAProxy,我會收到錯誤消息:

[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.

有沒有辦法告訴 HAProxy 檢查主機名是否有效?如果有效,則使用它,如果無效,則忽略它。

在 haproxy >= 1.7 中,您應該能夠使用該init-addr選項,指定 none 以防止在啟動時解析 DNS。

文件

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

如果使用 FQDN,請指明在啟動時應按什麼順序解析伺服器地址。嘗試通過依次應用逗號分隔列表中提到的每個方法來解析地址。使用第一種成功的方法。如果在沒有找到工作方法的情況下到達列表末尾,則會引發錯誤。方法“last”建議選擇出現在狀態文件中的地址(參見“server-state-file”)。方法“libc”使用 libc 的內部解析器(gethostbyname() 或 getaddrinfo(),具體取決於作業系統和建構選項)。方法“none”專門表示伺服器應該在沒有任何有效 IP 地址的情況下啟動。在啟動時忽略一些 DNS 問題會很有用,等待情況稍後解決。最後,可以提供 IP 地址(IPv4 或 IPv6)。

所以你的配置行可能是:

server s1 myhostname init-addr none

引用自:https://serverfault.com/questions/564233