失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Haproxy+Nginx实现web负载均衡群集

Haproxy+Nginx实现web负载均衡群集

时间:2022-08-31 22:35:36

相关推荐

Haproxy+Nginx实现web负载均衡群集

Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx,相比较而言,LVS性能最好,但是搭建相对复杂,Nginx的upstream模块支持群集功能,但是对群集节点的健康检查功能不强,性能没有Hapr oxy好。Haproxy的官方网站是http://haproxy.1wt.eu/。

本案例介绍使用Haproxy及Nginx搭建一套web群集。

一、案例环境

使用三台服务器模拟搭建一套web群集,具体的拓补图如下:

二.编译安装Nginx服务器

1.搭建Nginx1

使用nginx-1.12.0.tar.gz安装包进行编译安装

[root@localhost ~]# yum install gcc gcc-c++ pcre-devel zlib-devel make-y[root@localhost ~]# useradd -M -s /sbin/nologin nginx#创建运行用户[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/[root@localhost ~]# cd /opt/nginx-1.12.0/[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx #配置相关参数[root@localhost nginx-1.12.0]# make && make install #安装

安装完后的默认信息如下

默认安装目录:/usr/local/nginx 默认日志:/usr/local/nginx/logs默认监听端口:80默认web目录:/usr/local/nginx

接下来设置测试页面并启动Nginx服务。

[root@localhost ~]# cd /usr/local/nginx/html[root@localhost html]# echo "Server 172.16.10.10" > test.html[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost ~]# nginx #启动[root@localhost ~]# systemctl stop firewalld.service #关闭防火墙[root@localhost ~]# setenforce 0

在客户端访问http://172.16.10.10/test.html,网页正常显示。

2.搭建Nginx2

编译安装的步骤与Nginx1相同,不同之处在于建立测试页面。

[root@localhost ~]# cd /usr/local/nginx/html[root@localhost html]# echo "Server 172.16.10.20" > test.html

三、编译安装Haproxy

使用haproxy-1.5.19.tar.gz安装包进行编译安装

[root@localhost ~]# yum install gcc gcc-c++ pcre-devel bzip2-devel make -y #用yum安装一系列d的环境支持[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/ #解压haproxy软件包至/opt目录下[root@localhost ~]# cd /opt/haproxy-1.5.19/[root@localhost haproxy-1.5.19]# make TARGET=linux26 #64位系统[root@localhost haproxy-1.5.19]# make install #安装

四.创建Haproxy的配置文件

Haproxy默认不创建配置文件,需要复制软件包的模板配置文件,并进行编辑

[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy #创建配置文件目录[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ #将haproxy.cfg文件复制到配置文件目录下 [root@localhost haproxy-1.5.19]# cd /etc/haproxy/[root@localhost haproxy]# vim haproxy.cfg

#删除以下语句chroot /usr/share/haproxy #禁锢到haproxy的根目录下redispatch #强制将请求发送到已经down掉的机器#添加listen webcluster 0.0.0.0:80 #定义一个webcluster的应用option httpchk GET /test.html#访问服务器的test.html文件balance roundrobinserver inst1 172.16.10.10:80 check inter 2000 fall 3 #定义nginx节点服务器server inst2 172.16.10.20:80 check inter 2000 fall 3

五.创建Haproxy自启动脚本

[root@localhost haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy [root@localhost haproxy]# chmod +x haproxy[root@localhost haproxy]# chkconfig --add /etc/init.d/haproxy #添加系统服务[root@localhost haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy[root@localhost haproxy]# service haproxy start#启动Haproxy服务[root@localhost haproxy]# systemctl stop firewalld.service #关闭防火墙

六、测试web群集

通过上面的步骤,已经搭建完成Haproxy的web群集,接下来验证群集是否正常工作。一个群集一般需要具备两个特性,第一个是高性能,第二个是高可用。

1.测试高性能

在客户端使用浏览器打开http://172.16.10.30/test.html, 浏览器显示信息如图所示:

刷新一下,显示信息如图所示:

可以看到群集的负载均衡调度生效,满足群集的高性能要求。

2.测试高可用

将172.16.10.10的Nginx服务器停用,在客户端使用浏览器打开http://172.16.10.30/test。html ,浏览器显示信息如图所示:

从中可以看出,当一台节点出现故障时,不会影响群集的使用,这样满足了群集的高可用性。

七、Haproxy的日志

Haproxy的日志默认输出系统的syslog中,查看不方便,在生产环境中可以单独定义出来。

1.修改haproxy配置文件中(/etc/haproxy/haproxy.cfg)关于日志配置的选项,加入下面的配置:

log /dev/log local0 infolog /dev/log local0 notice

将这两行配置放到global配置项目中,主要是将info及notice日志分别记录到不同的日志文件中。

2.然后重启Haproxy的服务

[root@localhost ~]# service haproxy restart#重新启动Haproxy服务

3.修改rsyslog配置

为了便于管理,将Haproxy的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件。

[root@localhost ~]# touch /etc/rsyslog.d/haproxy.conf[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf

加入下面的内容:

if ($programname == 'haproxy' and $syslogseverity-text == 'info')then -/var/log/haproxy/haproxy-info.log #info日志记录到/var/log/haproxy/haproxy-info.log下&~#表示日志写入到日志文件后,rsyslog停止处理这个信息if ($programname == 'haproxy' and $syslogseverity-text == 'notice')then -/var/log/haproxy/haproxy-notice.log #notice日志记录到/var/log/haproxy/haproxy-notice.log下&~

4.保存配置文件并重启rsyslog服务

[root@localhost ~]# systemctl restart rsyslog.service

5.测试日志信息

在客户端访问http://172.16.10.30/test.html后,可以使用 tail -f /var/log/haproxy/haproxy-info.log 即时查看haproxy的访问请求日志。

[root@localhost ~]# tail -f /var/log/haproxy/haproxy-info.log Jun 30 21:20:48 localhost haproxy[4202]: 172.16.10.8:50352 [30/Jun/:21:20:48.466] webcluster webcluster/inst1 1/0/0/1/2 200 279 - - ---- 1/1/0/1/0 0/0 "GET /test.html HTTP/1.1"Jun 30 21:21:58 localhost haproxy[4202]: 172.16.10.8:50353 [30/Jun/:21:21:58.114] webcluster webcluster/inst2 0/0/4/1/5 200 249 - - ---- 1/1/0/1/0 0/0 "GET /test.html HTTP/1.1"

如果觉得《Haproxy+Nginx实现web负载均衡群集》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。