NAT模式下的lvs集群
lvs-nat概念:修改请求报文的目标IP,多目标IP的DNAT,本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发
最终实现效果:
1.Director 服务器采用双网卡,一个是桥接网卡连接外网,一个是仅主机网卡与后端Web服务器相连
2.Web服务器采用仅主机网卡与director相连
3.Web服务器网关指向192.168.0.100
4.后端web服务器不需要连接外网
准备工作
工作前提准备:
- VMware的网卡设置:一个网卡设置仅主机模式,网段为192.168.0.0/24 另一个网卡设置仅主机模式,网段为172.25.254.0/24
- 准备四台环境干净的虚拟机
- 防火墙及其selinux都处于关闭状态
流程原理图:
具体步骤
客户机
IP:172.25.254.200/24
gateway:172.25.254.100
lvs
bash
[root@lvs1 ~]# vmset.sh eth0 172.25.254.100 lvs1
[root@lvs1 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@lvs1 ~]# vmset.sh eth1 192.168.0.100 lvs
[root@lvs1 ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[root@lvs1 ~]# nmcli connection reload
[root@lvs1 ~]# nmcli connection up eth1
lvs中打开内核路由
bash
[root@lvs1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@lvs1 ~]# sysctl -p
安装lvs软件
bash
[root@lvs1 ~]# dnf install ipvsadm -y
编写策略
bash
[root@lvs1 ~]# ipvsadm -A -t 172.25.254.100:80 -s rr
[root@lvs1 ~]# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.10:80 -m
[root@lvs1 ~]# ipvsadm -a -t 172.25.254.100:80 -r 192.168.0.20:80 -m
# 查看
[root@lvs1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.254.100:80 rr
-> 192.168.0.10:80 Masq 1 0 0
-> 192.168.0.20:80 Masq 1 0 0
服务器1
bash
[root@webserver ~]# vmset.sh eth0 192.168.0.10 webserver
[root@webserver ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@webserver ~]# nmcli connection reload
[root@webserver ~]# nmcli connection up eth0
修改网关
安装web服务:
bash
[root@webserver ~]# yum install httpd -y
[root@webserver ~]# echo webserver 192.168.0.10 > /var/www/html/index.html
[root@webserver ~]# systemctl enable --now httpd
服务器2
bash
[root@webserver2 ~]# vmset.sh eth0 192.168.0.20 webserver2
[root@webserver2 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@webserver2 ~]# nmcli connection reload
[root@webserver2 ~]# nmcli connection up eth0
修改网关
安装web服务:
bash
[root@webserver2 ~]# yum install httpd -y
[root@webserver2 ~]# echo webserver 192.168.0.10 > /var/www/html/index.html
[root@webserver2 ~]# systemctl enable --now httpd
测试
在客户机上测试
DR模式下的lvs集群
DR模式:Direct Routing,直接路由,LVS默认模式,应用最广泛,通过为请求报文重新封装一个MAC首部进行
转发
源MAC是DIP所在的接口的MAC,目标MAC是某挑选出的RS的RIP所在接口的MAC地址;
源IP/PORT,以及目标IP/PORT均保持不变
流程原理图:
具体流程
客户机:
IP:172.25.254.200/24
gateway:172.25.254.100
路由器:
sh脚本:
bash
#!/bin/bash
rm -fr /etc/NetworkManager/system-connections/$1.nmconnection
cat > /etc/NetworkManager/system-connections/$1.nmconnection <<EOF
[connection]
id=$1
type=ethernet
interface-name=$1
[ipv4]
address1=$2/24,172.25.254.2
method=manual
dns=114.114.114.114;
EOF
chmod 600 /etc/NetworkManager/system-connections/$1.nmconnection
nmcli connection reload
nmcli connection up $1
hostnamectl hostname $3
cat > /etc/hosts <<EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
$2 $3
EOF
bash
[root@route ~]# vmset.sh eth0 172.25.254.100 route
[root@route ~]# vmset.sh eth1 192.168.0.100 route
[root@route ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[root@route ~]# nmcli connection reload
[root@route ~]# nmcli connection up eth1
因脚本问题,修改eth1的网卡配置
打开内核路由功能
bash
[root@route ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@route ~]# sysctl -p
LVS
bash
[root@lvs1 ~]# vmset.sh eth1 192.168.0.50 lvs
[root@lvs1 ~]# vim /etc/NetworkManager/system-connections/eth1.nmconnection
[root@lvs1 ~]# nmcli connection reload
[root@lvs1 ~]# nmcli connection up eth1
添加vip,采用环回,注意环回的掩码需要使用32位
bash
[root@lvs1 ~]# ip a a 192.168.0.200/32 dev lo
安装lvs软件
bash
[root@lvs1 ~]# dnf install ipvsadm -y
编写策略
bash
[root@lvs1 ~]# ipvsadm -A -t 192.168.0.200:80 -s wrr
[root@lvs1 ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -m
[root@lvs1 ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -m
服务器1
bash
[root@webserver ~]# vmset.sh eth0 192.168.0.10 webserver
[root@webserver ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@webserver ~]# nmcli connection reload
[root@webserver ~]# nmcli connection up eth0
修改网关
安装web服务:
bash
[root@webserver ~]# yum install httpd -y
[root@webserver ~]# echo webserver 192.168.0.10 > /var/www/html/index.html
[root@webserver ~]# systemctl enable --now httpd
修改rip,使RS主机VIP不对外响应
bash
[root@webserver ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@webserver ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@webserver ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@webserver ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
添加vip,采用环回,注意环回的掩码需要使用32位
bash
[root@webserver ~]# ip a a 192.168.0.200/32 dev lo
安装web服务:
bash
[root@webserver ~]# yum install httpd -y
[root@webserver ~]# echo webserver 192.168.0.10 > /var/www/html/index.html
[root@webserver ~]# systemctl enable --now httpd
服务器2
bash
[root@webserver2 ~]# vmset.sh eth0 192.168.0.20 webserver2
[root@webserver2 ~]# vim /etc/NetworkManager/system-connections/eth0.nmconnection
[root@webserver2 ~]# nmcli connection reload
[root@webserver2 ~]# nmcli connection up eth0
修改网关
修改rip,使RS主机VIP不对外响应
bash
[root@webserver2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@webserver2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@webserver2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@webserver2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
添加vip,采用环回,注意环回的掩码需要使用32位
bash
[root@webserver2 ~]# ip a a 192.168.0.200/32 dev lo
安装web服务:
bash
[root@webserver2 ~]# yum install httpd -y
[root@webserver2 ~]# echo webserver2 192.168.0.20 > /var/www/html/index.html
[root@webserver2 ~]# systemctl enable --now httpd
测试
在客户机上进行测试
bash
[root@client ~]# for i in {1..10}
> do
> curl 192.168.0.200
> done
werbserver2 192.168.0.20
webserver 192.268.0.10
werbserver2 192.168.0.20
werbserver2 192.168.0.20
webserver 192.268.0.10
werbserver2 192.168.0.20
werbserver2 192.168.0.20
webserver 192.268.0.10
werbserver2 192.168.0.20
werbserver2 192.168.0.20
[root@client ~]#