HAProxy is a very fast and reliable solution for high availability, load balancing, It supports TCP and HTTP-based applications. Nowadays most of the websites need 99.999% uptime for their site, which is not possible with single server setup. Then we need some high availability environment that can easily manage with single server failure.
本文将帮助您在 CentOS、RHEL 服务器上安装 HAProxy 并配置四层负载均衡 (传输层)。它将根据 IP 地址和端口号平衡负载并将请求传输到不同的 2 个服务器。
第 1 步 – 安装 HAProxy
HAProxy 软件包可在 CentOS、Redhat 系统的默认 yum 存储库下找到。使用以下 yum 包管理器命令在您的系统上安装 HAProxy。
sudo yum install haproxy
步骤 2 – 配置 HAProxy
更新您的 HAProxy 配置文件/etc/haproxy/haproxy.cfg根据您的要求,您还可以使用下面给定的配置文件作为设置示例并对其进行修改。
vim /etc/haproxy/haproxy.cfg
[ Update red mark values as per your network setup ]
global
log 127.0.0.1 local0
log 127.0.0.1 local1 debug
maxconn 45000 # Total Max Connections.
daemon
nbproc 1 # Number of processing cores.
defaults
timeout server 86400000
timeout connect 86400000
timeout client 86400000
timeout queue 1000s
# [HTTP Site Configuration]
listen http_web 192.168.10.10:80
mode http
balance roundrobin # Load Balancing algorithm
option httpchk
option forwardfor
server server1 192.168.10.100:80 weight 1 maxconn 512 check
server server2 192.168.10.101:80 weight 1 maxconn 512 check
# [HTTPS Site Configuration]
listen https_web 192.168.10.10:443
mode tcp
balance source# Load Balancing algorithm
reqadd X-Forwarded-Proto: http
server server1 192.168.10.100:443 weight 1 maxconn 512 check
server server2 192.168.10.101:443 weight 1 maxconn 512 check
根据您的网络设置更改配置文件中的 ip。在HTTP 站点配置如果在端口 80 上的 IP 192.168.10.10 上有任何请求,则该请求将被重定向到 192.168.10.100 或 192.168.10.101 服务器的端口 80。同样在HTTPS 站点配置如果在端口 443 上的 IP 192.168.10.10 上有任何请求,则该请求将被重定向到 192.168.10.100 或 192.168.10.101 服务器的端口 443。
您还需要根据您的系统配置对配置文件进行一些更改。
systemctl start haproxy
systemctl enable haproxy
你已经做到了。
恭喜!您已成功配置 HAProxy 负载均衡器。阅读下一篇文章启用 haproxy 统计信息 and 在 HAProxy 中设置 ACL.
有关更多配置详细信息,请检查以下网址。http://haproxy.1wt.eu/download/1.4/doc/configuration.txt