公司内网的一个项目开发那边自己配置的LVS,出了点问题,我重装配置了,济南和东莞各一套,东莞我用的是LVS-HA,济南我用的是LVS+keepalived,下面是具体步骤:

一、环境

东莞

VIP:172.28.29.71

realserver1:172.28.29.39(BACKUP)

realserver2:172.28.29.40(MASTER)

realserver3:172.28.29.41

济南

VIP:162.28.29.75

realserver1:162.28.29.38(MASTER)

realserver2:162.28.29.39(BACKUP)

realserver3:162.28.29.40

二、部署
A、东莞
1.安装需要的包(MASTER、BACKUP都安装安装)
yum -y install ipvsadm modcluster piranha system-config-cluster
2.撰写lvs.cfg配置文件(MASTER、BACKUP的内容是一样的)
[root@localhost ~]# vi /etc/sysconfig/ha/lvs.cf
serial_no = 18
primary = 172.28.29.40
service = lvs
backup_active = 1
backup = 172.28.29.39
heartbeat = 1
heartbeat_port = 539
keepalive = 6
deadtime = 18
network = direct
debug_level = NONE
monitor_links = 0
syncdaemon = 0
virtual 172.28.29.71 {
     active = 1
     address = 172.28.29.71 eth0:1
     vip_nmask = 255.255.255.0
     port = 81
     send = "GET / HTTP/1.0\r\n\r\n"
     expect = "HTTP"
     use_regex = 0
     load_monitor = none
     scheduler = wrr
     protocol = tcp
     timeout = 6
     reentry = 15
     quiesce_server = 0
     server 172.28.29.39 {
         address = 172.28.29.39
         active = 1
         port = 81
         weight = 1
     }
     server 172.28.29.40 {
         address = 172.28.29.40
         active = 1
         weight = 1
         port = 81
     }
     server 172.28.29.41 {
         address = 172.28.29.41
         active = 1
         port = 81
         weight = 1
     }
}
 
 
 
3.撰写realserver的脚本(三台realserver都需要)
vi /etc/init.d/lvs-realserver.sh
 
#/bin/bash
VIP=172.28.29.71
 
start(){
/sbin/ifconfig eth0:1 ${VIP} broadcast ${VIP} netmask 255.255.255.0 up
/sbin/route add -host ${VIP} dev eth0:1
/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=1
/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=2
/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=1
/sbin/sysctl -w net.ipv4.conf.all.arp_announce=2
/sbin/sysctl -p
}
 
stop(){
/sbin/ifconfig eth0:1 down
/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=0
/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=0
/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=0
/sbin/sysctl -w net.ipv4.conf.all.arp_announce=0
}
 
restart(){ 
stop
start
}
 
case $1 in
start)
     start
;;
stop) 
     stop
;; 
restart)
     restart 
;;
status)
     /sbin/ifconfig 
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
 
 
4.添加执行权限并启动realserver的脚本,加人开机启动(三台都需要)
[root@localhost server]# chmod +x  /etc/init.d/lvs-realserver.sh 
[root@localhost server]# sh /etc/init.d/lvs-realserver.sh
[root@localhost server]# echo "sh /etc/init.d/lvs-realserver.sh" >> /etc/rc.local 
 
5.打开内核的路由转发功能(MASTER和BACKUP)
[root@localhost~]# vi /etc/sysctl.conf
把net.ipv4.ip_forward = 0 给为 1
执行一下: sysctl -p
6.启动LVS集群(先MASTER.后BACKUP)
/etc/init.d/pulse start
 
7.查看运行情况
[root@localhost server]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.28.29.71:hosts2-ns wrr
  -> 172.28.29.41:hosts2-ns       Route   1      15410      1         
  -> 172.28.29.39:hosts2-ns       Route   1      15476      1         
  -> 172.28.29.40:hosts2-ns       Local   1      2          2231  
 
 
 
B、济南
1. 安装需要的包(MASTER、BACKUP)
yum -y install ipvsadm
2.安装keepalived (MASTER、BACKUP)
[root@localhost ~]# ln -sv /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux/
[root@localhost ~]# wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
[root@localhost ~]# tar -zxvf keepalived-1.1.17.tar.gz
[root@localhost ~]# cd keepalived-1.1.17
[root@localhost keepalived-1.1.17]# ./configure
[root@localhost keepalived-1.1.17]# make&&make install
 
。。。。。。

Keepalived configuration

------------------------

Keepalived version       : 1.1.17

Compiler                 : gcc

Compiler flags           : -g -O2

Extra Lib                : -lpopt -lssl -lcrypto 

Use IPVS Framework       : Yes

IPVS sync daemon support : Yes

Use VRRP Framework       : Yes

Use LinkWatch            : No

Use Debug flags          : No

 

[root@localhost keepalived-1.1.17]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

[root@localhost keepalived-1.1.17]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

[root@localhost keepalived-1.1.17]# cp /usr/local/sbin/keepalived /usr/sbin/

[root@localhost keepalived-1.1.17]# mkdir /etc/keepalived

 

vi /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

      [email protected]

   }

   notification_email_from [email protected]

   smtp_server smtp.163.com

  # smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

# VIP1

vrrp_instance VI_1 {

    state MASTER     备为:BACKUP    

    interface eth0

    lvs_sync_daemon_inteface eth0

    virtual_router_id 51

    priority 100     备为:90

    advert_int 5

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        162.28.29.75   

    }

}

 

virtual_server 162.28.29.75 81 {

    delay_loop 6             

    lb_algo rr              

    lb_kind DR        

    persistence_timeout 60        

    protocol TCP           

 

    real_server 162.28.29.38 81 {

        weight 100     

        TCP_CHECK {

        connect_timeout 10 

        nb_get_retry 3

        delay_before_retry 3

        connect_port 81

        }

    }

    real_server 162.28.29.39 81 {

        weight 100

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 81

        }

     }

    real_server 162.28.29.40 81 {

        weight 100

        TCP_CHECK {

        connect_timeout 10

        nb_get_retry 3

        delay_before_retry 3

        connect_port 81

        }

     }

}

 

 

3.撰写realserver脚本(三台一样)

[root@localhost ~]# vi /etc/init.d/lvs-realserver.sh

 

#/bin/bash

 

VIP=162.28.29.75

 

start(){

/sbin/ifconfig eth0:1 ${VIP} broadcast ${VIP} netmask 255.255.255.0 up

/sbin/route add -host ${VIP} dev eth0:1

/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=1

/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=2

/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=1

/sbin/sysctl -w net.ipv4.conf.all.arp_announce=2

/sbin/sysctl -p

}

 

stop(){

/sbin/ifconfig eth0:1 down

/sbin/sysctl -w net.ipv4.conf.lo.arp_ignore=0

/sbin/sysctl -w net.ipv4.conf.lo.arp_announce=0

/sbin/sysctl -w net.ipv4.conf.all.arp_ignore=0

/sbin/sysctl -w net.ipv4.conf.all.arp_announce=0

}

 

restart(){ 

stop

start

}

 

case $1 in

start)

     start

;;

stop) 

     stop

;; 

restart)

     restart 

;;

status)

     /sbin/ifconfig 

;;

*)

echo "Usage: $0 {start|stop|restart|status}"

exit 1

esac

 

4.添加执行权限并启动realserver的脚本,加人开机启动(三台都需要)
[root@localhost server]# chmod +x  /etc/init.d/lvs-realserver.sh 
[root@localhost server]# sh /etc/init.d/lvs-realserver.sh
[root@localhost server]# echo "sh /etc/init.d/lvs-realserver.sh" >> /etc/rc.local 
 
5.打开内核的路由转发功能(MASTER和BACKUP)
[root@localhost~]# vi /etc/sysctl.conf
把net.ipv4.ip_forward = 0 给为 1
执行一下: sysctl -p
6.启动LVS集群(先MASTER.后BACKUP)
[root@localhost ~]# /etc/init.d/keepalived start
7.查看运行情况
[root@localhost ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  162.28.29.75:81 rr persistent 60
  -> 162.28.29.40:81              Route   100    1961       0         
  -> 162.28.29.39:81              Route   100    1648       1         
  -> 162.28.29.38:81              Local   100    0          227