(一)DR****模式的搭建实战
配置vip网卡
-
在编辑虚拟网络中创建桥接模式的网卡,并且桥接到有网的适配器上
-
在 vmware 的虚拟主机资源管理器找到虚拟主机,右键菜单,设置
-
添加新的网卡,自定义为刚才创建的桥接模式网卡
-
此时在虚拟主机中使用 ifconfig 无法找到新的网卡
-
ip a 能够查看到新的 ens36 网卡,没有路由
-
编辑网卡配置
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens36"
UUID="fdbcb12c-33b6-4d7d-93bb-3b5380c4fb30"
DEVICE="ens36"
ONBOOT="yes"
IPADDR=192.168.10.100 -
重启 network 服务
[root@ds01 ~]# systemctl restart network
添加规则
[root@ds01 ~]# # 配置ipvs规则
[root@ds01 ~]# ipvsadm -A -t 192.168.10.100:80 -s rr
[root@ds01 ~]# 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.10.100:80 rr
[root@ds01 ~]# # 添加rs web01 web02 添加规则
[root@ds01 ~]# ipvsadm -a -t 192.168.10.100:80 -r
10.1.1.200:80 -m
[root@ds01 ~]# ipvsadm -a -t 192.168.10.100:80 -r
10.1.1.201:80 -m
[root@ds01 ~]# 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.10.100:80 rr
-> 10.1.1.200:80 Masq 1 0
0
-> 10.1.1.201:80 Masq 1 0
0
ip****转发
[root@ds01 ~]# vim /etc/sysctl.conf
##########################################
net.ipv4.ip_forward=1
#############################################
[root@ds01 ~]# sysctl -p
net.ipv4.ip_forward = 1
临时修改web01和web02的网关,网关必须指向dip**(调度服务器的对内的ip****)**
[root@web01 ~]# route del default
[root@web01 ~]# route add default gw 10.1.1.100
[root@web01 ~]# # 临时修改网关
# 这也要求了rs ip和 dip要在同一个网段,因为dip是要作为网关存在的
lvs-nat 模式的优点配置简单,缺点是请求和响应都必须经过 ds ,容易称为
性能瓶颈
希望有这样的模式,请求的时候使用 input 链进行负载均衡,响应的时候就
不要经过 ds ,直接由 rs 响应给客户端
在 nat 模式的时候,请求 vip ,接收 vip 的响应
构想 请求 vip ,接受 rip 响应,这是不允许 lvs-dr 模式
NAT****脚本
# ds脚本
#!/bin/bash
#配置网卡
echo TYPE="Ethernet" >> /etc/sysconfig/networkscripts/ifcfg-ens36
echo BOOTPROTO="none" >> /etc/sysconfig/networkscripts/ifcfg-ens36
read -p "router name:" router_name
echo NAME='"$rount_name"' >> /etc/sysconfig/networkscripts/ifcfg-ens36
uuidkey=$( uuidgen )
echo UUID='"$uuidkey"' >> /etc/sysconfig/networkscripts/ifcfg-ens36 >> /etc/sysconfig/networkscripts/ifcfg-ens36
echo DEVICE='"$rount_name"' >> /etc/sysconfig/networkscripts/ifcfg-ens36
echo ONBOOT="yes" >> /etc/sysconfig/network-scripts/ifcfgens36
echo IPADDR=192.168.10.100 >> /etc/sysconfig/networkscripts/ifcfg-ens36
systemctl restart network
#安装ipvsadm
yum list installed|grep ipvsadm
if[ $? -ne 0 ];then
yum -y install ipvsadm
fi
#配置规则
read -p "vip:" vip
read -p "port:" port
read -p "rule:" s
ipvsadm -A -t $vip:$port -s $s
# ip forward
echo "net.ipv4.ip_forward=1" >/etc/sysctl.conf
sysctl -p
# rs脚本
#!/bin/bash
read -p "dip:" dip
# 设置网关
route del default
route add defualt gw $dip
DR模式
-
性能更优,回路不再经过 ds
2.ds 和 rs 为了保证用户响应,都要求配置统一的 vip -
由于 rs 是直接响应 client ,网关一定不能设置为 ds 的 dip
-
对 rs 的 vip 进行抑制,让 ds 的 vip 接收请求, rs 的 vip 不接受请求
5.rs 的 vip 绑定点 lo 回路网卡上 -
在 ds 的 ens33 上挂一个 vip 10.1.1.102
[root@dr03 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu
1500
inet 10.1.1.101 netmask 255.255.255.0 broadcast
10.1.1.255
inet6 fe80::20c:29ff:fe38:97fb prefixlen 64
scopeid 0x20<link>
ether 00:0c:29:38:97:fb txqueuelen 1000
(Ethernet)
RX packets 153 bytes 16165 (15.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 144 bytes 15826 (15.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0
collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 64 bytes 5568 (5.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 64 bytes 5568 (5.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0
collisions 0
[root@dr03 ~]# ifconfig ens33:0 10.1.1.102 broadcat
10.1.1.102 netmask 255.255.255.255 up
broadcat: 未知的主机
ifconfig: `--help' gives usage information.
[root@dr03 ~]# ifconfig ens33:0 10.1.1.102 broadcast
10.1.1.102 netmask 255.255.255.255 up
[root@dr03 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu
1500
inet 10.1.1.101 netmask 255.255.255.0 broadcast
10.1.1.255
inet6 fe80::20c:29ff:fe38:97fb prefixlen 64
scopeid 0x20<link>
ether 00:0c:29:38:97:fb txqueuelen 1000
(Ethernet)
RX packets 399 bytes 38090 (37.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 300 bytes 32758 (31.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0
collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu
1500
inet 10.1.1.102 netmask 255.255.255.255
broadcast 10.1.1.102
ether 00:0c:29:38:97:fb txqueuelen 1000
(Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 64 bytes 5568 (5.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 64 bytes 5568 (5.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0
collisions 0
[root@dr03 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue
state UNKNOWN group default qlen 1000
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 group default qlen 1000
link/ether 00:0c:29:38:97:fb brd ff:ff:ff:ff:ff:ff
inet 10.1.1.101/24 brd 10.1.1.255 scope global ens33
valid_lft forever preferred_lft forever
inet 10.1.1.102/32 brd 10.1.1.102 scope global ens33:0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe38:97fb/64 scope link
valid_lft forever preferred_lft forever
[root@dr03 ~]# route add -host 10.1.1.102 dev ens33:010.1.1.101 dip
10.1.1.102 vip 在rs上的vip和这个vip相同
-
设置规范
安装ipvsadm
yum -y install ipvsadm
$ 设置规则
ipvsadm -A -t 10.1.1.102:80 -s rr
ipvsadm -a -t 10.1.1.102:80 -r 10.1.1.200 -g
ipvsadm -a -t 10.1.1.102:80 -r 10.1.1.201 -grs不在需要指定端口,dr不支持端口映射,vip上是80端口,最终就是80端
口
-m nat -g gateway
(二)设置rs主机
-
在 lo 接口上绑定 vip
[root@web01 ~]# ifconfig lo:0 10.1.1.102 broadcast
10.1.1.102 netmask 255.255.255.255 up -
设置主机路由
[root@web01 ~]# route add -host 10.1.1.102 dev lo:0
3.抑制rs接收请求
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
-
生成脚本,对 web02 使用
ifconfig lo:0 10.1.1.102 broadcast 10.1.1.102 netmask
255.255.255.255 up
route add -host 10.1.1.102 dev 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 -
测试使用,查看状态
[root@dr03 ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts
OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 10.1.1.102:80 1 6
0 360 0
-> 10.1.1.200:80 0 0
0 0 0
-> 10.1.1.201:80 1 6
0 360 0
[root@dr03 ~]#
(三)dr****模式的脚本
#!/bin/bash
#在ens33上挂载一个ip地址
read -p "vip:" vip
read -p "mac:" mac
read -p "num" num
ifconfig $mac:$num $vip broadcast $vip netmask
255.255.255.255
# 主机路由
route add -host $vip dev $mac:$num
#安装ipvsadm
yum list installed|grep ipvsadm
if [ $? -ne 0 ] ; then
yum -y install ipvsadm
fi
#配置规则(不需要设置ip_forword)
ipvsadm -C
read -p "rule:" rule
read -p "port:" port
ipvsadm -A -t $vip:$port -s $rule
read -p "rip1:" rip1
ipvsadm -a -t $vip:$port -r $rip1 -g
read -p "rip2:" rip2
ipvsadm -a -t $vip:$port -r $rip2 -g
(四)rs脚本
#!/bin/bash
#在ens33上挂载一个ip地址
read -p "vip:" vip
read -p "mac:" mac
read -p "num" num
ifconfig $mac:$num $vip broadcast $vip netmask
255.255.255.255
# 主机路由
route add -host $vip dev $mac:$num
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