ITPub博客

首页 > Linux操作系统 > Linux操作系统 > LVS+keepalived负载均衡

LVS+keepalived负载均衡

Linux操作系统 作者:bitifi 时间:2015-12-18 12:34:24 0 删除 编辑
背景:
随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要的浪费?同时实现商业硬件一样的高性能高可用的功能?有什么好的负载均衡可伸张可扩展的方案吗?答案是肯定的!有!我们利用 LVS+Keepalived基于完整开源软件的架构可以为你提供一个负载均衡及高可用的服务器。

LVS+Keepalived 介绍
LVS
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一.目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);
八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。

Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现
二. 网站负载均衡构架图


IP信息列表:
名称 IP
LVS-DR-Master 61.164.122.6
LVS-DR-BACKUP 61.164.122.7
LVS-DR-VIP 61.164.122.8
WEB1-Realserver 61.164.122.9
WEB2-Realserver 61.164.122.10
GateWay 61.164.122.1
三. 安装LVS和Keepalvied软件包
1. 下载相关软件包
Java代码 复制代码 收藏代码
  1. #mkdir /usr/local/src/lvs
  2. #cd /usr/local/src/lvs
  3. #wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
  4. #wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

2.安装LVS和Keepalived
Java代码 复制代码 收藏代码
  1. #lsmod |grep ip_vs
  2. #uname -r
  3. 2.6.18-53.el5PAE
  4. #ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/ /usr/src/linux
  5. #tar zxvf ipvsadm-1.24.tar.gz
  6. #cd ipvsadm-1.24
  7. #make && make install
  8. #find / -name ipvsadm # 查看ipvsadm的位置
  9. #tar zxvf keepalived-1.1.15.tar.gz
  10. #cd keepalived-1.1.15
  11. #./configure && make && make install
  12. #find / -name keepalived # 查看keepalived位置
  13. #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
  14. #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
  15. #mkdir /etc/keepalived
  16. #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
  17. #cp /usr/local/sbin/keepalived /usr/sbin/
  18. #service keepalived start|stop #做成系统启动服务方便管理.

四. 配置LVS实现负载均衡
1. LVS-DR,配置LVS脚本实现负载均衡
Java代码 复制代码 收藏代码
  1. #vi /usr/local/sbin/lvs-dr.sh
  2. #!/bin/bash
  3. # description: start LVS of DirectorServer
  4. #Written by :NetSeek http://www.linuxtone.org
  5. GW=61.164.122.1
  6. # website director vip.
  7. SNS_VIP=61.164.122.8
  8. SNS_RIP1=61.164.122.9
  9. SNS_RIP2=61.164.122.10
  10. ./etc/rc.d/init.d/functions
  11. logger $0 called with $1
  12. case "$1" in
  13. start)
  14. # set squid vip
  15. /sbin/ipvsadm --set 30 5 60
  16. /sbin/ifconfig eth0:0 $SNS_VIP broadcast $SNS_VIP netmask 255.255.255.255
  17. broadcast $SNS_VIP up
  18. /sbin/route add -host $SNS_VIP dev eth0:0
  19. /sbin/ipvsadm -A -t $SNS_VIP:80 -s wrr -p 3
  20. /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP1:80 -g -w 1
  21. /sbin/ipvsadm -a -t $SNS_VIP:80 -r $SNS_RIP2:80 -g -w 1
  22. touch /var/lock/subsys/ipvsadm >/dev/null 2>&1
  23. ;;
  24. stop)
  25. /sbin/ipvsadm -C
  26. /sbin/ipvsadm -Z
  27. ifconfig eth0:0 down
  28. ifconfig eth0:1 down
  29. route del $SNS_VIP
  30. route del $SS_VIP
  31. rm -rf /var/lock/subsys/ipvsadm >/dev/null 2>&1
  32. echo "ipvsadm stoped"
  33. ;;
  34. status)
  35. if [ ! -e /var/lock/subsys/ipvsadm ];then
  36. echo "ipvsadm stoped"
  37. exit 1
  38. else
  39. echo "ipvsadm OK"
  40. fi
  41. ;;
  42. *)
  43. echo "Usage: $0 {start|stop|status}"
  44. exit 1
  45. esac
  46. exit 0

2. 配置Realserver脚本.
Java代码 复制代码 收藏代码
  1. #vi /usr/local/sbin/realserver.sh
  2. #!/bin/bash
  3. # description: Config realserver lo and apply noarp
  4. #Written by :NetSeek http://www.linuxtone.org
  5. SNS_VIP=61.164.122.8
  6. . /etc/rc.d/init.d/functions
  7. case "$1" in
  8. start)
  9. ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
  10. /sbin/route add -host $SNS_VIP dev lo:0
  11. echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  12. echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  13. echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  14. echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  15. sysctl -p >/dev/null 2>&1
  16. echo "RealServer Start OK"
  17. ;;
  18. stop)
  19. ifconfig lo:0 down
  20. route del $SNS_VIP >/dev/null 2>&1
  21. echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  22. echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
  23. echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
  24. echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
  25. echo "RealServer Stoped"
  26. ;;
  27. *)
  28. echo "Usage: $0 {start|stop}"
  29. exit 1
  30. esac
  31. exit 0

或者采用secondary ip address方式配置

Java代码 复制代码 收藏代码
  1. # vi /etc/sysctl.conf
  2. net.ipv4.conf.lo.arp_ignore = 1
  3. net.ipv4.conf.lo.arp_announce = 2
  4. net.ipv4.conf.all.arp_ignore = 1
  5. net.ipv4.conf.all.arp_announce = 2
  6. #sysctl –p
  7. #ip addr add 61.164.122.8/32 dev lo
  8. #ip add list 查看是否绑定

3. 启动lvs-dr脚本和realserver启本,在DR上可以查看LVS当前状态:
Java代码 复制代码 收藏代码
  1. #watch ipvsadm –ln


五.利用Keepalvied实现负载均衡和和高可用性
1.配置在主负载均衡服务器上配置keepalived.conf
Java代码 复制代码 收藏代码
  1. #vi /etc/keepalived/keepalived.conf

Java代码 复制代码 收藏代码
  1. ! Configuration File for keepalived
  2. global_defs {
  3. notification_email {
  4. cnseek@gmail.com
  5. }
  6. notification_email_from sns-lvs@gmail.com
  7. smtp_server 127.0.0.1
  8. # smtp_connect_timeout 30
  9. router_id LVS_DEVEL
  10. }
  11. # 20081013 written by :netseek
  12. # VIP1
  13. vrrp_instance VI_1 {
  14. state MASTER #备份服务器上将MASTER改为BACKUP
  15. interface eth0
  16. virtual_router_id 51
  17. priority 100 # 备份服务上将100改为99
  18. advert_int 1
  19. authentication {
  20. auth_type PASS
  21. auth_pass 1111
  22. }
  23. virtual_ipaddress {
  24. 61.164.122.8
  25. #(如果有多个VIP,继续换行填写.)
  26. }
  27. }
  28. virtual_server 61.164.122.8 80 {
  29. delay_loop 6 #(每隔10秒查询realserver状态)
  30. lb_algo wrr #(lvs 算法)
  31. lb_kind DR #(Direct Route)
  32. persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
  33. protocol TCP #(用TCP协议检查realserver状态)
  34. real_server 61.164.122.9 80 {
  35. weight 3 #(权重)
  36. TCP_CHECK {
  37. connect_timeout 10 #(10秒无响应超时)
  38. nb_get_retry 3
  39. delay_before_retry 3
  40. connect_port 80
  41. }
  42. }
  43. real_server 61.164.122.10 80 {
  44. weight 3
  45. TCP_CHECK {
  46. connect_timeout 10
  47. nb_get_retry 3
  48. delay_before_retry 3
  49. connect_port 80
  50. }
  51. }
  52. }

2. BACKUP服务器同上配置,先安装lvs再按装keepalived,仍后配置/etc/keepalived/keepalived.conf,只需将红色标示的部分改一下即可.
3.
Java代码 复制代码 收藏代码
  1. vi /etc/rc.local
  2. #/usr/local/sbin/lvs-dr.sh 将lvs-dr.sh这个脚本注释掉。步
  3. #/usr/local/sbin/lvs-dr.sh stop 停止lvs-dr脚本
  4. #/etc/init.d/keepalived start 启动keepalived 服务,keepalived就能利用
keepalived.conf 配置文件,实现负载均衡和高可用.

4. 查看lvs服务是否正常
Java代码 复制代码 收藏代码
  1. #watch ipvsadm –ln

IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddressort Scheduler Flags
-> RemoteAddressort Forward Weight ActiveConn InActConn
TCP 61.164.122.8:80 wrr persistent 60
-> 61.164.122.10:80 Route 3 0 0
-> 61.164.122.9:80 Route 3 0 0
Java代码 复制代码 收藏代码
  1. #tail –f /var/log/message
监听日志,查看状态,测试LVS负载均衡及高可用性是否有效。

5.停Master服务器的keepalived服务,查看BAKCUP服务器是否能正常接管服务。
四.相关参考
1.LVS 基础知识汇总
LVS的算法介绍 http://www.linuxtone.org/viewthread.php?tid=69
学习LVS的三种转发模式 http://www.linuxtone.org/viewthread.php?tid=77
LVS中的IP负载均衡技术 http://www.linuxtone.org/viewthread.php?tid=68
更多的请到http://www.linuxtone.org 负载均衡版查看

Keepalived 相关参考资料。
http://www.keepalived.org/documentation.html

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30496894/viewspace-1874462/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 索引的类型
请登录后发表评论 登录
全部评论

注册时间:2015-09-21

  • 博文量
    211
  • 访问量
    276585