安裝詳解:http://www.2cto.com/os/201601/487689.html。
lvs有三種模式dr,nat,tun。這裡先介紹dr模式。
#!/bin/sh # lvs-dr.sh # # lvs的dr模式LVS server腳本 # # # vip和rip必須在同一個網段。VS/DR通過改寫請求報文的MAC地址,將請求的包發送到realserver上, # 不改變包的源和目標的IP地址,然後realserver直接回復客戶端,不再經過LVS調度器,這樣大大的減輕了LVS的負擔。 # set the vip and port VIP=192.168.142.211 VPORT1=80 # set the rip and port # web RIP1=192.168.142.130 RIP2=192.168.142.131 # port RPORT1=80 Usage (){ echo "Usage:`basename $0` (start|stop|status) " exit 1 } if [ $# -ne 1 ];then Usage fi case $1 in start) echo "start LVS of DirectorServer" echo 1 > /proc/sys/net/ipv4/ip_forward # set the vip /sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev eth0:1 # clear ipvs table /sbin/ipvsadm -C # add lvs vip and port /sbin/ipvsadm -A -t $VIP:$VPORT1 -s rr # add rip and port /sbin/ipvsadm -a -t $VIP:$VPORT1 -r $RIP1:$RPORT1 -g -w 1 /sbin/ipvsadm -a -t $VIP:$VPORT1 -r $RIP2:$RPORT1 -g -w 1 /sbin/ipvsadm -L -n ;; stop) echo "close LVS DirectorServer" /sbin/ipvsadm -C /sbin/ifconfig eth0:1 down ;; status) /sbin/ipvsadm -L -n ;; *) Usage esac啟動
# cd /usr/local/lvs
# vi realServer.sh
#!/bin/sh # realServer.sh # # lvs的dr模式RIP server腳本 # VIP=192.168.142.211 #vip's broadcast BROADCAST=192.168.142.255 Usage () { echo "Usage:`basename $0` (start|stop)" exit 1 } if [ $# -ne 1 ];then Usage fi case $1 in start) echo "reparing for Real Server" echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce echo "1" >/proc/sys/net/ipv4/conf/eth0/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/eth0/arp_announce /sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $BROADCAST up /sbin/route add -host $VIP dev lo:0 ;; stop) /sbin/ifconfig lo:0 down echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "0" >/proc/sys/net/ipv4/conf/eth0/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/eth0/arp_announce echo "stop Real Server" ;; *) Usage esac
5)刷新頁面將會看到不同的內同。