如何在CentOS 7中将HAProxy设置为Nginx的负载平衡器
HAProxy由Willy Tarreau用C语言编写,也称为高可用性代理,它是一种快速,轻量级的HTTP负载平衡器和代理服务器。
它具有较低的CPU使用率,并且内存占用较小。
流行的(例如StackOverflow,Twitter,Github和Tumblr)使用负载平衡器仅举几例。
在本教程中,我们将向您展示如何在CentOS 7上将HAProxy设置为Nginx Web服务器的负载平衡器。
该负载平衡器将位于2个Nginx Web服务器的前面,并公平地向服务器分发HTTP请求。
HAProxy平衡算法
这是负载均衡器在分配工作负载时用于选择Web服务器的算法。
1. Roundrobin
这是最简单的算法。
基本上,每个新连接都将由下一个Web服务器处理。
例如,如果您有4个后端服务器,则每个服务器将连续处理请求。
当到达列表中的最后一个Web服务器时,负载平衡器将从第一个Web服务器的顶部重新开始。
2. Lastconn
其中连接数量最少的服务器将处理一个新请求。
当请求的负载和时间相差很大时,这会派上用场。
入门
首先,请执行飞行前检查列表,并确保您具备以下条件。
- CentOS 7服务器
| Hostname | Server IP address |
|---|---|
| load-balancer | 173.82.168.96 |
| web-server-1 | 173.82.2.236 |
| web-server-2 | 173.82.94.57 |
- SSH访问所有服务器
步骤1:在负载均衡器中配置/etc/hosts文件
使用SSH登录到负载均衡器,并添加Nginx Web服务器的IP地址和主机名,如图所示。
vim /etc/hosts 173.82.2.236 web-server-1 173.82.94.57 web-server-2
保存并退出vim文本编辑器。
接下来,登录到每个Web服务器(web-server-1和web-server-2)并编辑/etc/hosts文件以指向负载均衡器。
173.82.168.96 load-balancer
保存并退出文本编辑器。
步骤2:在负载平衡器服务器上安装和配置HAProxy
HAProxy存储库可在CentOS存储库上使用。
要安装和设置HAProxy,首先,登录并更新系统存储库。
yum update -y
接下来,使用以下命令安装HAProxy:
yum -y install haproxy
示例输出
安装成功并完成后,请转到haproxy目录。
cd /etc/haproxy
通过将文件" haproxy.cfg"重命名为" haproxy.cfg.bak"来备份文件
mv haproxy.cfg haproxy.cfg.bak
接下来,创建一个新的HAproxy配置文件。
vim haproxy.cfg
#-------------------------------------------------------------------- # Global settings #-------------------------------------------------------------------- global log 127.0.0.1 local2 #Log configuration chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy #Haproxy running under user and group "haproxy" group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #-------------------------------------------------------------------- # common defaults that all the 'listen' and 'backend' sections will # use if not designated in their block #-------------------------------------------------------------------- defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 #-------------------------------------------------------------------- #HAProxy Monitoring Config #-------------------------------------------------------------------- listen haproxy3-monitoring *:8080 #Haproxy Monitoring run on port 8080 mode http option forwardfor option httpclose stats enable stats show-legends stats refresh 5s stats uri /stats #URL for HAProxy monitoring stats realm Haproxy\ Statistics stats auth Password123: Password123 #User and Password for login to the monitoring dashboard stats admin if TRUE default_backend app-main #This is optionally for monitoring backend #-------------------------------------------------------------------- # FrontEnd Configuration #-------------------------------------------------------------------- frontend main bind *:80 option http-server-close option forwardfor default_backend app-main #-------------------------------------------------------------------- # BackEnd round robin as balance algorithm #-------------------------------------------------------------------- backend app-main balance roundrobin #Balance algorithm option httpchk HEAD/HTTP/1.1\r\nHost:\ localhost #Check the server application is up and healty - 200 status code server web-server-1 173.82.2.236:80 check #Nginx1 server web-server-2 173.82.94.57:80 check #Nginx2
记下最后两行中指定的Web服务器,如输出所示。
保存并退出文本编辑器。
接下来,我们将配置rsyslog守护程序以记录HAProxy统计信息。
编辑rsyslog.conf文件以启用rsyslog使用的UDP端口514。
vim /etc/rsyslog.conf
要允许通过端口154的UDP连接,请取消注释以下几行。
$ModLoad imudp $UDPServerRun 514
保存并退出文本编辑器。
接下来,为syslog创建一个新的HAProxy配置文件。
vim /etc/rsyslog.d/haproxy.conf
粘贴以下配置
local2.=info /var/log/haproxy-access.log #For Access Log local2.notice /var/log/haproxy-info.log #For Service Info - Backend, loadbalancer
保存并退出文本编辑器。
继续并重新启动rsyslog。
systemctl restart rsyslog
接下来,启动并启用Haproxy以在启动时启动。
systemctl start haproxy systemctl enable haproxy
要确认HaProxy已启动并正在运行,请执行以下操作:
systemctl status haproxy
下一步,我们将Nginx安装到我们的Web服务器上。
步骤3:安装和配置Nginx
剩下的唯一关键步骤是在每台Web服务器上安装Nginx。
但首先,如图所示安装EPEL存储库
yum install epel-release
接下来,安装Nginx
yum install nginx -y
在两个服务器上都安装了Nginx的情况下,我们将修改每个Nginx Web服务器中的index.html文件,以便在使用HAproxy负载均衡器进行仿真时在每个服务器之间建立区别。
移至html目录,如下所示:
cd /usr/share/nginx/html/
备份index.html文件
mv index.html index.html.bak
接下来,创建一个新的index.html文件并粘贴一些示例内容。
对于Web服务器1
echo "web-server-1. Hey ! This is your first web server" > index.html
对于Web Server 2
echo "web-server-2. Hey ! This is your second web server" > index.html
接下来,在两个Web服务器中启动Nginx并确认服务是否正在运行
systemctl start nginx systemctl status nginx
测试负载平衡
要验证一切正常,请重复运行以下命令。
curl 173.82.168.96
您的输出应与此类似。
正如您可以敏锐地观察到的那样,每当随后在curl命令上运行该命令时,输出将在第一Web服务器内容和第二Web服务器内容" Perfect!"之间交替显示。
现在,让我们尝试使用网络浏览器进行测试。
https://负载平衡器IP地址
这将在两个Web服务器(在本例中为Web-server-2)上显示内容。
现在,尝试刷新一次或者两次,输出将指向另一个Web服务器,在本例中为Web-server-1。
这证实了我们的负载均衡器能够在我们的Web服务器之间公平地分配HTTP请求。
要收集有关负载均衡器浏览器的更多统计信息,请访问以下URL
https://load-balancer-IP:8080/stats
使用Password123作为用户名,并使用我们在haproxy.cfg配置文件中定义的密码。

