LVS只是做一个负载均衡,通过访问VIP来访问后端的网站程序,一旦LVS宕机,整个网站就访问不了,这就出现了单点。所以要结合keepalive这种高可用软件来保证整个网站的高可用。本文将介绍如何利用keepalive来实现LVS的高可用(以LVS的DR模式为例,生产环境后台的real server 网站内容是一致的,为了看到实验效果,这里是两个不同的页面)。
一、环境介绍
VIP:192.168.1.188
RIP1(Web1): 192.168.1.2
RIP2(Web2): 192.168.1.5
LVS1 :192.168.1.6
LVS2: 192.168.1.7
web1页面
web2页面
二、配置realserver
分别在web1和web2上执行lvs_rserver.sh
1
2
3
4
5
6
|
[root@web1 ~] # ./lvs_rserver.sh start
RealServer Start OK [root@web1 ~] #
[root@web2 ~] # ./lvs_rserver.sh start
RealServer Start OK [root@web2 ~] #
|
lvs_rserver.sh脚本内容如下:
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
|
[root@web1 ~] # cat lvs_rserver.sh
#!/bin/sh #LVS Client Server VIP=192.168.1.188 case $1 in start) ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/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 2>&1
echo "RealServer Start OK"
exit 0
;; stop) ifconfig lo:0 down
route del $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 OK"
exit 1
;; *) echo "Usage: $0 {start|stop}"
;; esac [root@web2 ~] #
|
三、安装配置lvs及keepalive
分别在lvs1和lvs2上安装lvs及keepalive
lvs及keppalive的安装见《构建负载均衡服务器之二 LVS详解及应用》 《构建高可用服务器之一 Keepalive介绍及安装》
在lvs1和lvs2上配置VIP并加载相关模块
1
2
3
4
5
|
[root@lvs1 ~] # ifconfig eth0:0192.168.1.188 netmask 255.255.255.0
[root@lvs1 ~] # modprobe ip_vs
[root@lvs1 ~] # modprobe ip_vs_rr
[root@lvs1 ~] # modprobe ip_vs_wrr
[root@lvs1 ~] #
|
1
2
3
4
5
|
[root@lvs2 ~] # ifconfig eth0:0192.168.1.188 netmask 255.255.255.0
[root@lvs2 ~] # modprobe ip_vs
[root@lvs2 ~] # modprobe ip_vs_rr
[root@lvs2 ~] # modprobe ip_vs_wrr
[root@lvs2 ~] #
|
在lvs1上修改keepalive.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
|
[root@lvs1 ~] # vim/etc/keepalived/keepalived.conf
[root@lvs1 ~] # cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email {
654001593@qq.com
}
notification_email_from 654001593@qq.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
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.188
}
} virtual_server 192.168.1.188 80 { delay_loop 6
lb_algo wrr
lb_kind DR
# persistence_timeout 60 protocol TCP
real_server 192.168.1.2 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.5 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
} [root@lvs1 ~] #
|
lvs2上的keepalive.conf文件基本和lvs1上的一致,但是state这一项lvs1上是MASTER,而lvs2上的BACKUP;优先级priority在lvs1上为100,在lvs2上为90
在lvs2上修改keepalive.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
|
[root@lvs2 ~] # cat/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { notification_email {
654001593@qq.com
}
notification_email_from 654001593@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
} # VIP1 vrrp_instance VI_1 { state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 90
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.188
}
} virtual_server 192.168.1.188 80 { delay_loop 6
lb_algo wrr
lb_kind DR
# persistence_timeout 60 protocol TCP
real_server 192.168.1.2 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.5 80 {
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
} [root@lvs2 ~] #
|
四、测试
停掉lvs1的网卡
去ping VIP一样一直在通信(配置有nopreempt)
通过VIP访问网站
刷新
即使lvs1宕机后又恢复,此时依旧是通过lvs2访问(由是否配置nopreempt决定)。
本文转自Jacken_yang 51CTO博客,原文链接:http://blog.51cto.com/linuxnote/1655103,如需转载请自行联系原作者