LVS nat模型+dr模型

nat模型

  

 

 

在 rs1 和 rs2  安装httpd  并配置测试页,启动

[root@rs1 ~]# yum install httpd -y
[root@rs1 ~]# echo "this is rs1" > /var/www/html/index.html
[root@rs1 ~]# systemctl restart httpd
[root@rs2
~]# yum install httpd -y
[root@rs2 ~]# echo "this is rs2" > /var/www/html/index.html
[root@rs2 ~]# systemctl restart httpd

 

将rs1 和 rs2 的网关指向 lvs 的DIP

[root@rs1 ~]# route add default gw 192.168.37.22
[root@rs2 ~]# route add default gw 192.168.37.22

 

在LVS服务器上开启核心转发功能

[root@lam ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1

[root@lam ~]# sysctl -p
net.ipv4.ip_forward = 1         #:使配置生效

 

在LVS服务器上安装ipvsadm

[root@lam ~]# yum install ipvsadm -y

 

在LVS服务器上配置规则

[root@lam ~]# ipvsadm -A -t 172.16.14.100:80 -s rr
[root@lam ~]# ipvsadm -a -t 172.16.14.100:80 -r 192.168.37.23:80 -m
[root@lam ~]# ipvsadm -a -t 172.16.14.100:80 -r 192.168.37.24:80 -m

 

在客户端测试

[root@master-mariadb ~]# curl 172.16.14.100
this is rs2
[root@master-mariadb ~]# curl 172.16.14.100
this is rs1
[root@master-mariadb ~]# curl 172.16.14.100
this is rs2
[root@master-mariadb ~]# curl 172.16.14.100
this is rs1
[root@master-mariadb ~]# curl 172.16.14.100

 

 

DR模型(rip和VIP在同一网段)

 

 

 在rs1和rs2上安装httpd服务并启动

[root@rs1 ~]# yum install httpd -y
[root@rs1 ~]# echo "this is 24" > /var/www/html/index.html
[root@rs1 ~]# systemctl start httpd

[root@rs2 ~]# yum install httpd -y
[root@rs2 ~]# echo "this is 24" > /var/www/html/index.html
[root@rs2 ~]# systemctl start httpd

 

在lvs 服务器安装lvsadm服务

[root@lvs ~]# yum install ipvsadm -y

 

配置router服务器

[root@router ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1     #:打开核心转发

修改网卡 一个桥接,一个内网  

 

配置客户端使其网关指向r1

[root@client ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="15caec5a-6740-4a81-8c72-5fbcb6fb9d25"
BOOTPROTO=static
IPADDR=172.16.14.11
NETMASK=255.255.0.0
GATEWAY=172.16.14.22
TYPE=Ethernet

 

配置lvs服务器使其网关指向r2,并添加一个DIP

[root@lvs ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
BOOTPROTO=static
IPADDR=192.168.37.23
NETMASK=255.255.255.0
GATEWAY=192.168.37.22
[root@lvs ~]# ip a a 192.168.37.100/24 dev eth0:0

 

配置rs1和rs2的网关和接口并修改参数

[root@rs1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
BOOTPROTO=static
IPADDR=192.168.37.24
NETMASK=255.255.255.0
GATEWAY=192.168.37.22

[root@rs1 ~]# ifconfig lo:1 192.168.37.100 netmask 255.255.255.255
[root@rs1 ~]# route add -host 192.168.37.100 dev lo:1
[root@rs1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
[root@rs1 ~]# sysctl -p
[root@rs2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 NAME="eth0" DEVICE="eth0" ONBOOT=yes NETBOOT=yes BOOTPROTO=static IPADDR=192.168.37.25 NETMASK=255.255.255.0 GATEWAY=192.168.37.22 [root@rs2 ~]# ifconfig lo:1 192.168.37.100 netmask 255.255.255.255 [root@rs2 ~]# route add -host 192.168.37.100 dev lo:1
[root@rs2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
[root@rs2 ~]# sysctl -p

 

在ipvs服务器配置规则

[root@lvs ~]# ipvsadm -A -t 192.168.37.100:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.37.100:80 -r 192.168.37.24 -g
[root@lvs ~]# ipvsadm -a -t 192.168.37.100:80 -r 192.168.37.25 -g

 

测试

[root@client ~]# curl 192.168.37.100
this is 25
[root@client ~]# curl 192.168.37.100
this is 24
[root@client ~]# curl 192.168.37.100
this is 25
[root@client ~]# curl 192.168.37.100

 

在各rs上修改参数也可用脚本完成

[root@rs1 ~]# vim lvs_dr_rs.sh 
#!/bin/bash
#Author:wangxiaochun
#Date:2017-08-13
vip=10.0.0.100
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"

 

 

dr模型(vip与rip不在同一网段)

现在rs1和rs2上面安装httpd并准备测试页

[root@rs1 ~]# yum install httpd -y
[root@rs1 ~]# echo "this is r1" > /var/www/html/index.html
[root@rs1 ~]# systemctl start httpd

[root@rs2 ~]# yum install httpd -y
[root@rs2 ~]# echo "this is r2" > /var/www/html/index.html
[root@rs2 ~]# systemctl start httpd

 

在lvs上安装ipvsadm

[root@lvs ~]# yum install ipvsadm -y

 

配置router服务器(在router上添加一块外网网卡,并添加两个内网地址)开启路由转发

[root@router network-scripts]# cp ifcfg-ens33 ifcfg-eth1
[root@router network-scripts]# vim ifcfg-eth1
NAME="eth1"
DEVICE="eth1"
ONBOOT=yes
NETBOOT=yes
BOOTPROTO=static
IPADDR=172.16.14.100
NETMASK=255.255.0.0
TYPE=Ethernet

[root@router network-scripts]# systemctl restart network
[root@router network-scripts]# ip a a 10.0.0.10/8 dev eth0:1 #:这个是临时加的,如果想永久有效,写在配置文件中

[root@router ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@router ~]# sysctl -p

 

配置client服务器网关指向路由

[root@client ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
BOOTPROTO=static
IPADDR=172.16.14.200
NETMASK=255.255.0.0
GATEWAY=172.16.14.100
TYPE=Ethernet

 

配置rs1和rs2的网关指向路由器

[root@rs1 ~]# vinet
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="95b18552-6cc5-48c5-b29a-91ed73675c50"
BOOTPROTO=static
IPADDR=192.168.37.24
NETMASK=255.255.255.0
GATEWAY=192.168.37.22
[root@rs1 ~]# systemctl restart network

[root@rs2 ~]# vinet
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="cdd70dbc-1363-45da-9c7a-0ae369f9aac3"
BOOTPROTO=static
IPADDR=192.168.37.25
NETMASK=255.255.255.0
GATEWAY=192.168.37.22
[root@rs2 ~]# systemctl restart network

 

配置lvs服务器的网关指向路由并添加一个vip

[root@lvs ~]# vinet
NAME="eth0"
DEVICE="eth0"
ONBOOT=yes
NETBOOT=yes
UUID="15caec5a-6740-4a81-8c72-5fbcb6fb9d25"
BOOTPROTO=static
IPADDR=192.168.37.23
NETMASK=255.255.255.0
GATEWAY=192.168.37.22
IPADDR1=10.0.0.100   #vip的地址
NETMASK=255.0.0.0
TYPE=Ethernet

 

在rs1和rs2上分别添加vip 并关闭arp通告

[root@rs1 ~]# ifconfig lo:1 10.0.0.100 netmask 255.255.255.255
[root@rs1 ~]# route add -host 10.0.0.100 dev lo:1
[root@rs1 ~]# vim /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
[root@rs1 ~]# sysctl -p


[root@rs2 ~]# ifconfig lo:1 10.0.0.100 netmask 255.255.255.255
[root@rs2 ~]# route add -host 10.0.0.100 dev lo:1
[root@rs2 ~]# vim /etc/sysctl.conf 
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2
[root@rs2 ~]# sysctl -p

 

在lvs服务器配置规则

[root@lvs ~]# ipvsadm -A -t 10.0.0.100:80 -s rr
[root@lvs ~]# ipvsadm -a -t 10.0.0.100:80 -r 192.168.37.24 -g
[root@lvs ~]# ipvsadm -a -t 10.0.0.100:80 -r 192.168.37.25 -g

 

在client服务器测试

[root@client ~]# curl 10.0.0.100
this is r2
[root@client ~]# curl 10.0.0.100
this is r1
[root@client ~]# curl 10.0.0.100
this is r2

 

 

 
 

 

posted @ 2019-08-05 15:07  Linus小跟班  阅读(301)  评论(0编辑  收藏  举报