这里写目录标题
前言
1. 常见的Web集群调度器
目前常见的Web集群调度器分为软件和硬件,软件通常使用开源的LVS、 Haproxy、 Nginx,硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等
2. LVS虽然在企业应用中抗负载能力很强,但存在不足
LVS不支持正则处理,不能实现动静分离
对于大型网站,LVS的实施配置复杂,维护成本相对较高
3. Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
特别适用于负载特别大的Web站点
运行在当前的硬件上可支持数以万计的并发连接连接请求
一、Haproxy调度算法
Haproxy支持多种调度算法,最常用的有三种:RR(Round Robin),LC(Least Connections),SH(Source Hashing)
1.1 RR(Round Robin)
RR算法是最简单最常用的一种算法,即轮询调度
理解举例
有三个节点A、B、C,第一个用户访问会被指派到节点A,第二个用户访问会被指派到节点B,第三个用户访问会被指派到C节点,第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果
1.2 LC(Least Connections)
LC算法即最小连接数算法,根据后端的节点连接数大小动态分配前端请求
理解举例
有三个节点A、B、C,各节点的连接数分别为A:4、B:5、C:6,此时如果有第一个用户连接请求,会被指派到A上,连接数变为A:5、B:5、C:6
第二个用户请求会继续分配到A上,连接数变为A6、B:5、C:6;再有新的请求会分配给B,每次将新的请求指派给连接数最小的客户端
由于实际情况下A、B、C的连接数会动态释放,很难会出现一样连接数的情况,因此此算法相比较rr算法有很大改进,是目前用到比较多的一种算法
1.3 SH(Source Hashing)
SH即基于来源访问调度算法,此算法用于一些有 Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
理解举例
有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第次访问被指派到了B
当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用
二、Haproxy群集部署
1 Haproxy配置文件详解
Haproxy配置文件通常分为三个部分
- global:为全局配置
- defaults:为默认配置
- listen:为应用组件配置
global配置参数
- log127.0.0.1 lcal0:配置日志记录,local0为日志设备,默认存放到系统日志
- log127.0.0.1 loca1 notice:notice为日志级别,通常有24个级别
- maxconn4096:最大连接数
- uid 99:用户uid
- gid 99:用户gid
defaults配置项配置默认参数,一般会被应用组件继承,如果在应用组件中没有特别声明,将安装默认配置参数设置
- log global:定义日志为global配置中的日志定义
- mode http:模式为http
- option httplog:采用http日志格式记录日志
- retries 3:检查节点服务器失败连续达到三次则认为节点不可用
- maxconn2000:最大连接数
- contimeout5000:连接超时时间
- clitimeout50000:客户端超时时间
- srvtimeout50000:服务器超时时间
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:81 check inter 2000 fall 3 backup:定义备份节点
2.1 实验环境
VMware软件
两台centos7虚拟机作为NGINX服务器(IP地址:192.168.200.80/192.168.200.90)
一台centos7虚拟机作为Haproxy(IP地址:192.168.200.60)
2.2 实验拓扑
2.3 实验步骤
1、配置nginx1服务器192.168.200.80
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# su
[root@web1 ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@web1 ~]# useradd -M -s /sbin/nologin nginx
[root@web1 ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@web1 ~]# cd /opt/nginx-1.12.0/
[root@web1 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@web1 nginx-1.12.0]# make
[root@web1 nginx-1.12.0]# make install
[root@web1 nginx-1.12.0]# cd /usr/local/nginx/html/
[root@web1 html]# vim test.html
<h1>this is aliyun web!</h1>
[root@web1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@web1 html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web1 html]# nginx
[root@web1 html]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 70643/nginx: master
用机器访问192.168.200.80/test.html
2、配置nginx2服务器192.168.200.90
nginx2 192.168.200.90
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname web2
[root@localhost ~]# su
[root@web2 ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
[root@web2 ~]# useradd -M -s /sbin/nologin nginx
[root@web2 ~]# tar zxvf nginx-1.12.0.tar.gz -C /opt/
[root@web2 ~]# cd /opt/nginx-1.12.0/
[root@web2 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@web2 nginx-1.12.0]# make
[root@web2 nginx-1.12.0]# make install
[root@web2 nginx-1.12.0]# cd /usr/local/nginx/html/
<h1>this is huawei web!</h1>
[root@web2 html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web2 html]# nginx
[root@web2 html]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15806/nginx: master
用机器访问192.168.200.90/test.html
3、配置haproxy服务器192.168.200.60
安装步骤
安装基础软件包,编译安装 haproxy,要注意操作系统版本,是32位系统还是64位
建立 Haproxy的配置文件
创建配置文件目录/etc/haproxy,将源码包提供的配置文件样例 haproxy.cfg复制到配置文件目录中
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# hostnamectl set-hostname web2
[root@localhost ~]# su
[root@haproxy ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++ make
[root@haproxy ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 模板 图片 下载 桌面
haproxy-1.5.19.tar.gz 公共 视频 文档 音乐
[root@haproxy ~]# tar zxvf haproxy-1.5.19.tar.gz -C /opt/
[root@haproxy ~]# cd /opt/haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# ls
CHANGELOG CONTRIBUTING ebtree include Makefile ROADMAP SUBVERS VERDATE
contrib doc examples LICENSE README src tests VERSION
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install
[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.5.19]# cd /etc/haproxy/
[root@haproxy haproxy]# vim haproxy.cfg
8 #chroot /usr/share/haproxy //注释掉
21 #redispatch //注释掉
删掉所有listen,自己写如下配置
27 listen webcluster 0.0.0.0:80
28 option httpchk GET /test.html //监听检查服务器的test.html文件(节点服务器的test页面)
29 balance roundrobin //负载均衡调度算法使用轮询算法
30 server inst1 192.168.200.80:80 check inter 2000 fall 3 //定义在线节点
31 server inst2 192.168.200.90:80 check inter 2000 fall 3
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy //复制创建启动脚本
[root@haproxy haproxy]# cd /etc/init.d/
[root@haproxy init.d]# ls
functions haproxy netconsole network README
[root@haproxy init.d]# chmod +x haproxy //添加启动脚本权限
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy //为service添加启动脚本
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy //创建命令软连接
[root@haproxy init.d]# service haproxy start //开启haproxy
Starting haproxy (via systemctl): [ 确定 ]
去浏览器输入http://192.168.200.60/test.html
三、Haproxy日志管理
Haproxy的日志默认是输出到系统的 syslog中,在生产环境中一般单独定义出来
定义的方法步骤
修改 Haproxy配置文件中关于日志配置的选项,加入配置:
- log /dev/log local0 info
- log /dev/log local0 notice
修改 rsyslog配置,将 Haproxy相关的配置独立定义到 - haproxy.conf,并放到/etc/rsyslog.d/下
保存配置文件并重启 rsyslog服务,完成 rsyslog配置
[root@haproxy init.d]# ls /dev/log
/dev/log
[root@haproxy init.d]# ls /var/log //未访问网页,查看/var/log 发现没有haproxy文件
anaconda glusterfs rhsm vmware-network.2.log
audit grubby_prune_debug sa vmware-network.log
boot.log lastlog samba vmware-vgauthsvc.log.0
boot.log-20200902 libvirt secure vmware-vmsvc.log
btmp maillog secure-20200902 vmware-vmusr.log
btmp-20200902 maillog-20200902 speech-dispatcher wpa_supplicant.log
chrony messages spooler wtmp
cron messages-20200902 spooler-20200902 Xorg.0.log
cron-20200902 mysql.log sssd Xorg.9.log
cups ntpstats swtpm yum.log
dmesg pluto tallylog
firewalld ppp tuned
gdm qemu-ga vmware-network.1.log
[root@haproxy init.d]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
haproxy.cfg
[root@haproxy haproxy]# vim haproxy.cfg //编辑haproxy配置文件
global
log /dev/log local0
log /dev/log local0 notice
[root@haproxy haproxy]# cd /etc/rsyslog.d/
[root@haproxy rsyslog.d]# ls
listen.conf
[root@haproxy rsyslog.d]# vim haproxy.conf 创建一个新haproxy配置文件
if ($programname == 'haproxy' and $syslogseverity-text == 'info') //编写haproxy配置文件脚本
then -/var/log/haproxy/haproxy-info.log
&~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log
&~
[root@haproxy rsyslog.d]# systemctl restart rsyslog.service
[root@haproxy rsyslog.d]# service haproxy restart
Restarting haproxy (via systemctl): [ 确定 ]
[root@haproxy rsyslog.d]# ls /var/log //查看网页之后,再次查看/var/log已经生成haproxy文件了,可以进去查看
haproxy
[root@haproxy haproxy]# cd /var/log/haproxy/
[root@haproxy haproxy]# ls
haproxy-info.log haproxy-notice.log
四、Haproxy参数优化
随着企业网站负载增加, haproxy参数优化相当重要
- maxconn:最大连接数,根据应用实际情况进行调整,推荐使用10 240
- 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左右就可以了