首页 > 代码库 > LVS+Keepalived负载均衡主备

LVS+Keepalived负载均衡主备

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

Keepalvied
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现


一、LVS+Keepalived负载均衡主备架构

I. 网站负载均衡拓朴图

(略)

IP信息清单:
名称 IP

  1. LVS-DR-Master 192.168.2.98

  2. LVS-DR-Backup 192.168.2.99

  3. LVS-DR-VIP 192.168.2.100

  4. Realserver01 192.168.2.101

  5. Realserver02 192.168.2.102

  6. Realserver03 192.168.2.103

  7. Realserver04 192.168.2.104

II.安装ipvsadmkeepalived软件包

ipvsadm: http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

keepalived:http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

下载后建议统一放置在/usr/local/src/lvs

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#uname -r  

2.6.18-53.el5PAE   //查看内核版本  

#ln -s /usr/src/kernels/2.6.18-53.el5PAE-i686/  /usr/src/linux  //防止报错  

#tar zxvf ipvsadm-1.24.tar.gz  

#cd ipvsadm-1.24  

#make && make install  

#find / -name ipvsadm  # 查看ipvsadm的位置  

#tar zxvf keepalived-1.1.15.tar.gz  

#cd keepalived-1.1.15  

#./configure  && make && make install  

#find / -name keepalived  # 查看keepalived位置                  

#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/  

#service keepalived start|stop     #做成系统启动服务方便管理.

III. 配置Realserver脚本

在4台Realserver主机上建立如下脚本,然后执行

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#!/bin/bash  

VIP=192.168.2.100  

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

case “$1″ in  

start)  

           ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up  

           /sbin/route add -host $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  

           echo "RealServer Start OK"  

           ;;  

stop)  

           ifconfig lo:0 down  

           route del $VIP 2>/dev/null  

           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  

           echo "RealServer Stoped"  

           ;;  

*)  

           echo "Usage: $0 {start|stop}"  

           exit 1  

esac  

exit 0

IV. 配置keepalived实现负载均衡和和高可用性

1.在LVS-DR-Master主机上编辑keepalived的配置文件 : vim /etc/keepalived/keepalived.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

! Configuration File for keepalived  

  

global_defs {  

notification_email {  

//此处填写你的邮件地址 }  

notification_email_from your email!  

  

smtp_server smtp.**.com  

smtp_connect_timeout 30  

router_id LVS_DEVEL  

}  

vrrp_instance VI_1 {  

state MASTER  

interface eth0  

lvs_sync_daemon_inteface eth0 //如是同一个网卡,可省略  

virtual_router_id 77 //标识ID,请务必保证内网唯一性,和备机必须一致!  

priority 100 //权重需比备机高  

advert_int 2 //主备检测间隔  

authentication { //主备通信密码,必须一致  

auth_type PASS  

auth_pass 1111  

}  

virtual_ipaddress {  

192.168.2.100 VIP,多个请换行填写  

  

}  

}  

  

virtual_server 192.168.2.100 80 {  

delay_loop 6 //每隔6秒检测Realserver健康状况  

lb_algo wlc //wlc算法,其他算法请参考ipvsadm十大算法  

lb_kind DR // 采用DR模式,即直接路由,其他还有TUN和NAT两种模式。  

persistence_timeout 1800 //设置超时未半小时  

protocol TCP  

  

real_server 192.168.2.101 80 { //第一个realserver,请注意80和后面{之间有个空格!  

weight 100 //权重  

HTTP_GET { //通过检测网页来检查Realserver健康状态  

url {  

path /XX/index.jsp //Realserver上的网页,如/model/index.jsp  

status_code 200 //正确的返回值,一般是200  

}  

connect_timeout 3 //超时3秒,则失败  

nb_get_retry 3 //失败重试次数  

delay_before_retry 3 //重试间隔时间  

}  

}  

  

real_server 192.168.2.102 80 {  

weight 100  

TCP_CHECK { //此范例为TCP检查方法,区别于HTTP方法,推荐使用HTTP方法。  

connect_timeout 10  

nb_get_retry 3  

delay_before_retry 3  

connect_port 80  

}  

}  

}  

  

real_server 192.168.2.103 80 {  

weight 100  

HTTP_GET {  

url {  

path /XX/index.jsp  

status_code 200  

}  

connect_timeout 3  

nb_get_retry 3  

delay_before_retry 3  

}  

}  

  

real_server 192.168.2.104 80 {  

weight 100  

HTTP_GET {  

url {  

path /XX/index.jsp  

status_code 200  

}  

connect_timeout 3  

nb_get_retry 3  

delay_before_retry 3  

}  

}

 

 

2.编辑LVS-DR-Backup上的keepalived配置文件(可拷贝Master上的),然后如下修改:

1

2

state BACKUP //主机上则为Master  

priority 90 //权值小于主机

其余保持不变即可。

V. 启动LVS

Realserver脚本启动之后不需要操作,然后依次启动LVS主机上的keepalived服务即可: service keepalived start


本文出自 “XFICC” 博客,请务必保留此出处http://xficc.blog.51cto.com/1189288/1564395

LVS+Keepalived负载均衡主备