高可用双机lvs(heartbeat+ipvsadm+ldirectord)

简介:

高可用双机lvs 
1、机器ip

192.168.2.245 虚拟ip
192.168.2.119 lvsmaster lvs主
192.168.2.205 lvsbackup lvs从
192.168.2.120 realserver1 web服务器
192.168.2.127 realserver2 web服务器


2、lvs安装

yum -y install ipvsadm
yum -y install libnet
yum -y install e2fsprogs
yum -y install heartbeat heartbeat-ldirectord
cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/
cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/
cp /usr/share/doc/heartbeat-2.1.3/haresources /etc/ha.d/
cp /usr/share/doc/heartbeat-ldirectord-2.1.3/ldirectord.cf /etc/ha.d/

3、配置步骤
主lvs配置
(1)lvsdr-basic脚本配置

cat >/etc/init.d/lvsdr-basic<<EOF
#!/bin/sh
VIP=192.168.2.245
RIP1=192.168.2.120
RIP2=192.168.2.127
/etc/rc.d/init.d/functions
case "$1" in
start)
echo "start [OK]"
/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:1
echo "1" > /proc/sys/net/ipv4/ip_forward
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t$VIP:80 -s rr
/sbin/ipvsadm -a -t$VIP:80 -r $RIP1:80 -g
/sbin/ipvsadm -a -t$VIP:80 -r $RIP2:80 -g
/sbin/ipvsadm
;;
stop)
echo "stop [OK]"
/sbin/ipvsadm -C
;;
*)
echo "Usage:$0 {start|stop}"
exit 1
esac
EOF

chmod u+x /etc/init.d/lvsdr-basic

(2)ha.cf配置

debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility     local0
deadtime 20
initdead 20
#bcast   eth1
ucast eth0 192.168.2.205
auto_failback on
node    master     主节点
node    lvsbackup
ping_group hacluster 192.168.2.119 192.168.2.205
respawn root /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=root uid=root

(3)authkeys认证文件的配置

auth 1
1 crc
chmod u+x 600 authkeys

(4)ldirectord.cf文件配置

checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.2.245:80
        real=192.168.2.120:80 gate
        real=192.168.2.127:80 gate
        service=http
        request="test.html"
        receive="Test"
        scheduler=sh
        protocol=tcp

(5)haresources文件配置
加入

memcache.dev-innet-shopex.cn IPaddr::192.168.2.245 lvsdr-basic ldirectord

(5)关闭ldirectord服务开启heartbeat服务。

# chkconfig --del ldirectord
# chkconfig --level 2345 ldirectord off
# chkconfig --level 2345 heartbeat on

备份lvs配置
(1)switchdr配置

cat > /etc/init.d/switchdr << EOF
#!/bin/sh
# description: close lo0 and arp_ignore
VIP=192.168.2.245

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

case "$1" in
    start)
        echo "************* start director server and close tunl ***********"
        ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
        ;;
    stop)
        echo "start Real Server"
        ifconfig eth0:0 down
        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
        ;;
    *)
        echo "Usage: switchdr {start|stop}"
        exit 1
esac
EOF
chmod u+x /etc/init.d/switchdr

(2)ha.cf配置

cat > ha.cf << EOF
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility     local0
deadtime 20
initdead 20
#bcast   eth1
ucast eth0 192.168.2.119
auto_failback on
node    memcache.dev-innet-shopex.cn
node    lvsbackup
ping_group hacluster 192.168.2.119 192.168.2.205
respawn root /usr/lib64/heartbeat/ipfail
apiauth ipfail gid=root uid=root
EOF

(3)authkeys 配置

cat > authkeys << EOF
auth 1
1 crc
EOF
chmod 600 authkeys

(4) ldirectord.cf配置

cat > ldirectord.cf << EOF
checktimeout=3
checkinterval=1
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no
virtual=192.168.2.245:80
        real=192.168.2.120:80 gate
        real=192.168.2.127:80 gate
        service=http
        request="test.html"
        receive="Test"
        scheduler=sh
        protocol=tcp

EOF

(5) 配置haresources

memcache.dev-innet-shopex.cn switchdr IPaddr::192.168.2.245 ldirectord


(6)关闭ldirectord服务开启heartbeat服务。

# chkconfig --del ldirectord
# chkconfig --level 2345 ldirectord off
# chkconfig --level 2345 heartbeat on


3、客户端配置
ip 192.168.2.127 192.168.2.120

cat > /etc/init.d/realserver << EOF
#!/bin/bash
#description : start realserver
VIP=192.168.2.245
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 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
EOF


chmod u+x /etc/init.d/realservert
执行# /etc/init.d/realserver

4、启动服务

 

lvs主

/etc/ini.d/heartbeat start

lvs备份

/etc/ini.d/switchdr stop

/etc/ini.d/heartbeat start

5、测试
(1)停掉主备份会接替主,当主恢复时备份自动转为备份状态。 
(2)停止一台nginx真实服务器。ipvsadm将会自动把这一台机器剔除。





    本文转自yuangang_love 51CTO博客,原文链接:http://blog.51cto.com/linux008/538369,如需转载请自行联系原作者



相关文章
|
7天前
|
负载均衡 监控 Linux
CentOS6.5高可用集群LVS+Keepalived(DR模式)
CentOS6.5高可用集群LVS+Keepalived(DR模式)
|
6月前
|
负载均衡 算法 网络协议
Keepalived+LVS搭建高可用负载均衡
Keepalived+LVS搭建高可用负载均衡
187 1
|
7月前
|
负载均衡 应用服务中间件 nginx
71分布式电商项目 - nginx高可用以及lvs+nginx负载均衡(资料)
71分布式电商项目 - nginx高可用以及lvs+nginx负载均衡(资料)
42 0
|
8月前
|
负载均衡 测试技术
LVS+KeepAlived构建高可用集群
LVS+KeepAlived构建高可用集群
55 0
|
8月前
|
域名解析 负载均衡 网络协议
keepalived+vip+lvs高可用负载均衡服务演化
keepalived+vip+lvs高可用负载均衡服务演化
334 0
|
12月前
|
监控 网络协议 数据安全/隐私保护
高可用 - 05 Keepalived配置(全局配置、VRRPD、LVS)
高可用 - 05 Keepalived配置(全局配置、VRRPD、LVS)
427 0
|
12月前
|
负载均衡 网络协议 调度
LVS+Keepalived实现高可用和负载均衡
LVS+Keepalived实现高可用和负载均衡
139 0
|
7月前
|
负载均衡 应用服务中间件 Linux
企业实战(13)LVS负载均衡NAT(网络地址转换)模式实战详解(一)
企业实战(13)LVS负载均衡NAT(网络地址转换)模式实战详解(一)
|
7月前
|
存储 负载均衡 网络协议
企业实战(13)LVS负载均衡DR(直接路由)模式实战详解(二)
企业实战(13)LVS负载均衡DR(直接路由)模式实战详解(二)
112 0
|
6月前
|
负载均衡 应用服务中间件 Linux
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
Nginx系列教程(14) - LVS+KeepAlived+Nginx实现高性能负载均衡集群
197 0