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

时间:2021-02-21 03:06:34

高可用双机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将会自动把这一台机器剔除。

爱慕尔商城欢迎您的光临!
穿衣打扮  
城市物语