LVS-DR三种不同实现方式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS-DR三种不同实现方式相关的知识,希望对你有一定的参考价值。

LVS-DR三种不同实现方式

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

网络拓扑图
技术图片

配置环境

配置Client网关指向route的RIP1

[[email protected] ~]# 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上开启路由间转发

[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

VS操作

在director上将网关指向route的RIP2

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# 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配置脚本

[[email protected] ~]# 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

测试

[[email protected] ~]# curl 192.168.153.50
<h1>rs2</h1>
[[email protected] ~]# curl 192.168.153.50
<h1>rs1</h1>

2.VIP和DIP不在同一网段

网络拓扑图
技术图片

配置环境

配置Client网关指向route的RIP1

[[email protected] ~]# 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上开启路由间转发

[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

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

[[email protected] ~]# ip a a 192.168.100.100/24 dev ens37

VS端操作

在director上将网关指向route的RIP2

[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# 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配置脚本

[[email protected] ~]# 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

测试

[[email protected] ~]# curl 192.168.100.50
<h1>rs2</h1>
[[email protected] ~]# curl 192.168.100.50
<h1>rs1</h1>

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

网络拓扑图
技术图片

配置环境

配置Client网关指向route的RIP1

[[email protected] ~]# 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上开启路由间转发

[[email protected] ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

在route上配置RIP2

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens37
IPADDR=192.168.100.100
PREFIX=24

RS配置

执行RS配置脚本

[[email protected] ~]# 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配置地址

[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
IPADDR=192.168.153.20
PREFIX=24

执行vs配置脚本

[[email protected] ~]# 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

测试

[[email protected] ~]# curl 192.168.100.50
<h1>rs2</h1>
[[email protected] ~]# curl 192.168.100.50
<h1>rs1</h1>
[[email protected] ~]# curl 192.168.100.50
<h1>rs2</h1>
[[email protected] ~]# curl 192.168.100.50
<h1>rs1</h1>

以上是关于LVS-DR三种不同实现方式的主要内容,如果未能解决你的问题,请参考以下文章

常用的几个JQuery代码片段

基于lvs-dr模型的discuz负载均衡实现

分布式锁三种解决方案

12个用得着的 JQuery 代码片段

LVS-DR实现基于LAMP的负载均衡

LVS-DR+keepalive做高可用,实现负载均衡(主备模式)