Haproxy搭建Web群集
Haproxy
常见的Web集群调度器
- 目前常见的Web集群调度器分为软件和硬件
- 软件通常使用开源的LVS,Haproxy,Nginx
- 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼,绿盟等
Haproxy应用分析
-
LVS在企业应用中抗负载能力很强,但存在不足
- LVS不支持正则表达式,不能实现动静分离
- 对于大型网站,LVS的实施配置复杂,维护成本相对较高
-
Haproxy是一款可提供高可用性,负载均衡,及基于TCP和HTTP应用的代理的软件
- 适用于负载大的Web站点
- 运行在硬件上可以支持数以万计的并发连接的连接请求
Haproxy调度算法原理
Haproxy支持多种调度算法,最常用的有三种
-
RR(Round Robin)
- RR算法是最简单最常用的一种算法,即轮询调度
-
举例:
- 有三个节点A,B,C
- 第一个用户访问会被指派到节点A
- 第二个用户访问会被指派到节点B
- 第三个用户访问会被指派到节点C
- 第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果
-
LC(Least Coonections)
- 最小连接数算法,根据后端的节点连接数大小动态分配前端请求
-
举例:
- 有三个节点A,B,C,各节点的连接数分别为A:4,B:5,C:6
- 第一个用户连接请求,会被指派到A上,连接数变为A:5,B:5,C:6
- 第二个用户请求会继续分配到A上(默认轮询),连接数变为A:6,B:5,C:6;再有新的的 请求会分配给B,每次将新的请求指派给连接数最小的客户端
- 由于实际情况下A,B,C的连接数会动态释放,很难会出现一样连接数的情况
- 此算法相比较rr算法有很大改进,是目前用到比较多的一种算法
-
SH(Source Hashing)
- 基于来源访问调度算法,用于一些有Session会话记录的服务器端的场景,可以基于来源的IP,Cookie等做集群调度
-
举例:
- 有三个节点A,B,C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
- 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
- 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用
Haproxy搭建群集
Haproxy安装与启动
- 在负载均衡器上安装Haproxy
- 安装步骤
- 安装基础软件包
- 编译安装haproxy
- 要注意操作系统版本,是32位系统还是64位
- 建立Haproxy的配置文件
- 创建配置文件目录/etc/haproxy
- 将源码包提供的配置文件样例haproxy.cfg复制到配置文件目录中
Haproxy配置文件详解
- defaults配置项配置默认参数,一般会被应用组件继承
- 如果应用组件中没有特别声明,将按默认配置参数设置
- log global:定义日志为global配置中的日志定义
- mode http:模式为http
- option httplog:采用http日志格式记录日志
- retries 3:检查节点服务器失败连续达到三次则认为节点不可用
- maxconn 2000:最大连接数
- contimeout 5000:连接超时时间(5秒)
- slitimeout 50000:客户端超时时间(50秒)
- srvtimeout 50000:服务器超时时间 - Listen配置项目一般为配置应用模块参数
- listen appli4-backup 0.0.0.0:10004:定义一个appli4-backup的应用
- option httpchk /index.html:检查服务器的index.html文件
- option persist:强制将请求发送到已经down掉的服务器
- balance roundrobin:负载均衡调度算法使用轮询算法
- server inst1 192.168.100.100:80 check inter 2000 fall 3:定义在线节点
- server inst2 192.168.100.100:80 check inter 2000 fall 3 backup: 定义备份节点
Haproxy参数优化
- 随着企业网站负载增加,haproxy参数优化相当重要
- maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10240
- daemon:守护进程模式,Haproxy可以使用非守护进程模式启动,建议使用守护进程模式启动
- nbproc:负载均衡的并发进程数,建议与当前服务器CPU核数相等或为其2倍
- retries:重试次数,主要用于对集群节点的检查,如果节点多,且并发量大,设置为2次或3次
- option http-server-close:主动关闭http请求选项,建议在生产环境中使用此选项
- timeout http-keep-alive:长连接超时时间,设置长连接超时时间,可以设置为10s
- timeout http-request:http请求超时时间,建议将此时间设置为5-10s,增加http连接释放速度
- timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了
配置部署
在nfs上配置
查看软件安装包
[root@nfs ~]# rpm -qa | grep nfs
[root@nfs ~]# rpm -qa | grep rpcbind
创建网页web1 web2
编辑配置文件
[root@nfs ~]# vi /etc/exports
/web1 192.168.1.50/32 (ro)
/web2 192.168.1.15/32 (ro)
服务启动
在web1
先添加nginx软件包
[root@web1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel #安装依赖包
[root@web1 ~]# useradd -M -s /sbin/nologin nginx # 创建不可登录,不带宿主的用户
[root@web1 ~]# tar zxvf nginx-1.12.7.tar.gz # 解压缩nginx软件
[root@web1 ~]# cd nginx-1.12.7/
[root@web1 nginx-1.12.7]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx #设置配置,安装路径,属主,属组
[root@web1 nginx-1.12.7]# make && make install #编译安装
[root@web1 nginx-1.12.7]# cd
[root@web1 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ # 创建软链接
[root@web1 ~]# cd /usr/local/nginx/html/
[root@web1 html]# ls -lh
[root@web1 html]# cd
[root@web1 ~]# mount 192.168.1.16:/web1 /usr/local/nginx/html/ #挂载
[root@web1 ~]# df -Th
[root@web1 ~]# nginx #服务启动
[root@web1 ~]# curl http://localhost 查看网页
在web2上
先添加nginx软件包
[root@web2 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel #安装依赖包
[root@web2 ~]# useradd -M -s /sbin/nologin nginx # 创建不可登录,不带宿主的用户
[root@web2 ~]# tar zxvf nginx-1.12.7.tar.gz # 解压缩nginx软件
[root@web2 ~]# cd nginx-1.12.7/
[root@web2 nginx-1.12.7]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx #设置配置,安装路径,属主,属组
[root@web2 nginx-1.12.7]# make && make install #编译安装
[root@web2 nginx-1.12.7]# cd
[root@web2 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ # 创建软链接
[root@web2 ~]# cd /usr/local/nginx/html/
[root@web2 html]# ls -lh
[root@web2 html]# cd
[root@web2 ~]# mount 192.168.1.16:/web2 /usr/local/nginx/html/ #挂载
[root@web2 ~]# df -Th
[root@web2 ~]# nginx #服务启动
[root@web2 ~]# curl http://localhost 查看网页
在haproxy上
先添加haproxy软件包
[root@haproxy ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel #安装依赖包
[root@haproxy ~]# tar zvxf haproxy-1.4.24.tar.gz # 解压缩
[root@haproxy ~]# mkdir /etc/haproxy #创建目录
[root@haproxy ~]# cd haproxy-1.4.24/
[root@haproxy haproxy-1.4.24]# make TARGET=linux26 # 内核版本26 #编译安装
[root@haproxy haproxy-1.4.24]# make install
[root@haproxy haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/ #复制文件
[root@haproxy haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg # 编辑配置文件
注释掉
#chroot /usr/share/haproxy # 锁定宿主目录
#redispatch # 如果节点失效,仍把请求指派给它
删除所有listen,添加修改以下内容
maxconn 10240
listen webcluster 0.0.0.0:80 #监听所有地址,端口
option httpchk GET /index.html #检查网页内容
balance roundrobin #负载均衡调度算法(rr)轮询
server web1 192.168.1.50:80 check inter 2000 fall 3 #检查节点间隔时间2秒,次数3次
server web2 192.168.1.15:80 check inter 2000 fall 3
[root@haproxy haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.4.24]# vi /etc/init.d/haproxy
修改
# chkconfig: 35 85 15 添加运行级别
[root@haproxy haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy #添加权限
[root@haproxy haproxy-1.4.24]# chkconfig --add haproxy #管理haproxy
[root@haproxy haproxy-1.4.24]# chkconfig --list
[root@haproxy haproxy-1.4.24]# cd
[root@haproxy ~]# ln -s/usr/local/sbin/haproxy /usr/sbin/
[root@haproxy ~]# systemctl start haproxy
访问测试
日志定义
编辑日志文件
[root@haproxy ~]# vi /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy #重启服务
修改添加
log /dev/log local0 info
log /dev/log local0 notice
配置文件
[root@haproxy ~]# vi /etc/rsyslog.d/haproxy.conf
添加:
if ($programname == 'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
[root@haproxy ~]# systemctl restart rsyslog.service
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# cd /var/log/haproxy/
[root@haproxy haproxy]# ll
访问验证
查看日志
[root@haproxy haproxy]# cat haproxy-info.log #记录调度信息
[root@haproxy haproxy]# cat haproxy-notice.log # 记录群集的启动情况