在本页面上
- 前提条件
- 安装 HAProxy
- 配置 HAProxy
- 配置防火墙
- 配置后端服务器
- 验证 HAProxy
- 结论
当你在互联网上部署一个应用程序时,该应用程序的可用性、可扩展性和性能对于获得更好的用户体验非常重要。有许多技术会引入冗余,例如负载均衡、集群和代理。
HAProxy 是一款免费、开源且可靠的负载均衡器,专为高流量网站设计。它通过将传入的流量分发到多个后端服务器来实现负载均衡。
在本文中,我们向你展示如何在 Rocky Linux 8 上安装 HAProxy。
为了本教程的目的,我们将使用以下设置:
服务器 IP 地址
HAProxy 172.16.10.10
Backend1 172.16.10.11
Backend2 172.16.10.12
前提条件
- 一台运行 RockyLinux 8 的服务器。
- 服务器上已配置 root 密码。
安装 HAProxy
默认情况下,HAProxy 软件包在 Rocky Linux 8 的默认仓库中可用。你可以使用以下命令安装它:
dnf install haproxy -y
HAProxy 安装完成后,启动 HAProxy 服务并使其在系统重启时启动:
systemctl start haproxy
systemctl enable haproxy
配置 HAProxy
接下来,你需要编辑 /etc/haproxy/haproxy.cfg 文件并修改 frontend 和 backend 两个部分。在 Frontend 部分,你需要定义 HAProxy 的 IP、其端口、stats URI 以及 backend 名称。在 Backend 部分,你需要定义负载均衡算法、后端服务器的名称、IP 和端口。
nano /etc/haproxy/haproxy.cfg
修改以下部分:
frontend http_balancer
bind 172.16.10.10:80
option http-server-close
option forwardfor
stats uri /haproxy?stats
default_backend Apache_webservers
backend apache_webservers
mode http
balance roundrobin
option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost
server apache1 172.16.10.11:80 check
server apache2 172.16.10.12:80 check
完成后保存并关闭文件,然后你需要编辑 /etc/rsyslog.conf 文件。
nano /etc/rsyslog.conf
取消注释以下行:
module(load="imudp") input(type="imudp" port="514")
保存并关闭文件,然后使用以下命令为 rsyslog 创建 haproxy.conf 文件:
nano /etc/rsyslog.d/haproxy.conf
添加以下行:
local2.=info /var/log/haproxy-access.log local2.notice /var/log/haproxy-info.log
接下来,启动 rsyslog 服务并使其在系统重启时启动:
systemctl start rsyslog
systemctl enable rsyslog
最后,重启 HAProxy 服务以应用更改:
systemctl restart haproxy
你还可以使用以下命令验证 HAProxy 服务的状态:
systemctl status haproxy
你应该会看到以下输出:
? haproxy.service - HAProxy Load Balancer
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2021-08-03 03:10:37 EDT; 1min 16s ago
Process: 44032 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 44036 (haproxy)
Tasks: 2 (limit: 12524)
Memory: 3.1M
CGroup: /system.slice/haproxy.service
??44036 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
??44111 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
Aug 03 03:10:36 RockyLinux8 systemd[1]: Starting HAProxy Load Balancer...
Aug 03 03:10:37 RockyLinux8 systemd[1]: Started HAProxy Load Balancer.
配置防火墙
接下来,你需要在所有服务器上配置 firewalld,并允许端口 80 通过防火墙。
你可以运行以下命令来允许端口 80:
firewall-cmd --permanent --add-port=80/tcp
接下来,重新加载 firewalld 服务以应用更改。
firewall-cmd --reload
配置后端服务器
接下来,你需要在两台后端服务器上安装 Apache 服务器。运行以下命令来安装 Apache 服务器。
dnf install httpd -y
Apache 服务器安装完成后,启动 Apache 服务并使其在系统重启时启动:
systemctl start httpd
systemctl enable httpd
接下来,你需要修改两台后端服务器上的默认 index.html 文件。
在第一台后端服务器上,使用以下命令编辑 index.html 文件:
nano /usr/share/httpd/noindex/index.html
删除所有行并添加以下行:
Welcome to First Apache Web Server!
完成后保存并关闭文件。
在第二台后端服务器上,使用以下命令编辑 index.html 文件:
nano /usr/share/httpd/noindex/index.html
删除所有行并添加以下行:
Welcome to Second Apache Web Server!
完成后保存并关闭文件。
验证 HAProxy
此时,HAProxy 已配置为基于负载均衡算法将所有传入请求转发到后端服务器。现在,是时候检查 HAProxy 是否正常工作了。
打开你的网页浏览器,在 URL 中输入 HAProxy 的 IP http://172.16.10.10 。你应该会看到第一台 apache webserver 的页面:

接下来,再次刷新同一页面,你应该会看到第二台 apache webserver 的页面:

这确认了 HAProxy 正在按预期工作,并且它正在两台后端服务器之间分发流量。
结论
恭喜!你已在 Rocky Linux 8 上成功安装并配置了 HAProxy 负载均衡器。现在你可以在生产环境中实施 HAProxy,以实现高性能和可扩展性。