一、 常见的Web群集调度器
- 目前常见的Web集群调度器分为软件和硬件。
- 软件通常使用开源的LVS、Haproxy、Nginx
- 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等
1.1 Haproxy应用分析
- LVS 在企业应用中抗负载能力很强,但存在不足
LVS 不支持正则处理,不能实现动静分离
对于大型网站,LVS的实施配置复杂,维护成本相对较高
- Haproxy 是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
适用于负载大的Web站点
运行在硬件上可支持数以万计的并发连接的连接请求
1.2 Haproxy 调度算法
- RR(Round Robin)
RR算法是最简单最常用的一种算法,即轮询调度
- LC(Least Connections)
最小连接数算法,根据后端的节点连接数大小动态分配前端请求。
- SH (Source Hashing)
基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
举例:
有两个节点 A、B ,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B,当第一个用户第二次访问的时候会继续指派给A,第二个用户第二次访问的时候会继续指派给B
二、Haproxy 配置文件详解
2.1 全局配置 global
参数 |
说明 |
log 127.0.0.1 local0 |
配置日志记录,local0为日志设备,默认存放到系统日志 |
log 127.0.0.1 local1 notice |
notice为日志级别,通常有24个级别 |
maxconn 4096 |
最大连接数 |
uid 99 |
用户uid |
gid 99 |
用户gid |
2.2 默认配置 defaults
参数 |
说明 |
log global |
定义日志为global配置中的日志定义 |
mode http |
模式为http |
option httplog |
采用http日志格式记录日志 |
retries 3 |
检查节点服务器失败连续达到三次则认为节点不可用 |
maxconn 2000 |
最大连接数 |
contimeout 5000 |
连接超时时间 |
clitimeout 5000 |
客户端超时时间 |
srvtimeout 5000 |
服务器超时时间 |
2.3 应用组件配置 listen
参数 |
说明 |
listen appli4-backup 0.0.0.0:10004 |
定义一个appli4-backup的应用 |
option httpchk /indexhtml |
检查服务器的index.html 文件 |
option persist |
强制将请求发送到已经down掉的服务器(配置的时候要注释掉) |
balance roundrobin |
负载均衡调度算法使用轮询算法 |
server inst1 192.168.114.56:80 check inter 2000 fall 3 |
定义在线节点 |
server inst1 192.168.114.56:80 check inter 2000 fall 3 backup |
定义备份节点 |
三、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~10 s,增加http连接释放速度 |
timeout client |
客户端超时时间,如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了 |
四、Haproxy 日志管理
- 默认是输出到系统的syslog中,生产环境中一般单独定义
- 定义的方法步骤
修改Hapeoxy配置文件中关于日志 配置的选项,加入配置
log /dev/log local0 info
log /dev/log local0 notice
- 修改rsyslog配置,将Haproxy相关的配置独立定义到haproxy.conf 并放到 /etc/rsrslog.d/ 下
- 保存配置文件并重启rsyslog服务,完成rsyslog配置
五、Haproxy搭建Web群集实验
5.1 实验环境
- 3台Centos 虚拟机 一台win10 系统
- 1台虚拟即充当Haproxy站点,IP地址为192.168.233.200
- 2台CentOS 虚拟机模拟Nginx服务器 IP地址分别为192.168.233.180 192.168.233.50
- win10 充当客户端
5.2 实验准备工作
- 准备好haproxy-1.5.19.tar和nginx-1.12.0.tar 安装包
- 开始操作前 关闭所有节点的防火墙和核心防护 防止干扰
[root@haproxy ~]# systemctl stop firewalld.service
[root@haproxy ~]# setenforce 0
5.3 节点服务器
root@localhost ~]# hostnamectl set-hostname web1
[root@localhost ~]# su
[root@web1 ~]#
[root@web1 ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y
[root@web1 ~]# useradd -M -s /sbin/nologin nginx
准备安装包nginx-1.12.0.tar
[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 && make install
[root@web1 nginx-1.12.0]# cd /usr/local/nginx/html/
[root@web1 html]# echo "this is accp web" > test.html ## web2 网页内容可以写不一样的,但是文件名称要一样 便于测试
[root@web1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@web1 html]# nginx
[root@web1 html]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 14300/nginx: master
5.4 haproxy服务器
[root@haproxy ~]# yum install pcre-devel bzip2-devel gcc gcc-c++ make -y
准备压缩包haproxy-1.5.19.tar
[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]# 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
# chroot /usr/share/haproxy ## 第八行 注释掉 否则服务起不来
# redispatch ## 第二十一行,注释掉
######### 删掉所有的listen 写入下列的
listen webcluster 0.0.0.0:80
option httpchk GET /test.html
balance roundrobin
server inst1 192.168.233.50:80 check inter 2000 fall 3
server inst2 192.168.233.180: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]# chmod +x /etc/init.d/haproxy
[root@haproxy haproxy]# chkconfig --add /etc/init.d/haproxy
[root@haproxy haproxy]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@haproxy haproxy]# service haproxy start
Starting haproxy (via systemctl): [ 确定 ]
访问http://192.168.233.200/test.html
刷新网页 ,发现两个网页来回切换
5.5 日志配置
[root@haproxy haproxy]# vim /etc/haproxy/haproxy.cfg
global
log /dev/log local0 info ## /dev/log 只是一个设备工具 日志还是会生成到/var/log 里面 local0 级别得一样
log /dev/log local0 notice
[root@haproxy haproxy]# service haproxy restart
Restarting haproxy (via systemctl): [ 确定 ]
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@haproxy haproxy]# vim /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 ~]# service haproxy restart
Restarting haproxy (via systemctl): [ 确定 ]
[root@haproxy ~]# ls /var/log/haproxy/
haproxy-info.log haproxy-notice.log