基于nat模式的lvs:
------------------------------服务端----------------------------------------
前端服务器设置:
对外网卡:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno16777736
ONBOOT=yes
IPADDR=192.168.1.110
PREFIX=255.255.255.0
GATEWAY=192.168.1.253
DNS1=“192.168.1.253”

对应网卡:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno33554960
ONBOOT=yes
IPADDR=192.168.17.138
NETMASK=255.255.255.0
DNS1=192.168.1.253

systemctl restart network

yum -y install ipvsadm

ipvsadm -A -t 192.168.1.110:80 -s rr (rr代表轮训)
ipvsadm -a -t 192.168.1.110:80 -r 192.168.17.130:80 -m 后端服务器有几台就加几台
ipvsadm -a -t 192.168.1.110:80 -r 192.168.17.131:80 -m

ipvsadm -Sn > /etc/sysconfig/ipvsadm 保存调度规则
打开/etc/sysctl.conf文件加入:net.ipv4.ip_forward=1 打开路由转发

打开防火墙systemctrl start wirealld
firewall-cmd --set-default-zone=trusted
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
systemctl start ipvsadm

ipvsadm -Ln 查看配置
ipvsadm -Lnc 看连接情况
ipvsadm -D -t 192.168.1.110:80 删除虚拟服务
ipvsadm -d -t 192.168.1.110:80 -r 192.168.17.130 删除后端服务器130邦
ipvsadm -Sn > /tmp/ipvs.back 备份规则
ipvsadm -C 清空规则
ipvsadm -R </tmp/ipvs.back 还原规则
--------------------------------------客户端--------------------------------------------

后端两台服务器130和131都要装上httpd服务并防火墙放行80,
服务器不用安装httpd服务但要放行80
后端两台服务器ip地址都要和前端lvs机的对内ip在一个局域网,而且后端两服务器的网关是
lvs对应的ip地址
例:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno33554960
ONBOOT=yes
IPADDR=192.168.17.130
NETMASK=255.255.255.0
GATEWAY=192.168.17.138
-----------------------------DR模式的负载均衡-----------------------------------
整体规划如下:
router 外网卡124.126.147.169 内网卡:192.168.0.253
lvs调整器VIP地址:124.126.147.168 内网卡:192.168.0.254
下面是真实的三台web服务器:
web1服务器真实网卡192.168.0.1 虚拟网卡ifcfg-lo:0 124.126.147.168(VIP)
web2服务器真实网卡192.168.0.2 虚拟网卡ifcfg-lo:0 124.126.147.168(VIP)
web3服务器真实网卡192.168.0.3 虚拟网卡ifcfg-lo:0 124.126.147.168(VIP)

LVS对外网卡设置:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno33554960
ONBOOT=yes
IPADDR=124.126.147.168
NETMASK=255.0.0.0
DNS1=202.96.134.133

对内网卡设置:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno33554960
ONBOOT=yes
IPADDR=192.168.0.254
NETMASK=255.255.255.0
DNS1=202.96.134.133

systemctl restart network重启网络
yum install ipvsadm
ipvsadm -A -t 124.126.147.168:80 -s wrr (wrr代表使用DR模式)
ipvsadm -a -t 124.126.147.168:80 -r 192.168.0.1:80 -g -w 1
ipvsadm -a -t 124.126.147.168:80 -r 192.168.0.2:80 -g -w 2
ipvsadm -a -t 124.126.147.168:80 -r 192.168.0.3:80 -g -w 3

ipvsadm -Sn > /etc/sysconfig/ipvsadm 保存调试器
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload

真实web1设置
真实网卡:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno1111
ONBOOT=yes
IPADDR=192.168.0.1
NETMASK=255.255.255.0
GATEWAY=192.168.0.253

新建立一个虚拟网卡
vim /etc/sysconfig/network-script/ifcfg-lo:0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=lo:0
ONBOOT=yes
IPADDR=124.126.147.168
NETMASK=255.255.255.0
GATEWAY=192.168.0.253

要禁止对VIP地址ARP响应:
vim /etc/sysctl.conf
net.ipv4.conf.eno1111.arp_ignore = 1
net.ipv4.conf.eno1111.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

sysctl -p
yum install httpd
systemctl start httpd
systemctl restart network
echo “192.168.0.1” > /var/www/html/index.html
firwall-cmd --permanent --add-port=80/tcp
firwall-cmd --reload

真实web2设置
真实网卡:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno2222
ONBOOT=yes
IPADDR=192.168.0.2
NETMASK=255.255.255.0
GATEWAY=192.168.0.253

新建立一个虚拟网卡
vim /etc/sysconfig/network-script/ifcfg-lo:0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=lo:0
ONBOOT=yes
IPADDR=124.126.147.168
NETMASK=255.255.255.0
GATEWAY=192.168.0.253

要禁止对VIP地址ARP响应:
vim /etc/sysctl.conf
net.ipv4.conf.eno2222.arp_ignore = 1
net.ipv4.conf.eno2222.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

sysctl -p
yum install httpd
systemctl start httpd
systemctl restart network
echo “192.168.0.2” > /var/www/html/index.html
firwall-cmd --permanent --add-port=80/tcp
firwall-cmd --reload

真实web3设置
真实网卡:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno3333
ONBOOT=yes
IPADDR=192.168.0.3
NETMASK=255.255.255.0
GATEWAY=192.168.0.253

新建立一个虚拟网卡
vim /etc/sysconfig/network-script/ifcfg-lo:0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=lo:0
ONBOOT=yes
IPADDR=124.126.147.168
NETMASK=255.255.255.0
GATEWAY=192.168.0.253

要禁止对VIP地址ARP响应:
vim /etc/sysctl.conf
net.ipv4.conf.eno3333.arp_ignore = 1
net.ipv4.conf.eno3333.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

sysctl -p
yum install httpd
systemctl start httpd
systemctl restart network
echo “192.168.0.3” > /var/www/html/index.html
firwall-cmd --permanent --add-port=80/tcp
firwall-cmd --reload

router用linux充当:
第一块网卡对内:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno4444
ONBOOT=yes
IPADDR=192.168.0.253
NETMASK=255.255.255.0

第二块对外:
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno5555
ONBOOT=yes
IPADDR=124.126.147.169
NETMASK=255.0.0.0

vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

sysctl -p
systemctl restart network