设为首页 收藏本站
查看: 1418|回复: 0

[经验分享] 负载均衡方案对比(LVS、HAProxy、OSPF)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-20 11:27:53 | 显示全部楼层 |阅读模式
负载均衡方案对比(LVS、HAProxy、OSPF)
一.         方案概述
1.   LVS
LVS(Linux Virtual Server)使用集群技术和Linux操作系统实现一个高性能、高可用的服务器,它具有很好的可伸缩性。
特点:
1、抗负载能力强、是工作在网络4层之上仅作分发之用,没有流量的产生,这个特点也决定了它在负载均衡软件里的性能最强的;
2、配置性比较低,这是一个缺点也是一个优点,因为没有可太多配置的东西,所以并不需要太多接触,大大减少了人为出错的几率;
3、工作稳定,自身有完整的双机热备方案,如LVS+Keepalived和LVS+Heartbeat,不过我们在项目实施中用得最多的还是LVS/DR+Keepalived;
4、无流量,保证了均衡器IO的性能不会收到大流量的影响;
5、应用范围比较广,可以对所有应用做负载均衡;
6、软件本身不支持正则处理,不能做动静分离,这个就比较遗憾了;其实现在许多网站在这方面都有较强的需求,这个是Nginx/HAProxy+Keepalived的优势所在。
7、如果是网站应用比较庞大的话,实施LVS/DR+Keepalived起来就比较复杂了,特别后面有Windows Server应用的机器的话,如果实施及配置还有维护过程就比较复杂了,相对而言,Nginx/HAProxy+Keepalived就简单多了。
2.   HAProxy
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
特点:
1、HAProxy支持虚拟主机
2、能够补充Nginx的一些缺点比如Session的保持,Cookie的引导等工作
3、支持url检测后端的服务器出问题的检测会有很好的帮助。
4、它跟LVS一样,本身仅仅就只是一款负载均衡软件;单纯从效率上来讲HAProxy更会比Nginx有更出色的负载均衡速度,在并发处理上也是优于Nginx的。
5、HAProxy可以对Mysql读进行负载均衡,对后端的MySQL节点进行检测和负载均衡,不过在后端的MySQL slaves数量超过10台时性能不如LVS,所以我向大家推荐LVS+Keepalived。
6、HAProxy的算法现在也越来越多了,具体有如下8种:
①roundrobin,表示简单的轮询,这个不多说,这个是负载均衡基本都具备的;
②static-rr,表示根据权重,建议关注;
③leastconn,表示最少连接者先处理,建议关注;
④source,表示根据请求源IP,这个跟Nginx的IP_hash机制类似,我们用其作为解决session问题的一种方法,建议关注;
⑤ri,表示根据请求的URI;
⑥rl_param,表示根据请求的URl参数’balance url_param’ requires an URL parameter name;
⑦hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求;
⑧rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。
3.   Ospf(ECMP)
OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在单一自治系统(autonomous system,AS)内决策路由。
ECMP(Equal-CostMultipathRouting)等价多路径,存在多条不同链路到达同一目的地址的网络环境中,如果使用传统的路由技术,发往该目的地址的数据包只能利用其中的一条链路,其它链路处于备份状态或无效状态,并且在动态路由环境下相互的切换需要一定时间,而等值多路径路由协议可以在该网络环境下同时使用多条链路,不仅增加了传输带宽,并且可以无时延无丢包地备份失效链路的数据传输。
特点:
1.4层负载均衡,效率高
2.配置简单,只需安装基于linux的路由软件quagga
3.无法进行监控检查,服务异常无法处理
4. 无session保持等,功能过于简单
二.         方案测试
1.   测试环境

  • LVS服务器
IP:192.168.0.9(主) 192.168.0.10(备)
CPU:Intel(R) Xeon(R) CPU           E5649  @ 2.53GHz
内存:32G
网卡:Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express
操作系统:CentOS release 5.8 (Final)
内核版本:2.6.18-308.8.2.el5

  • Web服务器
IP:192.168.0.14   192.168.0.15
CPU:Intel(R) Xeon(R) CPU           E5640  @ 2.67GHz
内存:32G
网卡:Broadcom NetXtreme II BCM5709 1000Base-T (C0) PCI Express
操作系统:CentOS release 6.3 (Final)
内核版本:2.6.32-220.17.1.el6.x86_64
Web服务:apache+php
2.   LVS测试
1.1   部署实施

  • 安装
Ipvsadm版本:1.2.1
Keepalived版本:1.2.1
Root用户登录192.168.0.9/10系统依次执行以下命令安装:
yum -y install ipvsadm wget gcc gcc-c++ make openssl-devel kernel-devel
Wget http://keepalived.org/software/keepalived-1.2.1.tar.gz
ln  -s  /usr/src/kernels/2.6.18-308.1.1.el5-x86_64/  /usr/src/linux
(将2.6.18-308.1.1.el5-x86_64替换为当前系统内核文件夹即可)
tar zxvf keepalived-1.2.3.tar.gz
cd keepalived-1.2.3
./configure
(注意这个步骤要看到以下字样才是正常的)
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
make && make install

  • 配置
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
chkconfig –add keepalived
chkconfig –level 2345 keepalived on
1).       配置keepalived文件
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc   接收通知的邮箱
}
notification_email_from Alexandre.Cassen@firewall.loc  通知发送邮箱
smtp_server mail.xxx.com     邮箱服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER    keepalived状态参数,主为MASTER,备为SLAVE
interface eth1    keepalived信息发送接口
virtual_router_id 51 虚拟路由ID,同一组LVS需要配置为相同ID
priority 100   优先级,主>备即可
advert_int 1

authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.23  虚拟服务IP
}
}

virtual_server 192.168.0.23 80 { 虚拟服务IP 和端口
delay_loop 6
lb_algo wrr   调度算法
lb_kind DR    工作模式
persistence_timeout 60  会话保持时间
protocol TCP

real_server 192.168.0.14 80 {真实服务IP和端口
weight 1
TCP_CHECK{    健康检查
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.0.15 80 {
weight 1
TCP_CHECK{
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
备机keepalived文件与主大部分相同,只需要更改state和priority字段即可
2).       配置Realserver 启动脚本
其中红色字段为iptables进行端口转换配置,需要根据相应服务真实端口进行变更。
#!/bin/bash
# description: Config realserver lo and apply noarp
WEB_VIP=192.168.0.23

. /etc/rc.d/init.d/functions

case “$1″ in
start)
ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP
/sbin/route add -host $WEB_VIP dev lo:0
echo “1″ >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “2″ >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “1″ >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “2″ >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo “RealServer Start OK”

;;
stop)
ifconfig lo:0 down
route del $WEB_VIP >/dev/null 2>&1
echo “0″ >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo “0″ >/proc/sys/net/ipv4/conf/lo/arp_announce
echo “0″ >/proc/sys/net/ipv4/conf/all/arp_ignore
echo “0″ >/proc/sys/net/ipv4/conf/all/arp_announce
echo “RealServer Stoped”
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP`
isrothere=`netstat -rn | grep “lo:0″ | grep $web_VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo “LVS-DR real server Stopped.”
else
echo “LVS-DR Running.”
fi
;;
*)
# Invalid entry.
echo “$0: Usage: $0 {start|status|stop}”
exit 1
;;
esac
exit 0


  • 验证
访问真实服务地址http://192.168.0.14应用正常
访问真实服务地址http://192.168.0.15应用正常
访问虚拟服务地址http://192.168.0.23应用正常
LVS实现负载均衡原理是通过修改mac字段将访问vip的数据包转发到相应realserver,转发要求RIP和DIP(LVS服务器IP)属于同一个网段才可以进行arp广播。所以现有四川三层架构无法支持LVS,需将LVS和RIP(真实服务器IP)修改为同一网段,变动较大,取消测试。
1.2   功能测试

  • 端口转发:DR模式+iptables
LVS配置虚IP 192.168.0.23 端口80 对应真实服务IP 192.168.0.14/15 端口80
真实服务器192.168.0.14/15 使用启动脚本
测试结果:正常转发请求

  • 主备切换:主LVS停止keepalived服务,service keepalived stop,192.168.0.23端口80服务是否正常
测试结果:实时切换到备LVS提供负载均衡服务,服务正常,且用户不会觉察。
1.3   性能测试
压力测试工具:webbench、http_load
http_load
创建文件#vi urls
写入URL:http://192.168.0.23/index.html
然后执行#./http_load -rate 5 -seconds 10 -parallel 500 urls
参数含义:

-fetches 简写-f :含义是总计的访问次数
-rate 简写-r :含义是每秒的访问频率
-seconds简写-s :含义是总计的访问时间
-parallel 简写-p:并发访问的线程数
urls是一个url 列表,每个url 单独的一行。可以单个页面。

返回结果
369097 fetches, 1000 max parallel, 2.88368e+09 bytes, in 60.0011 seconds

7812.81 mean bytes/connection

6151.5 fetches/sec, 4.80605e+07 bytes/sec

msecs/connect: 105.766 mean, 9036.84 max, 0.117 min

msecs/first-response: 35.2433 mean, 12993.4 max, 0.62 min

9 bad byte counts

HTTP response codes:

  code 200 — 369088

结果分析: 例如
219 fetches, 500 max parallel, 1.36262e+06 bytes, in 10.0008 seconds
219个请求,最大并发数500,总计传输的数据为1.36262e+06 bytes,运行时间10.0008秒
6222 mean bytes/connection
每一连接平均传输的数据量1.36262e+06/219=6222
21.8982 fetches/sec, 136251 bytes/sec
每秒的响应请求为21.8982,每秒传递的数据为136251btyes/sec
msecs/connect: 411.015 mean, 9080.76 max, 69.914 min
没连接的平均响应时间是411.015 means,最大响应时间9080.76 msecs,最小响应时间69.914 msecs
msecs/first-response: 148.292 mean, 3686.02 max, 70.624 min
HTTP response codes:
code 200 — 219


Webbench
参数说明:-c表示并发数,-t表示持续时间(秒)
Webbench –c 1000 –t 60 http://192.168.0.23/index.html
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://192.168.0.23/index.html
1000 clients, running 60 sec.

Speed=398317 pages/min, -17916648 bytes/sec.
Requests: 398284 susceed, 33 failed.

3.   HAProxy测试
2.1   部署实施

  • 安装
使用root用户登录192.168.0.9
依次运行以下命令:
Wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.21.tar.gz
1).       在haprxoy安装包目录下生成安装脚本haproxy_install.sh
#!/bin/bash
#install haproxy
#20111207 by dongnan

#variables
dir=/usr/local
ha_dir=${dir}/haproxy
ha_cfg=${ha_dir}/haproxy.cfg
kernel=`uname -r | grep ’2.6′`
pcre=$(rpm -qa | grep ‘pcre’ | wc -l)
echo “$dir, $ha_dir, $ha_cfg, $kernel, $pcre”


#check
if [ ! "$kernel" -o "$pcre" -lt "2" ];then
echo -e “the script need linux 2.6 kernel and pcre pcre-devel \nyou can usage ‘yum install pcre pcre-devel’ or ‘rpm -ivh pcre-devel-6.6-2.el5_1.7.x86_64.rpm’”
exit 1
fi

#function

install_ha_cfg (){
#configure haproxy.cfg
#default configure file for test,but need your change the frontend server and backend server ip address,
#good luck!

echo ‘
global
log 127.0.0.1   local0
maxconn 40960              #单个进程最大连接数
chroot /usr/local/haproxy #安装目录
uid 99                    #用户haproxy
gid 99                    #组haproxy
daemon                    #守护进程运行
nbproc 1                  #进程数量
pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid

defaults
log     global
mode    http               #7层 http;4层tcp
option  httplog            #http 日志格式
option  httpclose          #主动关闭http通道
option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option log-health-checks   #记录健康检查日志
option  dontlognull        #不记录空连接
maxconn 30000               #最大连接数
contimeout      5000       #连接超时(毫秒)
clitimeout      50000      #客户端超时(毫秒)
srvtimeout      50000      #服务器超时(毫秒)

frontend haproxy_kx         #定义前端服务器(haproxy)
bind 0.0.0.0:80    #监听地址
default_backend server_pool  #指定后端服务器群
#errorfile 502 /usr/local/haproxy/html/maintain.html
#errorfile 503 /usr/local/haproxy/html/maintain.html
#errorfile 504 /usr/local/haproxy/html/maintain.html

backend server_pool           #定义后端服务器群(web server/apache/nginx/iis..)
mode http
option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP
#balance roundrobin    #负载均衡的方式,轮询方式
balance leastconn     #负载均衡的方式,最小连接
cookie SERVERID insert nocache       #插入serverid到cookie中,serverid后面可以定义
#option  httpchk HEAD /check.html #用来做健康检查html文档
server server1 192.168.0.14:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
server server2 192.168.0.15:80 cookie server2 check inter 2000 rise 3 fall 3 weight 3
#服务器定义:
#cookie server1表示serverid为server1;
#check inter 2000 是检测心跳频率(check 默认 );
#rise 3 表示 3次正确认为服务器可用;
#fall 3 表示 3次失败认为服务器不可用;
#weight 表示权重。

listen admin_stat                   #status
bind *:8080                     #监听端口
mode http                       #http的7层模式
stats refresh 30s               #统计页面自动刷新时间
stats uri /haproxy-stats        #统计页面URL
stats realm Haproxy\ Statistics #统计页面密码框上提示文本
stats auth admin:admin          #统计页面用户名和密码设置
stats hide-version              #隐藏统计页面上HAProxy的版本信息
stats admin if TRUE             #手工启用/禁用,后端服务器’ > “$ha_cfg” && sed -i ’1 d’ “$ha_cfg”
}

#install
if [ ! -e "$ha_dir" ];then
tar zxf haproxy*.tar.gz
cd haproxy*/
make TARGET=linux26 USE_STATIC_PCRE=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && mkdir /usr/local/haproxy/{html,logs}
cd ../
#
if [ ! -e "$ha_dir" ];then
echo “error! can’t install haproxy  please check ! Will now out of the script !”
exit 1
else
! grep ‘haproxy’ /etc/syslog.conf && echo ‘local1.*            /var/log/haproxy.log’ >> /etc/syslog.conf
sed -ir ‘s/SYSLOGD_OPTIONS=”-m 0″/SYSLOGD_OPTIONS=”-r -m 0″/g’ /etc/sysconfig/syslog && /etc/init.d/syslog restart
install_ha_cfg
rm -rf haproxy*/
fi
else
echo “haproxy is already exists!”
fi
2).       运行脚本安装
sh haproxy_install.sh(注意脚本要和安装包在同一个目录下)
3).       主备双机热备部署
Root用户执行:
yum -y install heartbeat
yum -y install heartbeat
cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/ #heartbeat主配置文件
cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/ #权限验证文件
cp /usr/share/doc/heartbeat-2.1.3/haresources /etc/ha.d/ #资源配置文件,设置共享IP
cd /etc/ha.d
chmod 600 /etc/ha.d/authkeys
vi authkeys
auth 1
1 crc

vi ha.cf
logfile /var/log/ha-log
logfacility local0
keepalive 2
deadtime 10
warntime 5
initdead 120
udpport 694
ucast eth0 192.168.0.9 # 注意,此处各服务器上的配置不同,此参数应设置为另一台服务器的IP地址
auto_failback on
node HA01 #主服务器 uname –n 显示的主机名
node HA02#备服务器 uname –n 显示的主机名
vi haresources
HA01 IPaddr::192.168.0.24/32/eth0(HA01为主节点主机名,192.168.0.24为服务虚地址,32为掩码,eth0为接口)
chkconfig heartbeat on
service heartbeat start(注意先起主服务器heartbeat,等主服务虚地址启用后再起备服务器的heartbeat)
service heartbeat start

  • 配置
1).       生成haproxy启动脚本haproxy.sh
内容如下:
# cat /usr/local/sbin/haproxy.sh
#!/bin/bash
#haproxy command
#ver:0.1bate
#20111129 by dongnan
#/usr/local/haproxy/sbin/haproxy
#HA-Proxy version 1.4.18 2011/09/16
#Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
&#61656;         #
&#61656;         #Usage : haproxy [-f <cfgfile>]* [ -vdVD ] [ -n <maxconn> ] [ -N <maxpconn> ]
&#61656;         #        [ -p <pidfile> ] [ -m <max megs> ]
&#61656;         #        -v displays version ; -vv shows known build options.
&#61656;         #        -d enters debug mode ; -db only disables background mode.
&#61656;         #        -V enters verbose mode (disables quiet mode)
&#61656;         #        -D goes daemon
&#61656;         #        -q quiet mode : don’t display messages
&#61656;         #        -c check mode : only check config files and exit
&#61656;         #        -n sets the maximum total # of connections (2000)
&#61656;         #        -m limits the usable amount of memory (in MB)
&#61656;         #        -N sets the default, per-proxy maximum # of connections (2000)
&#61656;         #        -p writes pids of all children to this file
&#61656;         #        -de disables epoll() usage even when available
&#61656;         #        -ds disables speculative epoll() usage even when available
&#61656;         #        -dp disables poll() usage even when available
&#61656;         #        -sf/-st [pid ]* finishes/terminates old pids. Must be last arguments.
&#61656;
&#61656;         #variables
&#61656;         haproxy_dir=/usr/local/haproxy/
&#61656;         haproxy_conf=${haproxy_dir}haproxy.cfg
&#61656;         haproxy_pid=${haproxy_dir}logs/haproxy.pid
&#61656;         haproxy_cmd=${haproxy_dir}sbin/haproxy
&#61656;         #test variables
&#61656;         #file $haproxy_dir; file $haproxy_conf; file $haproxy_cmd; file $haproxy_pid
&#61656;
&#61656;
&#61656;
&#61656;         if [ &quot;$#&quot; -eq &quot;0&quot; ];then
&#61656;             echo “usage: $0 {start|stop|restart}”
&#61656;             exit 1
&#61656;         fi
&#61656;
&#61656;         if [ &quot;$1&quot; = &quot;start&quot; ];then
&#61656;         #echo $1
&#61656;             $haproxy_cmd -f $haproxy_conf
&#61656;         elif [ &quot;$1&quot; = &quot;stop&quot; ];then
&#61656;         #echo $1
&#61656;             kill `cat $haproxy_pid`
&#61656;         elif [ &quot;$1&quot; = &quot;restart&quot; ];then
&#61656;         #echo $1
&#61656;             $haproxy_cmd -f $haproxy_conf -st `cat $haproxy_pid`
&#61656;
&#61656;         else
&#61656;            echo “usage: $0 arguments only start and stop or restart !”
&#61656;     fi

2).       更改文件
Chmod 777 haproxy_install.sh haproxy.sh
在haproxy安装包目录下执行安装脚本,安装完毕,更改haproxy.cfg配置文件
&#61656;     ./haproxy_install.sh
&#61656;     vi /usr/local/haproxy/haproxy.cfg
&#61656;     global
&#61656;         log 127.0.0.1   local0
&#61656;         maxconn 40960              #最大连接数
&#61656;         chroot /usr/local/haproxy #安装目录
&#61656;         uid 99                    #用户haproxy
&#61656;         gid 99                    #组haproxy
&#61656;         daemon                    #守护进程运行
&#61656;         nbproc 1                  #进程数量
&#61656;         pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid
&#61656;
&#61656;     defaults
&#61656;        log     global
&#61656;        mode    http               #7层 http;4层tcp
&#61656;        option  httplog            #http 日志&#26684;式
&#61656;        option  httpclose          #主动关闭http通道
&#61656;        option  redispatch         #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
&#61656;
&#61656;        option  dontlognull
&#61656;        maxconn 30000               #最大连接数
&#61656;        contimeout      5000       #连接超时(毫秒)
&#61656;        clitimeout      50000      #客户端超时(毫秒)
&#61656;        srvtimeout      50000      #服务器超时(毫秒)
&#61656;
&#61656;     frontend haproxy_kx         #定义前端服务器(haproxy)
&#61656;             bind 192.168.0.9:80    #监听地址
&#61656;             default_backend server_pool  #指定后端服务器群
&#61656;             #errorfile 502 /usr/local/haproxy/html/maintain.html
&#61656;             #errorfile 503 /usr/local/haproxy/html/maintain.html
&#61656;             #errorfile 504 /usr/local/haproxy/html/maintain.html
&#61656;
&#61656;     backend server_pool           #定义后端服务器群(web server/apache/nginx/iis..)
&#61656;             mode http
&#61656;             option  forwardfor    #后端服务器(apache/nginx/iis/*),从Http Header中获得客户端IP
&#61656;             #balance roundrobin    #负载均衡的方式,轮询方式
&#61656;             balance leastconn     #负载均衡的方式,最小连接
&#61656;             cookie SERVERID       #插入serverid到cookie中,serverid后面可以定义
&#61656;             #option  httpchk HEAD /check.html #用来做健康检查html文档
&#61656;             server server1 192.168.0.14:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
&#61656;             server server2 192.168.0.15:80 cookie server2 check inter 2000 rise 3 fall 3 weight 3
&#61656;             #server server3 10.0.1.254:80 cookie server3 check maxconn 90 rise 2 fall 3 weight 3
&#61656;     #服务器定义:
&#61656;     #cookie server1表示serverid为server1;
&#61656;     #check inter 2000 是检测心跳频率(check 默认 );
&#61656;     #rise 3 表示 3次正确认为服务器可用;
&#61656;     #fall 3 表示 3次失败认为服务器不可用;
&#61656;     #weight 表示权重。
&#61656;
&#61656;     listen admin_stat                   #status
&#61656;         bind *:8080                     #监听端口
&#61656;         mode http                       #http的7层模式
&#61656;         stats refresh 30s               #统计页面自动刷新时间
&#61656;         stats uri /haproxy-stats        #统计页面URL
&#61656;         stats realm Haproxy\ Statistics #统计页面密码框上提示文本
&#61656;         stats auth admin:admin          #统计页面用户名和密码设置
&#61656;         stats hide-version              #隐藏统计页面上HAProxy的版本信息
&#61656;         stats admin if TRUE             #手工启用/禁用,后端服务器
3).       启动haproxy
&#61656;     /usr/local/haproxy/haproxy.sh start
&#61656;     查看进程ps –ef | grep haproxy已经运行

  • 验证
多次访问http://192.168.0.24显示相应页面即说明负载成功
访问http://192.168.0.24:8080/haproxy-stats 查看当前负载状态
默认用户admin 密码admin
2.2   功能测试

  • 负载均衡测试
多次访问http://192.168.0.24显示realserver ip address 在192.168.0.14和
192.168.0.15之间切换

  • 主备切换测试
主192.168.0.9上执行service heartbeat stop
虚IP 192.168.0.24漂移到192.168.0.10(备)上,访问服务http://192.168.0.24正常
主192.168.0.9上执行service heartbeat start
虚IP 192.168.0.24漂移回192.168.0.9(主)上,访问服务http://192.168.0.24正常
2.3   性能测试

  • http性能测试
http_load
参数说明 –p  并发用户数 –f  总的访问次数 urllist1 http://192.168.0.24
测试1:
[iyunv@HA010 ~]# http_load -p 1000 -f 20000 urllist1
20000 fetches, 1000 max parallel, 5.4e&#43;06 bytes, in 5.08898 seconds
270 mean bytes/connection
3930.06 fetches/sec, 1.06112e&#43;06 bytes/sec
msecs/connect: 2.59386 mean, 3000.77 max, 0.079 min
msecs/first-response: 143.209 mean, 3246.75 max, 1.292 min
HTTP response codes:
code 200 – 20000
测试2:
[iyunv@HA010 ~]# http_load -p 1000 -f 30000 urllist1
30000 fetches, 1000 max parallel, 8.1e&#43;06 bytes, in 8.2654 seconds
270 mean bytes/connection
3629.59 fetches/sec, 979989 bytes/sec
msecs/connect: 0.348845 mean, 1.385 max, 0.08 min
msecs/first-response: 145.923 mean, 6260.67 max, 5.663 min
HTTP response codes:
code 200 – 30000
测试3
http_load -p 1000 -f 50000 urllist1
总访问次数50000 并发1000 导致haproxy服务异常,realserver TCP连接大量time wait
Siege
参数说明 –c 并发用户数量 –r 重复测试次数 urllist1 http://192.168.0.24
测试1:
[iyunv@HA010 ~]# siege -c 1000 -r 20 -f urllist1
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege..      done.

Transactions:                  20000 hits
Availability:                 100.00 %
Elapsed time:                  19.25 secs
Data transferred:               5.15 MB
Response time:                  0.08 secs
Transaction rate:            1038.96 trans/sec
Throughput:                     0.27 MB/sec
Concurrency:                   78.64
Successful transactions:       20000
Failed transactions:               0
Longest transaction:            3.24
Shortest transaction:           0.00
测试2:
[iyunv@HA010 ~]# siege -c 1000 -r 30 -f urllist1
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege..      done.

Transactions:                  30000 hits
Availability:                 100.00 %
Elapsed time:                  26.16 secs
Data transferred:               7.72 MB
Response time:                  0.06 secs
Transaction rate:            1146.79 trans/sec
Throughput:                     0.30 MB/sec
Concurrency:                   65.63
Successful transactions:       30000
Failed transactions:               0
Longest transaction:            3.15
Shortest transaction:           0.00
测试3:
[iyunv@HA010 ~]# siege -c 1000 -r 50 -f urllist1
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege…[error] socket: unable to connect sock.c:222: Connection timed out
[error] socket: unable to connect sock.c:222: Connection timed out
siege aborted due to excessive socket failure; you
can change the failure threshold in $HOME/.siegerc

Transactions:                  37886 hits
Availability:                  95.08 %
Elapsed time:                  58.34 secs
Data transferred:               9.95 MB
Response time:                  0.42 secs
Transaction rate:             649.40 trans/sec
Throughput:                     0.17 MB/sec
Concurrency:                  275.07
Successful transactions:       37886
Failed transactions:            1960
Longest transaction:           18.00
Shortest transaction:           0.00
5%访问无法响应
总访问次数50000 并发1000 导致haproxy服务异常,realserver TCP连接大量time wait
4.   Ospf测试
3.1   部署实施

  • 安装
Web服务器192.168.0.14、192.168.0.15上安装quagga软件
Yum install quagga

  • 配置
1).    配置web服务器quagga
使用root用户登录192.168.0.14和192.168.0.15
cd /etc/quagga
cp ospfd.conf.sample ospfd.conf
chkconfig zebra on
chkconfig ospfd on
service zebra start
service ospfd start
telnet localhost ospfd
输入默认密码zebra
ospfd> en
ospfd# conf t
ospfd(config)# router ospf
ospfd(config-router)# router-id 192.168.2.66
ospfd(config-router)# network 192.168.0.23/32 area 0.0.0.0(lo IP)
ospfd(config-router)# network 192.168.2.64/30 area 0.0.0.0(接口IP)
ospfd(config-router)#end
ospfd#wr
2).    配置交换机端
根据交换机类型配置ospf与主机quagga建立ospf邻居关系
3).    配置主机
使用root用户分别在2台web服务上执行以下命令
ifconfig lo:0 192.168.0.23 netmask 255.255.255.255 up

  • 验证
多次访问http://192.168.0.23显示相应页面即说明负载成功

3.2   功能测试

  • 负载均衡测试
多次访问http://192.168.0.23显示realserver ip address 在192.168.0.14和
192.168.0.15之间切换

  • 服务切换测试
Down掉192.168.0.14/15上的服务IP 192.168.0.23可以正常访问应用
3.3   性能测试

  • http性能测试
http_load
参数说明 –p  并发用户数 –f  总的访问次数 urllist1 http://192.168.0.23
测试1:
[iyunv@HA010 ~]# http_load -p 1000 -f 20000 urllist2
20000 fetches, 1000 max parallel, 5.4e&#43;06 bytes, in 5.16173 seconds
270 mean bytes/connection
3874.67 fetches/sec, 1.04616e&#43;06 bytes/sec
msecs/connect: 128.888 mean, 3001.72 max, 0.068 min
msecs/first-response: 25.953 mean, 3031.86 max, 0.63 min
HTTP response codes:
code 200 – 20000
测试2:
[iyunv@HA010 ~]# http_load -p 1000 -f 30000 urllist2
30000 fetches, 1000 max parallel, 8.1e&#43;06 bytes, in 5.72167 seconds
270 mean bytes/connection
5243.23 fetches/sec, 1.41567e&#43;06 bytes/sec
msecs/connect: 84.9997 mean, 3001.48 max, 0.062 min
msecs/first-response: 18.0883 mean, 2813.84 max, 0.605 min
HTTP response codes:
code 200 – 30000
测试3:
[iyunv@HA010 ~]# http_load -p 1000 -f 50000 urllist2
50000 fetches, 1000 max parallel, 1.35e&#43;07 bytes, in 8.0187 seconds
270 mean bytes/connection
6235.43 fetches/sec, 1.68356e&#43;06 bytes/sec
msecs/connect: 101.749 mean, 3001.27 max, 0.07 min
msecs/first-response: 16.8017 mean, 1964.43 max, 0.601 min
HTTP response codes:
code 200 – 50000
总访问次数50000 并发1000,有TCP time wait,但http_load返回正常
测试4:
[iyunv@HA010 ~]# http_load -p 1000 -f 100000 urllist2
总访问次数100000 并发1000 导致TCP连接大量time wait

Siege
参数说明 –c 并发用户数量 –r 重复测试次数 urllist1 http://192.168.0.23
测试1:
[iyunv@HA010 ~]# siege -c 1000 -r 20 -f urllist2
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege..      done.

Transactions:                  20000 hits
Availability:                 100.00 %
Elapsed time:                  19.16 secs
Data transferred:               5.15 MB
Response time:                  0.03 secs
Transaction rate:            1043.84 trans/sec
Throughput:                     0.27 MB/sec
Concurrency:                   30.30
Successful transactions:       20000
Failed transactions:               0
Longest transaction:            3.01
Shortest transaction:           0.00
测试2:
[iyunv@HA010 ~]# siege -c 1000 -r 30 -f urllist2
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege..      done.

Transactions:                  30000 hits
Availability:                 100.00 %
Elapsed time:                  24.14 secs
Data transferred:               7.72 MB
Response time:                  0.01 secs
Transaction rate:            1242.75 trans/sec
Throughput:                     0.32 MB/sec
Concurrency:                   15.34
Successful transactions:       30000
Failed transactions:               0
Longest transaction:            3.00
Shortest transaction:           0.00
测试3:
[iyunv@HA010 ~]# siege -c 1000 -r 50 -f urllist2
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege..      done.

Transactions:                  50000 hits
Availability:                 100.00 %
Elapsed time:                  37.18 secs
Data transferred:              12.87 MB
Response time:                  0.01 secs
Transaction rate:            1344.81 trans/sec
Throughput:                     0.35 MB/sec
Concurrency:                    9.42
Successful transactions:       50000
Failed transactions:               0
Longest transaction:            3.01
Shortest transaction:           0.00
总访问次数50000 并发1000,有TCP time wait,但siege返回正常
测试4:
[iyunv@HA010 ~]# siege -c 1000 -r 100 -f urllist2
** SIEGE 2.72
** Preparing 1000 concurrent users for battle.
The server is now under siege..      done.

Transactions:                 100000 hits
Availability:                 100.00 %
Elapsed time:                  66.28 secs
Data transferred:              25.75 MB
Response time:                  0.01 secs
Transaction rate:            1508.75 trans/sec
Throughput:                     0.39 MB/sec
Concurrency:                    9.66
Successful transactions:      100000
Failed transactions:               0
Longest transaction:            3.02
Shortest transaction:           0.00
总访问次数100000 并发1000,有TCP time wait,但siege返回正常
三.         方案对比
方案名称

HAProxy

Ospf(ECMP)

LVS

方案复杂度

简单

简单

略复杂

实施难度

简单

简单

困难

会话保持




监控统计

有监控统计界面



主备冗余




性能




四.         附录

  • 测试用PHP主页代码
<?php

session_start();

$_SESSION['time'] =date(“Y:m:d:H:s”,time());

echo “time”.”<font color=red>”.$_SESSION['time'].”</font>”.”<br>”;

echo “realserver ip address”.”<font color=red>”.$_SERVER['SERVER_ADDR'].”</font>”.”<br>”;

echo “local address”.”<font color=red>”.$_SERVER['SERVER_NAME'].”</font>”.”<br>”;

echo “SESSIONNAME”.”<font color=red>”.session_name().”</font>”.”<br>”;

echo “SESSIONID”.”<font color=red>”.session_id().”</font>”.”<br>”;

?>
查看主机TCP连接情况 netstat -nat|awk ‘{print awk $NF}’|sort|uniq -c|sort -n
转自:http://www.anste.com/blog/67

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141477-1-1.html 上篇帖子: 负载均衡 下篇帖子: 如何选择Haproxy和Nginx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表