常见的Web集群调度器
- 目前常见的Web集群调度器分为软件和硬件
- 软件通常使用开源的LVS、Haproxy、Nginx
- 硬件一般使用较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等
Haproxy应用分析
- LVS在企业应用中抗负载能力很强,但存在不足
- LVS不支持正则处理,不能实现动静分离
- 对于大型网站,LVS的实施配置复杂、维护成本相对较高
- Haproxy是一款提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
- 适用于负载大的Web站点
- 运行在硬件上可支持数以万计的并发连接的连接请求
Haproxy常用调度算法
Haproxy支持多种调度算法,最常见的有三种
RR (Round Robin)轮询调度
RR算法是最简单最常用的一种算法,即轮询调度
理解举例
- 有三个节点
- 第一个用户访问会被指派到节点A
- 第二个用户访问会被指派到节点B
- 第三个用户访问会被指派到节点C
- 第四个用户访问继续指派节点A,轮询分配访问请求实现2负载均衡效果
LC(Least Connections)最小连接
最小连接数算法,根据后端的节点连接数大小动态分配前端请求
理解举例
- 有三个节点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访问量非常2大时会引起负载均衡,部分节点访问量超大,影响业务使用
使用Haproxy搭建群集
主机要求
- 用一台客户端,三台虚拟服务器模拟一套web集群
- 虚拟机安装CentOS 7.3的64位系统
主机 | 操作系统 | IP地址 |
Haproxy服务器 | Cent OS7.3x86_64 | 192.168.10.101 |
Nginx服务器1 | Cent OS7.3x86_64 | 192.168.10.102 |
Nginx服务器2 | Cent OS7.3x86_64 | 192.168.10.103 |
客户端 | Windows 10 | 192.168.10.104 |
案例:使用Haproxy搭建Web群集
实验操作
同步连接102、103主机,关闭防火墙,安装httpd软件包
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd #启动httpd服务
来到102主机创建一个index文件
[root@localhost ~]# echo "test" web01 >/var/www/html/index.html
来到103主机创建一个index文件
[root@localhost ~]# echo "test" web02 >/var/www/html/index.html
来到101主机测试俩主机是否可以访问的到
[root@localhost ~]# curl 192.168.10.102
test web01
[root@localhost ~]# curl 192.168.10.103
test web02
编译安装Haproxy
安装在101主机上,关闭防火墙,安装对应的软件包,解压该软件包,
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc*
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz #解压软件包
[root@localhost ~]# cd haproxy-1.5.19
[root@localhost haproxy-1.5.19]# make TARGET=linux26
[root@localhost haproxy-1.5.19]# make install
linux22 forLinux 2.2
linux24 forLinux 2.4 and above (default)
linux24e for Linux2.4 with support for a working epoll (> 0.21)
linux26 forLinux 2.6 and above
solaris forSolaris 8 or 10 (others untested)
freebsd forFreeBSD 5 to 8.0 (others untested)
openbsd forOpenBSD 3.1 to 4.6 (others untested)
cygwin forCygwin
generic forany other OS.
custom tomanually adjust every setting
Haproxy服务器配置
建立haproxy配置文件
[root@localhost etc]# mkdir haproxy
[root@localhost ~]# cd haproxy-1.5.19
[root@localhost haproxy-1.5.19]# cd examples/
[root@localhost examples]# cp haproxy.cfg /etc/haproxy/
创建服务脚本
[root@localhost examples]# cp haproxy.init /etc/init.d/haproxy
[root@localhost examples]# chmod +x /etc/init.d/haproxy
[root@localhost examples]# cd /usr/local/sbin/
[root@localhost ~]# chkconfig --add haproxy #将haproxy添加到服务列表,并在适当的时候自动启动该服务
[root@localhost ~]# chkconfig haproxy on #设置开机自启
创建一个软链接
在 /usr/sbin 目录下创建一个名为 haproxy 的符号链接,它指向 /usr/local/sbin/haproxy 这个可执行文件。这样做通常是为了使 haproxy 在系统上的路径更加统一和易于访问。
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
Haproxy配置介绍
创建一个目录
[root@localhost ~]# mkdir /usr/share/haproxy #创建目录
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
#删除配置文件25行以下的内容 d+G
#chroot /usr/share/haproxy
#注释掉也可也,创建目录也可以,俩种选一个
#redispatch
listen myweb 0.0.0.0:80 #添加监听项
option httpchk GET /index.html #告诉 HAProxy 使用 HTTP GET 请求来检查 /index.html 页面的可用性。如果服务器返回合适的响应(通常是状态码 2xx),则认为服务器是健康的。
balance roundrobin #轮询算法会依次将请求分发到每个后端服务器,从而平均分配负载。
server inst1 192.168.10.102:80 check inter 2000 fall 3 weight 1 #轮询算法会依次将请求分发到每个后端服务器,从而平均分配负载。
server inst2 192.168.10.103:80 check inter 2000 fall 3 weight 2
[root@localhost ~]# systemctl start haproxy #启动服务
用104客户端去访问
[root@localhost ~]# curl 192.168.10.101
test web02
[root@localhost ~]# curl 192.168.10.101
test web02
[root@localhost ~]# curl 192.168.10.101
test web01
编辑/etc/haproxy/haproxy.conf
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
log 127.0.0.1 local3
#local3是设备,对应于 /etc/rsyslog.conf中的配置,默认回收info的日志级别
[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
$ModLoad imudp 采集日志的协议UDP
$UDPServerRun 514 指定日志采集使用的端口号
local3.* /var/log/haproxy.log 指定日志存放位置
重启rsyslog和haproxy服务
[root@localhost ~]# systemctl restart rsyslog [root@localhost ~]# systemctl restart haproxy
访问网站后查看该日志
先去客户端测试一下地址,在cat haproxy.log查看该地址
[root@localhost ~]# cd /var/log
[root@localhost log]# cat haproxy.log
Aug 7 11:52:04 localhost haproxy[12632]: Proxy myweb started.
Aug 7 11:53:11 localhost haproxy[12633]: 192.168.10.104:42404 [07/Aug/2024:11:53:11.962] myweb myweb/inst2 0/0/0/0/0 200 251 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Aug 7 11:53:12 localhost haproxy[12633]: 192.168.10.104:42416 [07/Aug/2024:11:53:12.510] myweb myweb/inst2 0/0/0/0/0 200 251 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Aug 7 11:53:13 localhost haproxy[12633]: 192.168.10.104:42428 [07/Aug/2024:11:53:13.010] myweb myweb/inst1 0/0/0/0/0 200 251 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Aug 7 11:53:13 localhost haproxy[12633]: 192.168.10.104:42434 [07/Aug/2024:11:53:13.569] myweb myweb/inst2 0/0/0/0/0 200 251 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"