LVS/DR 配置

准备3台机器:

dir(192.168.220.141)
rs1(192.168. 220.143)
rs2(192.168.220.144)

vip(192.168.220.110)


Director 上 vim /usr/local/sbin/lvs_dr.sh //增加
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.220.110
rs1=192.168.220.143
rs2=192.168.220.144
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr 
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1


两台rs上:vim /usr/local/sbin/lvs_dr_rs.sh
#! /bin/bash
vip=192.168.220.110
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up 
route add -host $vip 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


然后director上执行: bash /usr/local/sbin/lvs_dr.sh
两台rs上执行: bash /usr/local/sbin/lvs_dr_rs.sh
Windows下浏览器测试访问


结合keepalived

再准备一台机器作为备用dir(192.168.220.129)

在主dir上

vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {
    state MASTER   #备用服务器上为 BACKUP
    interface eth0
    virtual_router_id 51
    priority 100  #备用服务器上为90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.220.110
    }
}
virtual_server 192.168.220.110 80 {
    delay_loop 6                  #(每隔10秒查询realserver状态)
    lb_algo wlc                  #(lvs 算法)
    lb_kind DR                  #(Direct Route)
    persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver)
    protocol TCP                #(用TCP协议检查realserver状态)

    real_server 192.168.220.143 80 {
        weight 100               #(权重)
        TCP_CHECK {
        connect_timeout 10       #(10秒无响应超时)
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
real_server 192.168.220.144 80 {
        weight 100
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

scp /etc/keepalived/keepalived.conf 192.168.220.129://etc/keepalived/keepalived.conf

在备用dir上

vim /etc/keepalived/keepalived.conf

修改以下几处:

state MASTER  -> state BACKUP
priority 100 -> priority 90

配置完keepalived后,需要开启端口转发(主从都要做):
echo 1 > /proc/sys/net/ipv4/ip_forward
然后,两个rs上执行 /usr/local/sbin/lvs_dr_rs.sh 脚本
最后,两个director上启动keepalived服务(先主后从):
/etc/init.d/keepalived start
另外,需要注意的是,启动keepalived服务会自动生成vip和ipvsadm规则,不需要再去执行上面提到的/usr/local/sbin/lvs_dr.sh 脚本。