码迷,mamicode.com
首页 > 其他好文 > 详细

LVS-DR三种不同实现方式

时间:2019-05-27 09:18:25      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:接收   work   ati   配置环境   tar   conf   tor   UNC   netmask   

LVS-DR三种不同实现方式

1.VIP和DIP在同一网段的实现

网络拓扑图
技术图片

配置环境

配置Client网关指向route的RIP1

[root@client ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.27.20    0.0.0.0         UG    100    0        0 ens33
172.22.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens33

route上开启路由间转发

[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

VS操作

在director上将网关指向route的RIP2

[root@director ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.153.10  0.0.0.0         UG    100    0        0 ens33
192.168.153.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

执行脚本配置VS

[root@director ~]# bash lvs_dr_vs.sh start
The VS Server is Ready!

脚本内容

#!/bin/bash
vip=‘192.168.153.50‘
iface=‘lo:1‘
mask=‘255.255.255.255‘
port=‘80‘
rs1=‘192.168.153.30‘
rs2=‘192.168.153.40‘
scheduler=‘wrr‘
type=‘-g‘
rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null

case $1 in
start)
    ifconfig $iface $vip netmask $mask #broadcast $vip up
    iptables -F

    ipvsadm -A -t ${vip}:${port} -s $scheduler
    ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
    ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
    echo "The VS Server is Ready!"
    ;;
stop)
    ipvsadm -C
    ifconfig $iface down
    echo "The VS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

RS操作

分别在两台RS上将网关指向route的RIP2

[root@rs1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.153.10  0.0.0.0         UG    100    0        0 ens33
192.168.153.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

在RS端执行lvs配置脚本

[root@rs1 ~]# bash lvs_dr_rs.sh start
The httpd Server is Ready!
The RS Server is Ready!

脚本内容

#!/bin/bash
vip=192.168.153.50
mask=‘255.255.255.255‘
dev=lo:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

测试

[root@client ~]# curl 192.168.153.50
<h1>rs2</h1>
[root@client ~]# curl 192.168.153.50
<h1>rs1</h1>

2.VIP和DIP不在同一网段

网络拓扑图
技术图片

配置环境

配置Client网关指向route的RIP1

[root@client ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.27.20    0.0.0.0         UG    100    0        0 ens33
172.22.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens33

route上开启路由间转发

[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

在route上绑定一个IP用来接收RS的相应报文

[root@route ~]# ip a a 192.168.100.100/24 dev ens37

VS端操作

在director上将网关指向route的RIP2

[root@director ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.153.10  0.0.0.0         UG    100    0        0 ens33
192.168.153.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

执行脚本配置VS

[root@director ~]# bash lvs_dr_vs.sh start
The VS Server is Ready!

脚本内容

#!/bin/bash
vip=‘192.168.100.50‘
iface=‘lo:1‘
mask=‘255.255.255.255‘
port=‘80‘
rs1=‘192.168.153.30‘
rs2=‘192.168.153.40‘
scheduler=‘wrr‘
type=‘-g‘
rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null

case $1 in
start)
    ifconfig $iface $vip netmask $mask #broadcast $vip up
    iptables -F

    ipvsadm -A -t ${vip}:${port} -s $scheduler
    ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
    ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
    echo "The VS Server is Ready!"
    ;;
stop)
    ipvsadm -C
    ifconfig $iface down
    echo "The VS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

RS操作

分别在两台RS上将网关指向route的RIP2

[root@rs1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.153.10  0.0.0.0         UG    100    0        0 ens33
192.168.153.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

在RS端执行lvs配置脚本

[root@rs1 ~]# bash lvs_dr_rs.sh start
The httpd Server is Ready!
The RS Server is Ready!

脚本内容

#!/bin/bash
vip=192.168.100.50
mask=‘255.255.255.255‘
dev=lo:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

测试

[root@client ~]# curl 192.168.100.50
<h1>rs2</h1>
[root@client ~]# curl 192.168.100.50
<h1>rs1</h1>

3.VIP和DIP不在同一网段2(route使用一个ip地址)

网络拓扑图
技术图片

配置环境

配置Client网关指向route的RIP1

[root@client ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.22.27.20    0.0.0.0         UG    100    0        0 ens33
172.22.0.0      0.0.0.0         255.255.0.0     U     100    0        0 ens33

route上开启路由间转发

[root@route ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

在route上配置RIP2

[root@route ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens37
IPADDR=192.168.100.100
PREFIX=24

RS配置

执行RS配置脚本

[root@rs1 ~]# bash lvs_dr_rs.sh start
The httpd Server is Ready!
The RS Server is Ready!

脚本内容

#!/bin/bash
vip=192.168.100.50
mask=‘255.255.255.0‘
dev=ens33:1
rpm -q httpd &> /dev/null || yum -y install httpd &>/dev/null
service httpd start &> /dev/null && echo "The httpd Server is Ready!"
echo "<h1>`hostname`</h1>" > /var/www/html/index.html

case $1 in
start)
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    ifconfig $dev $vip netmask $mask #broadcast $vip up
    #route add -host $vip dev $dev
    echo "The RS Server is Ready!"
    ;;
stop)
    ifconfig $dev down
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
    echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo "The RS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

将VS的网关指RIP2

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.100.100 0.0.0.0         UG    0      0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 ens33
192.168.153.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

VS配置

为director配置地址

[root@director ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.153.20
PREFIX=24

执行vs配置脚本

[root@director ~]# bash lvs_dr_vs.sh start
The VS Server is Ready!

脚本内容

#!/bin/bash
vip=‘192.168.100.50‘
iface=‘ens33:1‘
mask=‘255.255.255.255‘
port=‘80‘
rs1=‘192.168.153.30‘
rs2=‘192.168.153.40‘
scheduler=‘wrr‘
type=‘-g‘
rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null

case $1 in
start)
    ifconfig $iface $vip netmask $mask #broadcast $vip up
    iptables -F

    ipvsadm -A -t ${vip}:${port} -s $scheduler
    ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1
    ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 1
    echo "The VS Server is Ready!"
    ;;
stop)
    ipvsadm -C
    ifconfig $iface down
    echo "The VS Server is Canceled!"
    ;;
*)
    echo "Usage: $(basename $0) start|stop"
    exit 1
    ;;
esac

测试

[root@client ~]# curl 192.168.100.50
<h1>rs2</h1>
[root@client ~]# curl 192.168.100.50
<h1>rs1</h1>
[root@client ~]# curl 192.168.100.50
<h1>rs2</h1>
[root@client ~]# curl 192.168.100.50
<h1>rs1</h1>

LVS-DR三种不同实现方式

标签:接收   work   ati   配置环境   tar   conf   tor   UNC   netmask   

原文地址:https://blog.51cto.com/11886307/2400433

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!