[背景]前段时间在dell1950上装了个KVM,跑了几个虚拟机。这两天就在这几台虚拟机里做了个LVS+Keepalived实验。
基本实现如下功能:
(1) 高可用性
(2) 可扩展性
(3) 负载均衡
拓扑如下,
[实现过程]
安装:
(I)客户端安装(apache服务已经运行),只需要执行一个脚本即可
#!/bin/bash
#description : start realserver
VIP=192.168.1.11
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
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
;;
stop)
/sbin/ifconfig lo:0 down
echo "close LVS Directorserver"
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 "Usage: $0 {start|stop}"
exit 1
esac
(II)DR端安装
(1) ipvsadm,keepalived安装
(2) DR主keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_BADBOY1
}
vrrp_sync_group VDR {
group {
VI_cache
}
}
vrrp_instance VI_cache {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.11
}
}
virtual_server 192.168.1.11 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 60
protocol TCP
real_server 192.168.1.21 80 {
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.22 80 {
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
DR备keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_BADBOY2
}
# 20081013 written by :netseek
# VIP1
vrrp_sync_group VDR {
group {
VI_cache
}
}
vrrp_instance VI_cache {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.11
}
}
virtual_server 192.168.1.11 80 {
delay_loop 6
lb_algo rr
lb_kind DR
#persistence_timeout 60
protocol TCP
real_server 192.168.1.21 80 {
#weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.22 80 {
#weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
(III)启动测试
Keepalived –D或者以服务形式运行service keepalived start
(IV)客户端测试脚本
#!/usr/bin/env python
import urllib
import threading
import Queue
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
doc=urllib.urlopen("http://192.168.1.11/index.html").read()
print doc,
if __name__=='__main__':
for i in range(10000):
obj=MyThread()
obj.start()
obj.join()
本文转自hahazhu0634 51CTO博客,原文链接:http://blog.51cto.com/5ydycm/371527,如需转载请自行联系原作者