拓扑图:

 

linux集群之LVS-DR_linux 

1、LVS配置脚本:

需要安装:ipvsadm-1.24-10.i386.rpm

vi  /etc/rc.d/init.d/lvsdr
#!/bin/bash
vip=192.168.1.249
web1=192.168.1.251
web2=192.168.1.252
./etc/rc.d/init.d/functions
case "$1" in
  start)
    echo "start lvs-dr"
    echo "1" > /proc/sys/net/ipv4/ip_forward
         ifconfig eth0:0 ${vip} broadcast ${vip} netmask 255.255.255.255 up
         route add -host ${vip} dev eth0:0
         ipvsadm -C
         ipvsadm -A -t ${vip}:80 -s rr
         ipvsadm -a -t ${vip}:80 -r ${web1}:80 -g
         ipvsadm -a -t ${vip}:80 -r ${web2}:80 -g
         ipvsadm
  ;;
  stop)
    echo "stop lvs-dr"
    echo "0" >/proc/sys/net/ipv4/ip_forward
         ipvsadm -C
         ifconfig eth0:0 down
  ;;

 *)
    echo "Usage:$0 {start|stop}"
         exit 1
esac

 启动:

[root@linux-1 lvs]# service lvsdr start
start lvs-dr
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.249:http rr
  -> 192.168.1.252:http           Route   1      0          0        
  -> 192.168.1.251:http           Route   1      0          0

二、web1配置脚本

vi /etc/rc.d/init.d/lvsweb
#!/bin/bash
vip=192.168.1.249
./etc/rc.d/init.d/functions
case "$1" in

   start)
      echo "start lvs-web1"
          ifconfig lo:0 ${vip} broadcast ${vip} netmask 255.255.255.255 up
          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)
      echo "stop lvs-web1"
          ifconfig lo:0 down
          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

启动脚本:

[root@linux-2 local]# service lvsweb start
start lvs-web1

在web1上/var/www/html 创建一个文件为index.html

cat /var/www/html/index.html
the is web1

三、web2配置脚本

vi  /etc/rc.d/init.d/lvsweb
#!/bin/bash
vip=192.168.1.249
./etc/rc.d/init.d/functions
case "$1" in

   start)
      echo "start lvs-web2"
          ifconfig lo:0 ${vip} broadcast ${vip} netmask 255.255.255.255 up
          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)
      echo "stop lvs-web2"
          ifconfig lo:0 down
          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

启动脚本:

[root@linux-3 ~]# service lvsweb start
start lvs-web2

在web2上/var/www/html 创建一个文件为index.html

cat /var/www/html/index.html
the is web2

#####################到此简单的LVS-DR集群成功##################

 四,监控LVS (在LVS服务器上安装)

        1、安装rrdtool工具

        wget http://down1.chinaunix.net/distfiles/rrdtool-1.2.30.tar.gz

        tar zxvf rrdtool-1.2.30.tar.gz

       cd rrdtool-1.2.30

      ./configure --prefix=/usr/local/rrdtool

        make&&make install

        echo “/usr/local/rrdtool/lib” >>/etc/ld.so.conf

       ldconf -v

      2、安装lvs-rrd

        wget http://tepedino.org/lvs-rrd/lvs-rrd-v0.7.tar.gz

       tar tar zxvf lvs-rrd-v0.7.tar.gz

       mv lvs-rrd-v0.7 /var/www/html/lvs-rrd

      3、配置lvs-rrd

        cd  /var/www/html/lvs-rrd

         vi    lvs.rrd.update (只需要配置指定的路径即可)

        RRDTOOL="/usr/local/rrdtool/bin/rrdtool"
        IPVSADM="/sbin/ipvsadm"
        WORKDIR="/var/www/html/lvs-rrd"

       vi graph-lvs.sh

       WORKDIR="/var/www/html/lvs-rrd"
       RRDTOOL="/usr/local/rrdtool/bin/rrdtool"
       GRAPHS="$WORKDIR/graphs"
       WEBPATH="/lvs-rrd/graphs"

     4、设置权限:

          chown -R apache.apache graphs/

     5 、使用crontab 更新

          crontab -e

          * * * * * /var/www/html/lvs-rrd/lvs.rrd.update 2> /dev/null > /dev/null

#######################到此监控安装完毕##############################

测试结果:

http://192.168.1.250/lvs-rrd/

linux集群之LVS-DR_lvs-dr_02

         

 

linux集群之LVS-DR_linux集群_03

 

linux集群之LVS-DR_linux集群_04

 

linux集群之LVS-DR_linux_05 

linux集群之LVS-DR_linux集群之lvs-dr_06 

linux集群之LVS-DR_lvs集群_07