18.11 LVS DR模式搭建

 

准备工作:

分发器(dir):

分配公网ip 192.168.31.127

处理请求的机器(rs):

分配公网ip 192.168.31.128192.168.31.129

VIP(虚拟ip):在DR模式下的所有机器都需要绑定该ip

设置VIP31.200

假设以上所有ip均为公网ip

 

配置dir

将两台rs的网关修改为31.2

[root@hyc-01-01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

[root@hyc-01-01 ~]# ifdown ens33 && ifup ens33

成功断开设备 'ens33'

连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/9

dir上编辑脚本:

[root@hyc-01-01-01 ~]# vim /usr/local/sbin/lvs_dr.sh

#! /bin/bash

echo 1 > /proc/sys/net/ipv4/ip_forward 打开端口转发

ipv=/usr/sbin/ipvsadm 设置变量

vip=192.168.31.200

rs1=192.168.31.128

rs2=192.168.31.129

# 注意这里的网卡名称

ifdown ens33 重启网卡,使网卡释放原来的虚拟ip

ifup ens33

ifconfig ens33:2 $vip broadcast $vip netmask 255.255.255.255 up

route add -host $vip dev ens33:2

设置网卡ens33:2ipvip31.200),子网掩码为32,增加一个路由

$ipv –C 清空规则

$ipv -A -t $vip:80 -s wrr 指定算法为wrr

$ipv -a -t $vip:80 -r $rs1:80 -g -w 1 –g指定模式为DR,将发给vip80端口的请求发给rs1

$ipv -a -t $vip:80 -r $rs2:80 -g -w 1 将发给vip80端口的请求发给rs2

[root@hyc-01-01-01 ~]# sh !$ 执行脚本

sh /usr/local/sbin/lvs_dr.sh

成功断开设备 'ens33'

连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/12

 

配置两台rs

[root@hyc-01 ~]# vim /usr/local/sbin/lvs_rs.sh

#/bin/bash

vip=192.168.31.200 设置变量vip

ifdown lo 重启loopback

ifup lo

#vip绑定在loop上,是为了实现rs直接把结果返回客户端

ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up vip绑定在机器的loop

route add -host $vip lo:0

设置lo0的地址为vip,子网掩码32,增加一个路由

#以下操作为更改arp内核参数,目的是为了让rs顺利发送mac地址给客户端

#参考文档www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html

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

[root@hyc-01 ~]# sh !$

sh /usr/local/sbin/lvs_rs.sh 执行脚本

检查配置:

rs:

[root@hyc-01 ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet 192.168.31.200/32 brd 192.168.31.200 scope global lo:0

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

[root@hyc-01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         192.168.31.2    0.0.0.0         UG    100    0        0 ens33

192.168.31.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

192.168.31.200  0.0.0.0         255.255.255.255 UH    0      0        0 lo

dir上:

[root@hyc-01-01-01 ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:16:b2:84 brd ff:ff:ff:ff:ff:ff

    inet 192.168.31.127/24 brd 192.168.31.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.31.200/32 brd 192.168.31.200 scope global ens33:2

       valid_lft forever preferred_lft forever

    inet6 fe80::c4c6:8a54:2329:8bea/64 scope link

       valid_lft forever preferred_lft forever

    inet6 fe80::526f:cdd5:5651:5dcc/64 scope link tentative dadfailed

       valid_lft forever preferred_lft forever

    inet6 fe80::d46b:4589:4da1:2f34/64 scope link tentative dadfailed

       valid_lft forever preferred_lft forever

[root@hyc-01-01-01 ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         192.168.31.2    0.0.0.0         UG    100    0        0 ens33

192.168.18.0    0.0.0.0         255.255.255.0   U     100    0        0 ens37

192.168.31.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33

192.168.31.200  0.0.0.0         255.255.255.255 UH    0      0        0 ens33

 

测试:

使用浏览器访问192.168.31.200

      9.7 18.11-18.12_LVS DR

清空缓存、cookie,反复刷新

9.7 18.11-18.12_LVS DR_02

[root@hyc-01-01-01 ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

TCP  192.168.31.200:80 rr

  -> 192.168.31.128:80            Route   1      1          0        

  -> 192.168.31.129:80            Route   1      2          0     

 

18.12 keepalived LVS