LVS/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 持久操作

单纯添加,重启服务器后,规则会失效。为了不让规则失效,我们可以这样做:

第一种:将规则导入到文本文件中,然后手动导入规则

复制代码
[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.nmconnectionens33.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
相关推荐
码农101号28 分钟前
Linux中Docker Compose介绍和使用
linux·运维·docker
IT成长日记1 小时前
【Docker基础】Dockerfile多阶段构建:Multi-stage Builds详解
运维·docker·容器·multi-stage·builds
deeper_wind4 小时前
keeplived双击热备配置
linux·运维·网络
不易思不逸8 小时前
Ubuntu20.04 RTX 4080 Nvidia驱动安装
运维·服务器
筱小虾米8 小时前
Docker配置国内镜像源
运维·docker·容器
L_autinue_Star9 小时前
从0到1实现Shell!Linux进程程序替换详解
linux·运维·服务器·c++·chrome
Ftrans9 小时前
【分享】文件摆渡系统适配医疗场景:安全与效率兼得
大数据·运维·安全
程序员JerrySUN10 小时前
Linux 文件系统实现层详解:原理、结构与驱动衔接
android·linux·运维·数据库·redis·嵌入式硬件
SAP龙哥11 小时前
日常运维问题汇总-58
运维
Britz_Kevin11 小时前
从零开始的云计算生活——第三十二天,四面楚歌,HAProxy负载均衡
负载均衡·生活·#haproxy