LVS+keepalived負載均衡

bitifi發表於2015-12-18
背景:
隨著你的網站業務量的增長你網站的伺服器壓力越來越大?需要負載均衡方案!商業的硬體如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的實現
二. 網站負載均衡構架圖
LVS+keepalived負載均衡

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://
  4. #wget http://

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://
  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://
  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的演算法介紹
學習LVS的三種轉發模式
LVS中的IP負載均衡技術
更多的請到 負載均衡版檢視

Keepalived 相關參考資料。
http://www.keepalived.org/documentation.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30496894/viewspace-1874462/,如需轉載,請註明出處,否則將追究法律責任。

相關文章