LVS+Keepalived負載均衡集羣之DR模式

LVS+Keepalived負載均衡集羣之DR模式html


1、環境vim

5臺虛擬機,分別是:
後端

1臺測試機(192.168.3.111);bash

2臺DirectorServer(192.168.3.22一、192.168.3.222);服務器

2臺RealServer(192.168.3.23一、192.168.3.232);負載均衡

注:VIP本實驗設置爲:192.168.3.220ide


2、安裝配置RealServeroop

yum -y install httpd測試

chkconfig httpd on
router

echo 'R1/R2' /var/www/html/index.html    #兩臺的配置都同樣,這步爲了區別訪問的是哪臺的數據,因此分爲R1與R2,生產機上通常是統一訪問後端的數據層的數據;

service httpd start

#!/bin/bash
SNS_VIP=192.168.3.220
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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"
;;
stop)
ifconfig lo:0 down
route del $SNS_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
sysctl -p >/dev/null 2>&1
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0

複製以上內容丟到/etc/init.d/ 目錄下,命名自定義,我是命名爲:realserver.sh

chmod u+x /etc/init.d/realserver.sh

ln -s /etc/init.d/realserver.sh /etc/rc.d/rc3.d/S99realserver.sh    #設置開機自啓動

/etc/init.d/realserver.sh start


3、安裝配置DirectorServer

yum -y install gcc kernel-devel openssl-devel lrzsz vim ipvsadm

wget  http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

tar xzvf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/

make

make install

ln -s /usr/local/sbin/keepalived /sbin/

cd /etc/keepalived/

cp keepalived.conf keepalived.conf.bak

編輯keepalived.conf,內容大體以下:

! Configuration File for keepalived
global_defs {
   notification_email {
     744120325@qq.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 MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.3.220
    }
}
virtual_server 192.168.3.220 80 {
    delay_loop 6
    lb_algo wlc
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    real_server 192.168.3.231 80 {
        weight 2
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
    real_server 192.168.3.232 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

注:第二臺DS服務器(備)須要更改以上兩處的內容,一是把MASTER更改成BACKUP,二是把優先級由100改成80便可;


4、校驗及測試

4.一、ipvsadm -L -n #正常顯示VIP及RIP等內容;

4.二、ip addr list #主服務器正常顯示VIP,備服務器沒有VIP;

4.三、把主服務器關機,看備服務器會不會把服務接管過來;

相關文章
相關標籤/搜索