Lvs+Keepalived構建雙機高可用

應用web

IP地址bash

網關服務器

Vip網絡

公網地址架構

Virtual IPtcp



10.0.1.100ide

119.188.13x.x(網通nat)oop

218.98.3x.20x(電信nat)spa

Lvs(master)router

10.0.1.101 eth0

10.0.1.254



Lvslave

10.0.1.102 eth0

10.0.1.254



Real-server

10.0.1.103 eth0

10.0.1.254

10.0.1.100  lo:0


Real-server

10.0.1.104 eth0

10.0.1.254

10.0.1.100  lo:0







Virtual IP



10.0.1.200

119.188.13x.4(網通nat)

218.98.3x.201(電信nat)

Real-server

10.0.1.105 eth0

10.0.1.254

10.0.1.200  lo:0


Real-server

10.0.1.106 eth0

10.0.1.254

10.0.1.200  lo:0


1、安裝LVS

rpm -ivh ipvsadm-1.24-13.el5.x86_64

2ipvsadm檢查安裝

201207930.png

3lsmod |grep ip_vs檢查是否加裝ip_vs模塊

201235143.png

4realserver真實節點服務器配置(10三、104配置相同)

vi/etc/init.d/lvsrsdr

#!/bin/bash

#description : start realserver DR

VIP=10.0.1.100

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

case "$1" in

start)

echo "start LVS of RealServer DR"

/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

;;

Stop)

/sbin/ifconfig lo:0 down

echo "close LVS of RealServer DR"

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

腳本可執行模式

chmod +x /etc/init.d/lvsrsdr

啓動腳本(以服務方式啓動)

service lvsrsdr start

5realserver真實節點服務器配置(10五、106配置相同不一樣於10三、104vip地址不一樣)

vi/etc/init.d/lvsrsdr

#!/bin/bash

#description : start realserver DR

VIP=10.0.1.200

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

case "$1" in

start)

echo "start LVS of RealServer DR"

/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

;;

Stop)

/sbin/ifconfig lo:0 down

echo "close LVS of RealServer DR"

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

LVS高可用配置

1、下載安裝(Master和Backup都必須安裝Keepalive)

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
tar zxvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure
make

make install

二、將Keepalived以服務啓動(Master和Backup同時配置)

cp /usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived/etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf/etc/keepalived/

cp /usr/local/sbin/keepalived /usr/sbin/

chkconfig --add keepalived

service keepalived start|stop 

3、配置MASTER

cp /etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.bak
vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

notification_email {

acassen@firewall.loc

failover@firewall.loc

sysadmin@firewall.loc

}

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 {

10.0.1.100

10.0.1.200

}

}

 

virtual_server 10.0.1.100 80 {

delay_loop 6

lb_algo wlc

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

 

real_server10.0.1.103 80 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

 

real_server10.0.1.104 80 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

 

}

virtual_server 10.0.1.200 80 {

delay_loop 6

lb_algo wlc

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

 

real_server10.0.1.105 80 {

weight 1

TCP_CHECK {

 

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

 

real_server10.0.1.106 80 {

weight 1

TCP_CHECK {

 

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

 

}

 

 

3、配置BACKUP          

vrrp_instance VI_1 {
state BACKUP  
    interfaceeth0           

    virtual_router_id 51
 priority 99

 

 

總結:

 

一個公網IP映射給內部vip(既vip和web集羣爲一個網段),VS/DR 經過改寫請求報文的MAC地址(改成選出服務器的MAC地址),再將修改後的數據幀發送給選出的web服務器。由於數據幀的MAC地址是選出的服務器,因此服務器確定能夠收到這個數據幀,從中能夠得到該IP報文。當服務器發現報文的目標地址VIP是在本地的網絡設備上(lo:0口),服務器處理這個報文,而後根據路由表將響應報文直接返回給客戶,既應用服務器執行相應操做後,經過lo:0口(也就是vip的地址已經經過nat可出訪外網),將結果返回給客戶,這也就是爲何整個的架構中,僅需vip有公網IP,而後經過防火牆將公網IP作映射到私網vip便可.

 

 

客戶端抓包:
客戶端IP在通訊過程一直是vip(實際是vip中的一臺realserver!),因此這臺realserver依舊經過vip的地址將請求轉給客戶端!若是vip是公網IP,那realserver也會經過這個公網IP將請求發給客戶!


tcpdump -i eth0 tcp port 80

tcpdump |grep http


web服務器抓包:

客戶端IP一直與vip通訊,realserver也是將本身當成vip來處理數據。

DR模式,注意DR模式不支持端口重定向,VIP監聽的port必須與rs服務監聽的port一致

相關文章
相關標籤/搜索