我们运行 haproxy 作为负载均衡器并为我们的 API 服务提供备份功能。
我们的 API 是一个使用 Jersey 框架在 Apache Tomcat 中运行的 Java 应用程序。前段时间,我们在 API 方面遇到了严重的性能问题,我们只是不知道发生了什么。深入挖掘后,我们发现选项 httpchk 导致了这个问题:
option httpchk GET /resource/healthcheck
http-check expect rstatus (2|3)[0-9][0-9]
server server1_8585 x.x.x.x:8585 check inter 1s fastinter 2s fall 2 rise 1 pool-purge-delay 30s
haproxy 发送的数据包不包含 Host 标头,因此 Tomcat 会尝试解析主机,从而导致严重的性能问题。因此,我们将后端配置调整为:
后端配置如下:
backend api_example_com
balance roundrobin
option httpchk GET /resource/healthcheck HTTP/1.1\r\nHost:\ server1.example.com
http-check expect rstatus (2|3)[0-9][0-9]
server server1_8585 x.x.x.x:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s
server server2_8585 y.y.y.y:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s backup
server server3_8585 z.z.z.z:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s backup
但是现在我们面临的问题是,如果进行健康检查,备份 API 也会得到错误的主机名。
您知道如何为每个服务器设置单独的健康检查吗?
答案1
您可以尝试为每个备份服务器创建一个代理,然后使用追踪功能。
但正如 Gerard 所写,最好在每个服务器上都使用相同的主机名。对于 tomcat 来说,这是主机名别名
HAProxy 轨道建议(未经测试)
backend track-server2_8585
option httpchk GET /resource/healthcheck HTTP/1.1\r\nHost:\ server2.example.com
http-check expect rstatus (2|3)[0-9][0-9]
server server2_8585 y.y.y.y:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s
backend track-server3_8585
option httpchk GET /resource/healthcheck HTTP/1.1\r\nHost:\ server3.example.com
http-check expect rstatus (2|3)[0-9][0-9]
server server3_8585 y.y.y.y:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s
backend api_example_com
balance roundrobin
option httpchk GET /resource/healthcheck HTTP/1.1\r\nHost:\ server1.example.com
http-check expect rstatus (2|3)[0-9][0-9]
server server1_8585 x.x.x.x:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s
server server2_8585 y.y.y.y:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s backup track track-server2_8585/server2_8585
server server3_8585 z.z.z.z:8585 check inter 10s fastinter 5s fall 2 rise 1 pool-purge-delay 30s backup track track-server3_8585/server3_8585