文章目录
一、常见的Web集群调度器
- 目前常见的Web集群调度器分为软件和硬件
- 软件通常使用开源的LVS、Haproxy、Nginx
- 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等。
二、Haproxy应用分析
1.LVS在企业应用中抗负载能力很强,但存在不足
- LVS不支持正则处理,不能实现动静分离
- 对于大型网站,LVS的实施配置复杂,维护成本相对较高
2.Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
- 适用于负载大的Web节点
- 运行在硬件上可支持数以万计的并发连接的连接请求
三、调度算法原理
1.Haproxy支持多种调度算法,最常用的有三种
1.1 RR(Round Robin)
RR算法是最简单最常用的一种算法,即轮询调度
1.理解举例
- 有三个节点A、B、C
- 第一个用户访问会被指派到节点A
- 第二个用户访问会被指派到节点B
- 第三个用户访问会被指派到节点C
1.2 LC(Least Connections)
最小连接数算法,根据后端的节点连接数大小动态分配前端请求
2.理解举例
- 有三个节点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算法有很大改进,是目前用到比较多的一种算法
1.3 SH(Source Hashing)
基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源IP、Cookie等做集群调度
3.理解举例
- 有三个节点A、B、C,第一个用户第一次访问被指派到A,第二个用户第一次访问被指派到了B
- 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡器不重启,第一个用户访问会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
- 此调度算法好处是实现会话保持,但某些IP访问量非常大会引起负载不均衡,部分节点访问量超大,影响业务使用
三、Nginx的安装与启动
1.在两台网站服务器上安装Nginx,并启动服务
- 使用源码编译的方式进行安装
- 关闭Firewalld防火墙
- 安装基础软件包
- 增加系统用户账号nginx
- 编译安装Nginx并启动
温馨提示:在两台Nginx上配置测试网站,注意测试网页得到内容应该不同,以便进行测试区分
2.在负载均衡器上安装Haproxy
2.1 安装步骤
- 安装基础软件包
- 编译安装haproxy
- 要注意操作系统版本,是32位还是64位
3.建立Haproxy的配置文件
- 创建配置文件目录/etc/haproxy
- 将源码包提供的配置文件样例haproxy.cfg复制到配置文件目录中
四、使用Haproxy搭建Web群集
拓扑图
五、Haproxy配置文件详解
1.defaults配置项配置默认参数,一般会被应用组件继承
log global:定义日志为global配置中的日志定义
mode http:模式为http
option httplog:采用http日志格式记录日志
retries 3:检查节点服务器失败连续达到三次则认为节点不可用
maxconn 2000:最大连接数
contimeout 5000:连接超时时间(5秒)
slitimeout 50000:客户端超时时间(50秒)
srvtimeout 50000:服务器超时时间
2.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.114.56:80 check inter 2000 fall 3:定义在线节点
server inst2 192.168.114.56: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请求超过时间,建议将此时间设置为5s~10s,增加http连接释放速度
timeout client:客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了
七、部署Web群集
1.步骤:
1.nfs创建共享存储,测试网页
2.节点服务器配置nginx,挂载共享存储
3.haproxy配置安装
4.参数优化
5.保存规则并测试
基础配置:
调度服务器一台:haproxy
haproxy ip:192.168.1.10
节点服务器两台:
nginx2 ip:192.168.1.11(web nginx1)
nginx2 ip:192.168.1.12(web nginx2)
NFS共享服务器:
nfs ip:192.168.1.13
客户端一台:用于测试验证
client ip:192.168.1.14
在nfs存储服务器上
1.配置共享目录,创建网页
[root@nfs ~]# mkdir /nginx1
[root@nfs ~]# mkdir /nginx2
[root@nfs ~]# echo "<h1>This is the nginx1</h1>" > /nginx1/index.html
[root@nfs ~]# echo "<h1>This is the nginx2</h1>" > /nginx2/index.html
2.安装检查nfs、rpcbind
[root@nfs ~]# yum -y install nfs-utils rpcbind
[root@nfs ~]# rpm -qa rpcbind
rpcbind-0.2.0-42.el7.x86_64
[root@nfs ~]# rpm -qa nfs-utils
nfs-utils-1.3.0-0.48.el7.x86_64
3.编辑配置文件
[root@nfs ~]# vi /etc/exports
添加:
/nginx1 192.168.1.11 (ro)
/nginx2 192.168.1.12 (ro)
4.启动服务
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl restart rpcbind
[root@nfs ~]# showmount -e #查看共享情况
Export list for nfs:
/nginx2 (everyone)
/nginx1 (everyone)
在nginx1、nginx2上
1.安装以依赖包
[root@nginx1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
2.创建不可登陆,不带宿主的用户
[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx
通过xshell进行文件传输 nginx-1.13.7软件包
3.解压安装包
[root@nginx1 ~]# tar zxvf nginx-1.13.7.tar.gz
4.配置
[root@nginx1 ~]# cd nginx-1.13.7/
[root@nginx1 nginx-1.13.7]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
5.编译安装
[root@nginx1 nginx-1.13.7]# make && make install
[root@nginx1 nginx-1.13.7]# cd
6.创建软链接
[root@nginx1 nginx-1.13.7]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx1 nginx-1.13.7]# cd /usr/local/nginx/html/
[root@nginx1 html]# ll
7.挂载
[root@nginx1 html]# cd
[root@nginx1 ~]# mount 192.168.1.13:/nginx1 /usr/local/nginx/html/
8.启动服务、测试网页
[root@nginx1 ~]# nginx
[root@nginx1 ~]# curl http://localhost
<h1>This is the nginx1</h1>
nginx2同上
[root@nginx2 nginx-1.13.7]# cd
[root@nginx2 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx2 ~]# mount 192.168.1.13:/nginx2 /usr/local/nginx/html/
[root@nginx2 ~]# df -Th
[root@nginx2 ~]# nginx
[root@nginx2 ~]# curl http://localhost
<h1>This is the nginx2</h1>
在haproxy上
1.安装依赖包
[root@haproxy ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
通过xshell文件传输haproxy-1.4.24软件包
2.解压软件
[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
3.复制文件,编辑配置文件
[root@haproxy haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg 编辑配置文件
[root@haproxy haproxy-1.4.24]# cd ..
注释这2条
#chroot /usr/share/haproxy 锁定宿主目录
#redispatch 如节点失效,依旧请求指派给它
删除所有listen下的内容,添加修改以下内容
maxconn 10240
listen webcluster 0.0.0.0:80 #监听所有地址,端口
option httpchk GET /index.html #检查网页内容
balance roundrobin #负载均衡调度算法(rr)轮询
server nginx1 192.168.1.11:80 check inter 2000 fall 3 #检查节点间隔时间2秒,次数3次
server nginx2 192.168.1.12:80 check inter 2000 fall 3
4.修改优先级
[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
5.添加权限,用chkconfig去管理haproxy
[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 ~]# systemctl enable haproxy #设置自启动
[root@haproxy ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/
[root@haproxy ~]# systemctl start haproxy
测试:
客户机测试访问调度器haproxy 192.168.1.10
刷新一下
2.日志定义
1.编辑日志文件、重启服务
[root@haproxy ~]# vi /etc/haproxy/haproxy.cfg
[root@haproxy ~]# systemctl restart haproxy
修改添加:
log /dev/log local0 info
log /dev/log local0 notice
2.编辑配置
[root@haproxy ]# vi /etc/rsyslog.d/haproxy.conf
[root@haproxy ]# systemctl restart rsyslog
[root@haproxy ]# systemctl restart haproxy
添加:
如果程序是haproxy,日志类型是info,就创建日志
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
&~
编辑完成后,会生成2个日志文件
[root@haproxy ~]# cd /var/log/haproxy/
[root@haproxy haproxy]# ll
测试:
客户机测试访问调度器haproxy 192.168.1.10
刷新一下
3.查看调度情况
[root@haproxy haproxy]# cat haproxy-info.log #记录调度信息
[root@haproxy haproxy]# cat haproxy-notice.log #记录群集的启动情况
3.统计页面设置
1.设置配置文件
[root@haproxy haproxy]# vi /etc/haproxy/haproxy.cfg
[root@haproxy haproxy]# systemctl restart haproxy
添加
listen admin_stats bind 0.0.0.0:1080
设置Frontend和Backend的组合体,监控组的名称,按需要自定义名称
mode http #http的7层模式
option httplog #采用http日志格式
#log 127.0.0.1 local0 err #错误日志记录
maxconn 10 #默认的最大连接数
stats refresh 30s #统计页面自动刷新时间
stats uri /stats # 统计页面url
stats realm XingCloud\ Haproxy #统计页面密码框上提示文本
stats auth apple:apple #设置监控页面的用户和密码:apple,可以设置多个用户名
stats auth admin:admin #置监控页面的用户和密码: admin
stats hide-version #隐藏统计页面上HAProxy的版本信息
stats admin if TRUE #设置手工启动/禁用,后端服务器(haproxy-1. 4.9以后版本)
测试:
http://192.168.1.10:1080/stats
用户名:stats
密码:fa