centos-rhel服务器相关 / 未分类 · 2013年9月11日

keepalived 搭建LVS高可用 Cluste

完成时间:2013.0911
使用DR模式
OS: centos5.9 64
参考 <<高性能网站构建实战>> 当然,这作者抄人家的东西时变量都写错了一个,害我都不太懂相信他r的书了
VIP : 192.168.1.100
LVS_Master_dr(dr1) :192.168.1.10
LVS_Backup_dr(dr2) :192.168.1.11
RealSeerver1 : 192.168.1.14
RealSeerver2 : 192.168.1.15
## http://www.keepalived.org/documentation.html
首先 在两台 DR上安装keepalived 和lvs软件 ipvsadm
modprobe -l | grep ipvs
wget -c http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar xvf keepalived-1.2.6.tar.gz && cd keepalived-1.2.6
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.18-348.el5-x86_64
make -j2 && make install
ln -s /usr/local/sbin/keepalived /sbin/

配置文件请见 keepalived.conf
modprobe -l | grep ipvs
/lib/modules/2.6.18-348.el5/kernel/net/ipv4/ipvs/ip_vs.ko
/lib/modules/2.6.18-348.el5/kernel/net/ipv4/ipvs/ip_vs_dh.k
11.4.1
1,源码安装 下载时要看对应的kernel
wget -c http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
tar xvf ipvsadm-1.24.tar.gz && cd ipvsadm-1.24
ln -s /usr/src/kernels/2.6.18-348.el5-x86_64/ /usr/src/linux
make -j 2 && make install

#http://kb.linuxvirtualserver.org/wiki/Compiling_ipvsadm_on_different_Linux_distributions#Red_Hat_Enterprise_Linux_5
#yum install libnl* popt*
2,#Package ipvsadm.x86_64 0:1.24-13.el5 set to be updated
yum install ipvsadm
ipvsadm --help

#网络配置
http://www.linuxchina.net/?p=1123
**********
配置real server
先安装
vi /etc/init.d/lvsrs
chmod +x /etc/init.d/lvsrs

#yum install nginx
http://wiki.nginx.org/Install
yum install nginx -y
vi /usr/share/nginx/html/index.html
高可用性功能测试
故意停掉 dr1
cat /var/log/messages | grep -E ‘MASTER|Trans*’
……
Sep 11 01:15:01 dr2 kernel: input: AT Translated Set 2 keyboard as /class/input/input0
Sep 11 02:27:55 dr2 Keepalived_vrrp[2776]: VRRP_Instance(VI_1) Transition to MASTER STATE
…..
可见dr1出现故障后,dr2马上检测到,并变为MASTER
停掉其中一台 realserver
[root@DR2 network-scripts]# cat /var/log/messages | grep Removing
Sep 11 03:09:06 dr2 Keepalived_healthcheckers[2775]: Removing service [192.168.1.14]:80 from VS [192.168.1.100]:80
Sep 11 03:15:30 dr2 Keepalived_healthcheckers[2775]: Removing service [192.168.1.15]:80 from VS [192.168.1.100]:80
[root@DR1 ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.100:http wrr persistent 50
-> 192.168.1.15:http Route 3 0 0
-> 192.168.1.14:http Route 3 0 0
三个配置文件
#[root@DR1 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
evan886@gmail.com
}
notification_email_from keepalived@localhost
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.14 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
#bc_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.15 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

[root@localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
evan886@gmail.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
virtual_server 192.168.1.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.14 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.15 80 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}

[root@real1 ~]# cat /etc/init.d/lvsrs
#!/bin/sh
# ip is 123.30.168.177 lvs 1.24 vip 123.30.168.211
#chkconfig: - 11 91 cat see 121.9.242.139 ,web 119.145.139.106
# description: start LVS of Directorserver /etc/rc.d/init.d/lvsdr
ulimit -HSn 65535
VIP=192.168.1.100
. /etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of Real Server"
# set the Virtual IP Address
#/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.0 up
/sbin/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
;;
stop)
echo "close LVS Directorserver"
ifconfig lo:0 down
/sbin/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 "Usage: $0 {start|stop}"
exit 1
esac

参考
VRRP简介
http://www.chinaunix.net/old_jh/30/284898.html