1、目前常见的web集群调度器分为软件和硬件
2、软件通常使用开源的LVS、Haproxy、Nginx
3、硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等
• LVS不支持正则处理,不能实现动静分离
• 对于大型网站,LVS的实施配置复杂,维护成本相对较高
• 适用于负载大的web站点
• 运行在硬件上可支持数以万计的并发连接的连接请求
Haproxy 支持多种调度算法,最常用的有三种
RR算法是最简单最常用的一种算法,即轮询调度
例如:
• 有三个节点A、B、C
• 第一个用户访问会被指派到节点A
• 第一个用户访问会被指派到节点B
• 第一个用户访问会被指派到节点C
• 第四个用户访问会被指派到节点A,轮询分配访问请求实现负载均衡效果
最小连接数算法,根据后端的节点连接数大小动态分配前端请求
例如:
• 有三个节点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算法有很大改进,是目前用到比较多的一-种算法
基于来源访问调度算法,用于一些有Session会记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
例如:
• 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
• 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
• 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引|起负载不均衡,部分节点访问量超大,影响业务使用
是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理。
HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。
HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动,单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。
事件驱动模型因为在有更好的资源和时间管理的用户空间(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作。
包括 GitHub、Bitbucket、StackOverflow、Reddit、Tumblr、Twitter和 Tuenti在内的知名网站,及亚马逊网络服务系统都使用了HAProxy。
可靠性和稳定性非常好,可以与硬件级的F5负载均衡设备相媲美;
最高可以同时维护40000-50000个并发连接,单位时间内处理的最大请求数为20000个,最大处理能力可达10Git/s;
支持多达8种负载均衡算法,同时也支持会话保持;
支持虚机主机功能,从而实现web负载均衡更加灵活;
支持连接拒绝、全透明代理等独特的功能;
拥有强大的ACL支持,用于访问控制;
其独特的弹性二叉树数据结构,使数据结构的复杂性上升到了0(1),即数据的查寻速度不会随着数据条目的增加而速度有所下降;
支持客户端的keepalive功能,减少客户端与haproxy的多次三次握手导致资源浪费,让多个请求在一个tcp连接中完成;
支持TCP加速,零复制功能,类似于mmap机制;
支持响应池 (response buffering);
支持RDP协议;
基于源的粘性,类似nginx的ip hash功能,把来自同一客户端的请求在一定时间内始终调度到上游的同一服务器;
更好统计数据接口,其web接口显示后端集群中各个服务器的接收、发送、拒绝、错误等数据的统计信息;
详细的健康状态检测,web接口中有关于对上游服务器的健康检测状态, 并提供了一定的管理功能;
基于流量的健康评估机制;
基于http认证;
基于命令行的管理接口;
日志分析器,可对日志进行分析
HAProxy负载均衡策略非常多,常见的有如下8种∶
roundrobin∶表示简单的轮询。
static-rr∶表示根据权重。
leastconn∶ 表示最少连接者先处理。
source∶ 表示根据请求的源IP,类似Nginx的IP hash机制。
ri∶表示根据请求的URI。
rl_param∶表示根据HTTP请求头来锁定每 一 次HTrTP请求。
rdp-cookie (name)∶表示根据据cookie (name)来锁定并哈希每一次TCP请求。
LVS基于Linux操作系统实现软负载均衡,而HAProxy和Nginx是基于第三方应用实现的软负载均衡;
LVS是可实现4层的IP负载均衡技术,无法实现基于目录、URL的转发。而HAProxy和Nainx都可以实现4层和7层技术,HAProxy可提供TCP和HrTP应用的负载均衡综合解决方案
LVS因为工作在ISO模型的第四层,其状态监测功能单一, 而HAProxy在状监测方面功能更丰富、强大, 可支持端口、URL、脚本等多种状态检测方式;
HAProxy功能强大,但整体性能低于4层模式的LVS 负载均衡。
Nginx主要用于Web服务器或缓存服务器。
准备工作:
Haproxy服务器:192.168.229.90
Nginx 服务器1:192.168.229.80
Nginx 服务器2:192.168.229.70
客户端:192.168.229.200
systemctl stop firewalld
setenforce 0haproxy-1.5.19.tar.gz
yum install -y pcre-devel bzip2-devel gcc gcc-c++ maketar zxvf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19/
make TARGET=linux2628 ARCH=x86_64
make install
---------------------参数说明--------------------------
TARGET=linux26 #内核版本,
#使用uname -r查看内核,如:2.6.18-371.el5,此时该参数用TARGET=linux26;kernel大于2.6.28的用TARGET=linux2628
ARCH=x86_64 #系统位数,64位系统
mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/cd /etc/haproxy/
vim haproxy.cfg
global
--4~5行--修改,配置日志记录,local0为日志设备,默认存放到系统日志log /dev/log local0 info log /dev/log local0 notice#log loghost local0 infomaxconn 4096 #最大连接数,需考虑ulimit -n限制
--8行--注释,chroot运行路径,为该服务自设置的根目录,一般需将此行注释掉#chroot /usr/share/haproxyuid 99 #用户UIDgid 99 #用户GIDdaemon #守护进程模式defaults log global #定义日志为global配置中的日志定义mode http #模式为httpoption httplog #采用http日志格式记录日志option dontlognull #不记录健康检查日志信息retries 3 #检查节点服务器失败次数,连续达到三次失败,则认为节点不可用redispatch #当服务器负载很高时,自动结束当前队列处理比较久的连接maxconn 2000 #最大连接数contimeout 5000 #连接超时时间clitimeout 50000 #客户端超时时间srvtimeout 50000 #服务器超时时间--删除下面所有listen项--,添加
listen webcluster 0.0.0.0:80 #定义一个名为webcluster的应用option httpchk GET /test.html #检查服务器的test.html文件balance roundrobin #负载均衡调度算法使用轮询算法roundrobinserver inst1 192.168.229.80:80 check inter 2000 fall 3 #定义在线节点server inst2 192.168.229.70:80 check inter 2000 fall 3
---------------------参数说明-----------------------------------------------------
balance roundrobin #负载均衡调度算法
#轮询算法:roundrobin;最小连接数算法:leastconn;来源访问调度算法:source,类似于nginx的ip_hashcheck inter 2000 #表示haproxy服务器和节点之间的一个心跳频率
fall 3 #表示连续三次检测不到心跳频率则认为该节点失效
若节点配置后带有“backup”表示该节点只是个备份节点,只有主节点失效该节点才会上。不携带“backup”,表示为主节点,和其它主节点共同提供服务。
---------------------------------------------------------------------------------
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod +x haproxy
chkconfig --add /etc/init.d/haproxyln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
service haproxy start 或 /etc/init.d/haproxy start
systemctl stop firewalld
setenforce 0yum install -y pcre-devel zlib-devel gcc gcc-c++ makeuseradd -M -s /sbin/nologin nginxcd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make installmake && make install--192.168.229.80---
echo "this is ly web" > /usr/share/nginx/html/index.html--192.168.229.70---
echo "this is weq web" > /usr/share/nginx/html/index.htmlln -s /usr/share/nginx/sbin/nginx /usr/local/sbin/nginx #启动nginx 服或者用yum安装
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
EOFyum install nginx -y
在客户端使用浏览器打开 http://192.168.229.90/index.html ,不断刷新浏览器测试负载均衡效果
[root@192 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1)
[root@192 ~]# setenforce
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
[root@192 ~]# setenforce 0
setenforce: SELinux is disabled
[root@192 ~]# cd /opt/
[root@192 opt]# ls
rh
[root@192 opt]# rz -E
rz waiting to receive.
[root@192 opt]# ls
haproxy-1.5.19.tar.gz rh
[root@192 haproxy-1.5.19]# ls
CHANGELOG CONTRIBUTING ebtree haproxy include Makefile ROADMAP SUBVERS VERDATE
contrib doc examples haproxy-systemd-wrapper LICENSE README src tests VERSION
[root@192 haproxy-1.5.19]# cd examples/
[root@192 examples]# ls
acl-content-sw.cfg content-sw-sample.cfg examples.cfg init.haproxy stats_haproxy.sh
auth.cfg cttproxy-src.cfg haproxy-1.1.21-flx.1.pkg init.haproxy.flx0 tarpit.cfg
build.cfg debug2ansi haproxy.cfg linux-2.4.21-40.EL-custom.diff test-section-kw.cfg
check debug2html haproxy.init option-http_proxy.cfg transparent_proxy.cfg
check.conf debugfind haproxy.spec rc.highsock url-switching.cfg
config.rc.haproxy errorfiles haproxy.vim ssl.cfg
[root@192 examples]# mkdir /etc/haproxy
[root@192 examples]# cp haproxy.cfg /etc/haproxy/
[root@192 examples]# cd /etc/haproxy/
[root@192 haproxy]# ls
haproxy.cfg
[root@192 haproxy]# vim haproxy.cfg1 # this config needs haproxy-1.1.28 or haproxy-1.2.123 global4 log /dev/log local0 info5 log /dev/log local0 notice6 #log loghost local0 info7 maxconn 40968 #chroot /usr/share/haproxy9 uid 9910 gid 9911 daemon12 #debug13 #quiet1415 defaults16 log global17 mode http18 option httplog19 option dontlognull20 retries 321 redispatch22 maxconn 200023 contimeout 500024 clitimeout 5000025 srvtimeout 500002627 listen webcluster 0.0.0.0:8028 option httpchk GET /index.html29 balance roundrobin30 server inst1 192.168.229.80:80 check inter 2000 fall 331 server inst2 192.168.229.70:80 check inter 2000 fall 3
Nginx 服务器1:192.168.229.80
[root@192 ~]# vim /etc/yum.repos.d/nginx.repo
[root@192 html]# cat /etc/yum.repos.d/nginx.repo[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@192 ~]# yum install nginx -y
[root@192 ~]# systemctl start nginx
root@192 ~]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1714/nginx: master
[root@192 ~]# cd /usr/share/nginx/html/
[root@192 html]# ls
50x.html index.html
[root@192 html]# echo 'This is ly web
' >> index.html
Nginx 服务器2:192.168.229.70
[root@192 ~]# vim /etc/yum.repos.d/nginx.repo
[root@192 html]# cat /etc/yum.repos.d/nginx.repo[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
[root@192 ~]# yum install nginx -y
[root@192 ~]# systemctl start nginx
root@192 ~]# netstat -natp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1714/nginx: master
[root@192 ~]# cd /usr/share/nginx/html/
[root@192 html]# ls
50x.html index.html
[root@192 html]# echo 'This is weq web
' >> index.html
在客户端使用浏览器打开 http://192.168.229.90/index.html ,不断刷新浏览器测试负载均衡效果
默认haproxy的日志是输出到系统的syslog中,查看起来不是非常方便,为了更好的管理haproxy的日志,我们在生产环境中一般单独定义出来。需要将haproxy的info及notice日志分别记录到不同的日志文件中
vim /etc/haproxy/haproxy.cfg
globallog /dev/log local0 infolog /dev/log local0 noticeservice haproxy restart#需要修改rsyslog配置,为了便于管理。将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件。
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
&~#说明:
这部分配置是将haproxy的info日志记录到/var/log/haproxy/haproxy-info.log下,将notice日志记录到/var/log/haproxy/haproxy-notice.log下。“&~”表示当日志写入到日志文件后,rsyslog停止处理这个信息。systemctl restart rsyslog.servicetail -f /var/log/haproxy/haproxy-info.log #查看haproxy的访问请求日志信息
[root@192 haproxy]# vim /etc/rsyslog.d/haproxy.confif ($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
&~