分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

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

发布时间:2023-09-06 02:02责任编辑:彭小芳关键词:暂无标签
Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx,相比较而言,LVS性能最好,但是搭建相对复杂,Nginx的upstream模块支持群集功能,但是对群集节点的健康检查功能不强,性能没有Hapr oxy好。Haproxy的官方网站是http://haproxy.1wt.eu/。
本案例介绍使用Haproxy及Nginx搭建一套web群集。

一、案例环境

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

主机系统IP地址主要软件
Haproxy服务器CentOS7.4 X86_64172.16.10.30haproxy-1.5.19.tar.gz
Nginx服务器1CentOS7.4 X86_64172.16.10.10nginx-1.12.0.tar.gz
Nginx服务器2CentOS7.4 X86_64172.16.10.20nginx-1.12.0.tar.gz
客户端Windows7172.16.10.8IE浏览器

二.编译安装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 roundrobin ???????server 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/2018: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/2018: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负载均衡群集

原文地址:http://blog.51cto.com/11134648/2134644

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved