我为了使用LVS+NGINX*2, 还得再部署一台虚拟机
DIP: LVS服务器的本地IP
VIP: 虚拟路由IP, 用于返回数据
RIP: Nginx服务器的本地IP
# 停止网络管理器 systemctl stop NetworkManager # 禁止网络管理器 systemctl disable NetworkManager
cd /etc/sysconfig/network-scripts cp ifcfg-ens33 ifcfg-ens33:1 vi ifcfg-ens33:1
配置文件
TYPE=Ethernet BOOTPROTO=static DEVICE=ens33:1 ONBOOT=yes IPADDR=192.168.247.150 PREFIX=24 GATEWAY=192.168.247.2 DNS1=192.168.247.2
重启网络服务
systemctl restart network
# 安装 yum install ipvsadm # 查看列表 ipvsadm -Ln
cd /etc/sysconfig/network-scripts cp ifcfg-lo ifcfg-lo:1 vi ifcfg-lo:1
配置文件
DEVICE=lo:1 IPADDR=192.168.247.150 NETMASK=255.255.255.255 NETWORK=127.0.0.0 # If you're having problems with gated making 127.0.0.0/8 a martian, # you can change this to something else (255.255.255.255, for example) BROADCAST=127.255.255.255 ONBOOT=yes NAME=loopback
刷新网卡配置
ifup lo
修改配置文件
vi /etc/sysctl.conf
配置文件
net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2
刷新配置
sysctl -p
# 添加路由表 route add -host 192.168.247.150 dev lo:1 # 查看路由表 route -n
为了防止重启路由表恢复, 在开机自动命令表中添加
echo "route add -host 192.168.247.150 dev lo:1" >> /etc/rc.local
[root@localhost network-scripts]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn # 添加一个虚拟服务 采用TCP协议 rr策略(轮询) [root@localhost network-scripts]# ipvsadm -A -t 192.168.247.150:80 -s rr [root@localhost network-scripts]# 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.247.150:80 rr # 为虚拟服务添加一个真实服务器 -g(DR 模式) [root@localhost network-scripts]# ipvsadm -a -t 192.168.247.150:80 -r 192.168.247.136:80 -g [root@localhost network-scripts]# ipvsadm -a -t 192.168.247.150:80 -r 192.168.247.137:80 -g [root@localhost network-scripts]# 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.247.150:80 rr -> 192.168.247.136:80 Route 1 0 0 -> 192.168.247.137:80 Route 1 0 0 [root@localhost network-scripts]#
[root@localhost network-scripts]# ipvsadm -Ln --stats IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes -> RemoteAddress:Port TCP 192.168.247.150:80 3 73 0 19101 0 -> 192.168.247.136:80 1 63 0 18587 0 -> 192.168.247.137:80 2 10 0 514 0 [root@localhost network-scripts]#
man ipvsadm
默认访问策略为300秒, 300秒过后才会轮询所以会导致, 自己测试一直都是访问的一台
ipvsadm -E -t 192.168.247.150:80 -s rr -p 5
已添加时间
通过ipvsadm -Lnc 查看连接过期时间
在过期之后就会触发轮询机制
在搜索过后, 感觉这篇写的很好, 推荐一波
LVS命令解析