# 拓扑如下所示

# 节点分布情况

LVS-dr-master

    eth0: 192.168.146.141

  

LVS-dr-slave

    eth0: 192.168.146.142

nginx1:

    eth0: 192.168.146.139

nginx2:

    eth0: 192.168.146.140

tomcat1:

    eth0: 192.168.146.138 启用了4个tomcat

VIP: 192.168.146.200

# 具体配置

### lvs master

### 1、安装ipvsadm、keepalived

yum install -y keepalived ipvsadm

### 2、使用keepalived来管理lvs

脚本:

global_defs{router_id master_210} vrrp_instance aiyou {state MASTERinterface eth0virtual_router_id 100    #这个数值 master和slave必须统一priority 151     #这个数值决定哪台服务器是master advert_int 1authentication {        auth_type PASS        auth_pass 123456        }virtual_ipaddress {        192.168.146.200        }}virtual_server 192.168.146.200 80 {            delay_loop 6            lb_algo wrr            lb_kind DR#            persistence_timeout 50            protocol TCPreal_server 192.168.146.139 80 {                weight 1                TCP_CHECK {                    connect_timeout 3                    nb_get_retry 3                    delay_before_retry 3                    connect_port 80                }            }real_server 192.168.146.140 80 {                weight 1                TCP_CHECK {                    connect_timeout 3                    nb_get_retry 3                    delay_before_retry 3                    connect_port 80                }            }}

### lvs slave

### 1、安装ipvsadm、keepalived

yum install -y keepalived ipvsadm

### 2、使用keepalived来管理lvs

global_defs{router_id slave_211} vrrp_instance aiyou {state MASTERinterface eth0virtual_router_id 100    #这个数值 master和slave必须统一priority 150     #这个数值决定哪台服务器是master 这里我们比master数值低,所以角色是backup,advert_int 1authentication {        auth_type PASS        auth_pass 123456        }virtual_ipaddress {        192.168.146.200        }}virtual_server 192.168.146.200 80 {            delay_loop 6            lb_algo wrr            lb_kind DR#            persistence_timeout 50            protocol TCPreal_server 192.168.146.139 80 {                weight 1                TCP_CHECK {                    connect_timeout 3                    nb_get_retry 3                    delay_before_retry 3                    connect_port 80                }            }real_server 192.168.146.140 80 {                weight 1                TCP_CHECK {                    connect_timeout 3                    nb_get_retry 3                    delay_before_retry 3                    connect_port 80                }            }}

### nginx1

### nginx上要跑个脚本

#!/bin/bash  #   # Script to start LVS DR real server.   # description: LVS DR real server   #   .  /etc/rc.d/init.d/functionsVIP=192.168.146.200   #这里根据需要改成自己的VIP地址host=`/bin/hostname`case "$1" in  start)          # Start LVS-DR real server on this machine.           /sbin/ifconfig lo down           /sbin/ifconfig lo up           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        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up          /sbin/route add -host $VIP dev lo:0;;  stop)        # Stop LVS-DR real server loopback device(s).          /sbin/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;;  status)        # Status of LVS-DR real server.          islothere=`/sbin/ifconfig lo:0 | grep $VIP`           isrothere=`netstat -rn | grep "lo:0" | grep $VIP`           if [ ! "$islothere" -o ! "isrothere" ];then               # Either the route or the lo:0 device               # not found.               echo "LVS-DR real server Stopped."           else               echo "LVS-DR real server Running."           fi   ;;   *)               # Invalid entry.               echo "$0: Usage: $0 {start|status|stop}"               exit 1   ;;   esac

### nginx2

### nginx上要跑个脚本

#!/bin/bash  #   # Script to start LVS DR real server.   # description: LVS DR real server   #   .  /etc/rc.d/init.d/functionsVIP=192.168.146.200   #这里根据需要改成自己的VIP地址host=`/bin/hostname`case "$1" in  start)          # Start LVS-DR real server on this machine.           /sbin/ifconfig lo down           /sbin/ifconfig lo up           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        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up          /sbin/route add -host $VIP dev lo:0;;  stop)        # Stop LVS-DR real server loopback device(s).          /sbin/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;;  status)        # Status of LVS-DR real server.          islothere=`/sbin/ifconfig lo:0 | grep $VIP`           isrothere=`netstat -rn | grep "lo:0" | grep $VIP`           if [ ! "$islothere" -o ! "isrothere" ];then               # Either the route or the lo:0 device               # not found.               echo "LVS-DR real server Stopped."           else               echo "LVS-DR real server Running."           fi   ;;   *)               # Invalid entry.               echo "$0: Usage: $0 {start|status|stop}"               exit 1   ;;   esac

### 后端tomcat安装省略

采用一机多实例模式(非多虚拟主机)

### 测试

### master上验证

[root@lvs1 ~]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  192.168.146.200:80 wrr  -> 192.168.146.139:80           Route   1      0          0           -> 192.168.146.140:80           Route   1      0          0         [root@lvs1 ~]#

### slave上验证

[root@lvs2 ~]# ipvsadm -lnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  192.168.146.200:80 wrr  -> 192.168.146.139:80           Route   1      0          0           -> 192.168.146.140:80           Route   1      0          0         [root@lvs2 ~]#

参考文章: