文章目录
lvs -nat 模式的实现
设计要点:
(1) RIP与DIP在同一IP网络, RIP的网关要指向DIP
(2) 支持端口映射
(3) Director要打开核心转发功能
RS 上的操作
apache 安装
root@rs1:~# apt install apache2
root@rs2:~# apt install apache2
root@rs1:~# echo RS1 >/var/www/html/index.html
root@rs2:~# echo RS2 >/var/www/html/index.html
apache 服务器的网关指向DIP
root@rs1:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.102 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
root@rs2:~# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.1.102 0.0.0.0 UG 0 0 0 eth0
192.168.0.0 0.0.0.0 255.255.248.0 U 0 0 0 eth0
Director 上的操作
Director 服务器开启核心转发功能
net.ipv4.ip_forward = 1
添加规则
root@director:~# ipvsadm -A -t 10.20.1.102:80 -s rr
root@director:~# ipvsadm -a -t 10.20.1.102:80 -r 192.168.1.103 -m
root@director:~# ipvsadm -a -t 10.20.1.102:80 -r 192.168.1.104 -m
root@director:~# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.20.1.102:80 rr
-> 192.168.1.103:80 Masq 1 0 0
-> 192.168.1.104:80 Masq 1 0 0
client 上的操作
验证:
root@client:~# while true ;do curl 10.20.1.102;sleep 0.5;done
RS1
RS2
ipvsadm 规则的使用
root@director:~# ipvsadm -Ln --rate
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS
-> RemoteAddress:Port
TCP 10.20.1.102:80 0 0 0 0 0
-> 192.168.1.103:80 0 0 0 0 0
-> 192.168.1.104:80 0 0 0 0 0
root@director:~# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 0A140166:0050 rr
-> C0A80168:0050 Masq 1 0 0
-> C0A80167:0050 Masq 1 0 0
rs 加权
root@director:~# ipvsadm -E -t 10.20.1.102:80 -s wrr
root@director:~# ipvs -e -t 10.20.1.102:80 -r 192.168.1.103 -m -w 6
root@director:~# ipvsadm -e -t 10.20.1.102:80 -r 192.168.1.104 -m -w 2
root@director:~# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.20.1.102:80 wrr
-> 192.168.1.103:80 Masq 6 0 0
-> 192.168.1.104:80 Masq 2 0 0
root@director:~#
root@client:~# while true ;do curl 10.20.1.102;sleep 0.5;done
RS2
RS1
RS1
RS1
RS2
RS1
RS1
RS1
RS2
nat 端口映射
root@rs2:~# vim /etc/apache2/ports.conf
Listen 8080
发现无法修改转发规则
root@director:~# ipvsadm -e -t 10.20.1.102:80 -r 192.168.1.104:8080 -m -w 2
Memory allocation problem
可以先删除后建立
root@director:~# ipvsadm -d -t 10.20.1.102:80 -r 192.168.1.103:80
root@director:~# ipvsadm -d -t 10.20.1.102:80 -r 192.168.1.104:80
root@director:~# ipvsadm -a -t 10.20.1.102:80 -r 192.168.1.104:8080 -m -w 2
root@director:~# ipvsadm -a -t 10.20.1.102:80 -r 192.168.1.103:8080 -m -w 6
root@director:~# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.20.1.102:80 wrr
-> 192.168.1.103:8080 Masq 6 0 3
-> 192.168.1.104:8080 Masq 2 0 1
ipvsadm -e -t 10.20.1.102:80 -r 192.168.1.104:8080 -m -w 2
ipvsadm -e -t 10.20.1.102:80 -r 192.168.1.103:8080 -m -w 6
使用源地址hash
root@director:~# ipvsadm -E -t 10.20.1.102:80 -s sh
root@client:~# while true ;do curl 10.20.1.102;sleep 0.5;done
RS1
RS1
RS1