前言* 随着互联网飞速的发展,互联网WEB服务器,必须要保证每天7x24x365不间断服务,随着人数不断的增加,有什么好的WEB前端架构呢?既能实现高可用、负载均衡、成本有最低廉呢?答案有木有呢?LVS+Keepalived是一个非常不错的前端均衡选择。(今天来研究一下如何快速配置LVS及日常的维护,供参考!)
一、系统环境
1
2
3
4
5
|
LVS-Master 192.168 . 2.108
LVS-BACKUP 192.168 . 2.109
LVS-DR-VIP 192.168 . 2.100
WEB1-Realserver 192.168 . 2.79
WEB2-Realserver 192.168 . 2.80
|
二、Keepalived安装
由于我们使用的是lvs+keepalived,所以这里不需要配置lvs-dr脚本,直接在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
|
#!/bin/sh ###脚本编写目的:自动安装lvs+keepalived ###编写时间: 2011 年 7 月 6 日 17 : 25 : 39
###初稿人:wugk ###定义变量 DIR1=/usr/src DIR2=/usr/local cat << EOF ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++Welcome to use Linux installed a key LVS+KEEPALIVED shells scripts
+++++++++++++++++++++*************************++++++++++++++++++++++++ EOF if [ $UID -ne 0 ];then
echo “This script must use root user ,please exit……”
sleep 2
exit 0
fi download () { cd $DIR1 && wget -c http: //www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
if [ $? = 0 ];then
echo "Download LVS Code is OK!"
else echo "Download LVS Code is failed,Please check!"
exit 1
fi } ipvsadm_install () { ln -s $DIR1/kernels/ 2.6 .* $DIR1/linux
cd $DIR1 && tar xzvf ipvsadm- 1.24 .tar.gz &&cd ipvsadm- 1.24 && make && make install
if [ $? -eq 0 ];then
echo "Install ipvsadm success,please waiting install keepalived ..............."
else echo "Install ipvsadm failed ,please check !"
exit 1
fi } keepalived_install () { cd $DIR1 && tar -xzvf keepalived- 1.1 . 15 .tar.gz &&cd keepalived- 1.1 . 15 && ./configure && make && make install
if [ $? -eq 0 ];then
echo "Install keepalived success,please waiting configure keepalived ..............."
else echo "Install keepalived failed ,please check install version !"
exit 1
fi } ######如果以上软件包编译报错的话,请检查相关的版本跟系统版本之间的关系,然后手动下载安装. keepalived_config () { cp $DIR2/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp $DIR2/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived &&cp $DIR2/etc/keepalived/keepalived.conf /etc/keepalived/ && cp $DIR2/sbin/keepalived /usr/sbin/ if [ $? -eq 0 ];then
echo "Keepalived system server config success!"
else echo "Keepalived system server config failed ,please check keepalived!"
exit 1
fi } PS3= "Please select Install Linux Packages:"
select option in download ipvsadm_install keepalived_install keepalived_config
do $option done |
以上脚本分别在lvs-master和lvs-backup上执行安装。
三、Keepalived配置
也可以参考配置:http://chinaapp.sinaapp.com/download/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
|
! Configuration Filefor keepalived global_defs { notification_email {
wgkgood@ 163 .com
}
notification_email_from wgkgood@ 163 .com
smtp_server 127.0 . 0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
} # VIP1 vrrp_instance VI_1 { state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168 . 2.100
}
} #REAL_SERVER_1 virtual_server 192.168 . 2.100 80 {
delay_loop 6
lb_algo wlc
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168 . 2.79 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
} #REAL_SERVER_2 real_server 192.168 . 2.80 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
} |
注意***Lvs-backup端同样配置,只需要更改state MASTER为state BACKUP,修改priority 100为priority 90即可。
四、WEB服务端配置
如下的VIP1指的是lvs-dr-vip地址,及对外提供访问的虚拟ip:
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
|
#!/bin/sh PS3= "Please Choose whether or not to start a realserver VIP1 configuration:"
select i in "start" "stop"
do case "$i" in
start) read -p "Please enter the virtual server IP address:" VIP1
ifconfig lo: 0 $VIP1 netmask 255.255 . 255.255 broadcast $VIP1
/sbin/route add -host $VIP1 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"
exit 0
;; stop) ifconfig lo: 0 down
route del $VIP1 >/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"
exit 1
;; *) echo "Usage: $0 {start|stop}"
exit 2
esac done |
脚本会提示是否启动,按1即启动,然后输入vip地址 192.168.2.100 ,用ifconfig你会看到:lo:0的ip即表示配置ip成功。
1
2
3
|
lo: 0 Link encap:Local Loopback
inet addr: 192.168 . 2.100 Mask: 255.255 . 255.255
UP LOOPBACK RUNNING MTU: 16436 Metric: 1
|
五、测试LVS+keepalived
1
2
3
4
5
|
/etc/init.d/keepalived restart 查看lvs-master日志: 在浏览器里面访问 http: //192.168.2.100/ 即可!当你宕掉lvs-master后,lvs-backup会接管变成lvs-master提供服务,当lvs-master故障恢复后,lvs-backup又会变成原来的状态!
所安装的脚本可以在这里下载: 在lvs-master和lvs-backup上执行http: //chinaapp.sinaapp.com/download/lvs-keepalived.sh
在web服务器端执行http: //chinaapp.sinaapp.com/download/realserver.sh
|
六、实战建议
1
2
3
4
|
为了解决脑裂问题,需要把主MASTER和备BACKUP各自的keepalived.conf配置文件里面的state 状态都改成BACKUP,优先级分别设置 100 和 90 。
我们常听说脑裂问题,到底什么是脑裂问题呢? 在集群配置中的split-brain现象是指,主从之间互相检测不到心跳,同时抢占资源,导致 服务无法正常访问的情况。所以我们还需要在主备keepalived.conf添加nopreempt 指令! |
附realserver.sh脚本文件:
本文转自 wgkgood 51CTO博客,原文链接:http://blog.51cto.com/wgkgood/605322