1、LVS实现nat,dr
1)NAT
LVS服务器 外网卡:192.168.80.100 内网卡:192.168.10.100
后端服务器1:192.168.10.101
后端服务器2:192.168.10.102
echo1> /proc/sys/net/ipv4/ip_forward
ipvsadm -A -t 192.168.80.100:80 -s rr
ipvsadm -a -t 192.168.80.100:80 -r 192.168.10.101:80 -m
ipvsadm -a -t 192.168.80.100:80 -r 192.168.10.102:80 -m
后端服务器上都安装NGINX服务
[root@localhost ~]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http rr
-> 192.168.10.101:http Masq 1 1 0
-> 192.168.10.102:http Masq 1 1 0
访问LVS外网服务器地址:80端口 RR 算法
2)DR
虚拟VIP地址:192.168.80.200
LVS服务器地址:192.168.80.100
NGINX1服务器地址:192.168.80.101
NGINX1服务器地址:192.168.80.102
1)LVS服务器配置
ifconfig ens33:0 192.168.80.200 netmask 255.255.255.255
# 在LVS服务器上添加一个地址为VIP的虚拟网卡ens33:0
2)后端服务器配置
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
# 将对应网卡设置为只回应目标IP为自身接口地址的ARP请求
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
# 将ARP请求的源IP设置为eth0上的IP,也就是RIP
ifconfig lo:0 192.168.80.200 broadcast 192.168.80.200 netmask 255.255.255.255
# 添加IP地址为VIP的虚拟网卡lo:0
route add -host 192.168.80.200 dev lo:0
# 添加一条路由,目标IP为VIP的数据包使用lo接口发送,这样响应报文的源IP就会为VIP
ipvsadm -A -t 192.168.80.200:80 -s wrr
# 添加虚拟服务,指定IP、端口、算法
ipvsadm -a -t 192.168.80.200:80 -r 192.168.80.101:80 -g -w 1
# 添加nginx1服务器,指定DR模式,指定权重为1
ipvsadm -a -t 192.168.80.200:80 -r 192.168.80.102:80 -g -w 1
# 添加nginx2服务器,指定DR模式,指定权重为1
[root@localhost network-scripts]# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP localhost.localdomain:http wrr
-> 192.168.80.101:http Route 1 0 0
-> 192.168.80.102:http Route 1 0 0
2、nginx反向代理,虚拟主机
NGINX1 192.168.80.100 (反向代理服务器)
/etc/nginx/conf.d/upstream.conf
upstream nginx {
server 192.168.80.101:80;
server 192.168.80.102:80;
}
修改 /etc/nginx/nginx.conf 配置文件
location / {
proxy_pass http://nginx/;
}
NGINX2 192.168.80.101 (WEB服务器1)
NGINX2 192.168.80.102 (WEB服务器2)