添加规则,并做持久操作
1 添加规则
[root@lvs ~]# ipvsadm -A -t 10.36.178.183:80 -s wrr
[root@lvs ~]# ipvsadm -a -t 10.36.178.183:80 -r 192.168.65.201:80 -m -w 3
[root@lvs ~]# ipvsadm -a -t 10.36.178.183:80 -r 192.168.65.202:80 -m -w 1
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.36.178.183:80 wrr
-> 192.168.65.201:80 Masq 3 0 0
-> 192.168.65.202:80 Masq 1 0 0
2 持久操作
单纯添加,重启服务器后,规则会失效。为了不让规则失效,我们可以这样做:
第一种:将规则导入到文本文件中,然后手动导入规则
[root@lvs ~]# ipvsadm -S
-A -t lvs.com:http -s wrr
-a -t lvs.com:http -r node1:http -m -w 3
-a -t lvs.com:http -r node2:http -m -w 1
[root@lvs ~]# ipvsadm -S -n # -n: 以数字形式显示
-A -t 10.36.178.183:80 -s wrr
-a -t 10.36.178.183:80 -r 192.168.65.201:80 -m -w 3
-a -t 10.36.178.183:80 -r 192.168.65.202:80 -m -w 1
# 将规则导入到ipvsrule文件中
[root@lvs ~]# ipvsadm -S -n > ./ipvsrule
[root@lvs ~]# cat ipvsrule
-A -t 10.36.178.183:80 -s wrr
-a -t 10.36.178.183:80 -r 192.168.65.201:80 -m -w 3
-a -t 10.36.178.183:80 -r 192.168.65.202:80 -m -w 1
# 清除规则
[root@lvs ~]# ipvsadm -C
# 查看规则,为空
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
# 导入规则
[root@lvs ~]# ipvsadm -R < ipvsrule
# 查看规则
第二种:开启ipvsadm服务,创建存放规则的配置目录,最后设置ipvsadm开机自启
[root@lvs ~]# systemctl start ipvsadm.service # 启动失败
Job for ipvsadm.service failed because the control process exited with error code.
See "systemctl status ipvsadm.service" and "journalctl -xeu ipvsadm.service" for details.
[root@lvs ~]# journalctl -xeu ipvsadm.service
# 查看错误原因:"/etc/sysconfig/ipvsadm:没有那个文件" 故咱们自己创建一个
# 再次启动ipvsadm.service就成功了
[root@lvs ~]# touch /etc/sysconfig/ipvsadm
[root@lvs ~]# ipvsadm -R < ipvsrule # 手动导入规则
[root@lvs ~]# systemctl start ipvsadm.service # 启动ipvsadm.service
# 启动ipvsadm.service的时候干哪些事情:
1.ipvsadm -R < /etc/sysconfig/ipvsadm
[root@lvs ~]# systemctl enable ipvsadm.service --now
[root@lvs ~]# systemctl stop ipvsadm.service # 停止ipvsadm.service
# 停止ipvsadm.service时干哪些事情:
1.先保存规则到/etc/sysconfig/ipvsadm
2.ipvsadm -C
LVS 负载均衡集群企业级应用实战
1LVS/NAT模式
1.配置规则
[root@lvs ~]# ipvsadm -A -t 10.36.178.183:80 -s wrr
[root@lvs ~]# ipvsadm -a -t 10.36.178.183:80 -r 192.168.65.201:80 -m -w 3
[root@lvs ~]# ipvsadm -a -t 10.36.178.183:80 -r 192.168.65.202:80 -m -w 1
[root@lvs ~]# ipvsadm -Ln # 查看规则
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.36.178.183:80 wrr
-> 192.168.65.201:80 Masq 3 0 0
-> 192.168.65.202:80 Masq 1 0 0
2.为负载均衡器添加一块网卡,保证负载均衡器和服务器正常通信
负载均衡器左边是桥接模式,连接客户端,右边是NAT模式连接后台服务器,因此负载均衡器需要两张网卡(桥接与NAT)。
负载均衡器左边桥接模式ip作用:桥接网段用户能够通过负载均衡器连接到服务器。
负载均衡器右边NAT模式ip作用:连接后端两台服务器。
- 添加网卡时需要注意:
因为我是直接复制的/etc/NetworkManager/system-connections/ens33.nmconnection
为ens33.nmconnection
,导致ens33和ens36网卡的uuid一样,一直起不来ens36。
- 解决办法:删掉复制过来的uuid即可。
3.开启路由转发,让NAT可以连接到桥接
[root@lvs ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@lvs ~]# sysctl -p # 确保打开路由转发,加载sysctl.conf配置
4.两台服务器将lvs作为网关
# 添加一条指向到负载均衡器的路由,ip为负载均衡器内能够与后端服务器通信的ip
[root@node1 ~]# ip r add default via 192.168.65.168
[root@node2 ~]# ip r add default via 192.168.65.168
[root@node1 ~]# ip r # 查看路由
default via 192.168.65.168 dev ens33
default via 192.168.65.2 dev ens33 proto static metric 100
如果不设置lvs作为网关的话,这两台服务器就只有输入,没有输出。
5.两台服务器安装nginx
[root@node1 ~]# yum install -y nginx
[root@node2 ~]# yum install -y nginx
[root@node1 ~]# rm -rf /usr/share/nginx//html/*
[root@node2 ~]# rm -rf /usr/share/nginx//html/*
6.配置nginx文件
[root@node1 ~]# vim /etc/nginx/nginx.conf
# keepalive_timeout:Nginx 处理的每个请求均有相应的超时设置
keepalive_timeout 65; ==> keepalive_timeout 0;
[root@node2 ~]# vim /etc/nginx/nginx.conf
keepalive_timeout 65; ==> keepalive_timeout 0;
# curl没有请求超时
[root@lvs ~]# curl http://10.36.178.183
7.查看规则
[root@lvs ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 10.36.178.183:80 63 1199 646 240404 68671
-> 192.168.65.201:80 47 1031 552 223177 61187
-> 192.168.65.202:80 16 168 94 17227 7484
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.36.178.183:80 wrr
-> 192.168.65.201:80 Masq 3 2 13
-> 192.168.65.202:80 Masq 1 0 4
[root@lvs ~]# ipvsadm -Z # 虚拟服务器表计数(当前连接数量)清零
[root@lvs ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 10.36.178.183:80 0 0 0 0 0
-> 192.168.65.201:80 0 0 0 0 0
-> 192.168.65.202:80 0 0 0 0 0