主页 > 开源代码  > 

LVS负载均衡集群(NAT模式)

LVS负载均衡集群(NAT模式)
一、环境准备

四台主机(一台 LVS、两台 RS、一台客户端)

注意每个虚拟机都需要关闭防火墙和 selinux

1.1.LVS 主机

LVS 主机(两块网卡)

第一块:NAT模式(内网)

第二块:添加网卡(仅主机模式)(外网)

开启新加网卡的dhcp服务,自动获取IP

dhclient ens33 命令的作用是在名为 ens33 的网络接口上启动 dhclient 服务。

dhclient 是一个用于动态获取 IP 地址的客户端程序。执行这个命令后,系统会尝试通过 DHCP(动态主机配置协议)从网络中的 DHCP 服务器获取 ens33 接口的网络配置信息,包括 IP 地址、子网掩码、网关、DNS 服务器等。

# dhclient 开启网卡DHCP服务 dhclient ens192

修改新加网卡 IP,修改主机域名

# 修改内网IP nmcli connection modify ens192 ipv4.method manual ipv4.addresses 192.168.13.100/24 ipv4.gateway 192.168.13.2 ipv4.dns 223.5.5.5 connection.autoconnect yes ​ nmcli connection up ens192 ​ # 修改外网IP nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.67.100/24 ipv4.gateway 192.168.67.2 ipv4.dns 223.5.5.5 connection.autoconnect yes ​ nmcli connection up ens160 ​ # 修改主机名 hostnamectl set-hostname lvs

LVS 两块网卡网段不一致,打开内核路由,实现通信

vim /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). kernel.sysrq=0 net.ipv4.ip_forward=1 net.ipv4.conf.all.send_redirects=0 net.ipv4.conf.default.send_redirects=0 net.ipv4.conf.all.accept_source_route=0 net.ipv4.conf.default.accept_source_route=0 net.ipv4.conf.all.accept_redirects=0 net.ipv4.conf.default.accept_redirects=0 net.ipv4.conf.all.secure_redirects=0 net.ipv4.conf.default.secure_redirects=0 net.ipv4.icmp_echo_ignore_broadcasts=1 net.ipv4.icmp_ignore_bogus_error_responses=1 net.ipv4.conf.all.rp_filter=1 net.ipv4.conf.default.rp_filter=1 net.ipv4.tcp_syncookies=1 kernel.dmesg_restrict=1 net.ipv6.conf.all.accept_redirects=0 net.ipv6.conf.default.accept_redirects=0

sysctl -p 命令用于使通过 sysctl 命令设置的内核参数更改立即生效。通常,在修改了 /etc/sysctl.conf 或其他相关的内核参数配置文件后,执行 sysctl -p 可以让新的参数配置在系统中立即应用,而无需重新启动系统。 # 使内核参数更改立即生效 sysctl -p

1.2.web-server 主机

web-server1 和 web-server2 主机修改IP,网关地址是 lvs 主机的 DIP 地址

# web-server1 主机 nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.67.10/24 ipv4.gateway 192.168.67.100 ipv4.dns 223.5.5.5 connection.autoconnect yes ​ nmcli connection up ens160 ​ hostnamectl set-hostname web-server1 ​ # web-server2 主机 nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.67.20/24 ipv4.gateway 192.168.67.100 ipv4.dns 223.5.5.5 connection.autoconnect yes ​ nmcli connection up ens160 ​ hostnamectl set-hostname web-server1

查看网关地址

route -n

1.3.添加 hosts 解析 

lvs、web-server1、web-server2 都需要做

cat >> /etc/hosts << EOF 192.168.67.100 lvs 192.168.13.100 lvs 192.168.67.10 web-server1 192.168.67.20 web-server2 EOF

二、配置 nginx 服务

2.1.安装软件

web-server1 和 web-server2 主机安装 nginx 服务

yum install nginx -y 2.2.配置服务

为了查看实验效果,所以两台web服务器提供的内容不一样。真实企业项目,两台web服务器需提供相同的内容。

# web-server1 主机配置 echo web-server1 - 192.168.67.10 > /usr/share/nginx/html/index.html ​ systemctl enable --now nginx ​ # web-server2 主机配置 echo web-server2 - 192.168.67.120 > /usr/share/nginx/html/index.html ​ systemctl enable --now nginx

检测,lvs 主机访问两个 web-server 服务器

三、配置 LVS

3.1.安装 LVS 软件

在 LVS 服务器中安装 ipvsadm 软件

yum install ipvsadm

3.2.LVS软件相关信息

程序包:ipvsadm

Unit File:ipvsadm.service

主程序:/usr/sbin/ipvsadm

规则保存工具:/usr/sbin/ipvsadm-save

规则重载工具:/usr/sbin/ipvsadm-restore

配置文件:/etc/sysconfig/ipvsadm-config

ipvs 调度规则文件:/etc/sysconfig/ipvsadm

3.3.ipvsadm 选项

ipvsadm 是用于 Linux 内核的 IP 虚拟服务器(IPVS)的管理工具。以下是一些常见的选项:

-A:添加一个新的虚拟服务

-E:修改一个现有的虚拟服务

-D:删除一个虚拟服务

-C:清除内核虚拟服务器表中的所有记录

-L 或 -l:列出内核虚拟服务器表中的规则

-S:保存规则到指定的文件

-R:从指定的文件恢复规则

-Z:清空计数器

-a:向虚拟服务添加一个真实服务器

-e:修改一个真实服务器的参数

-d:从虚拟服务中删除一个真实服务器

-n:以数字形式显示地址和端口,而不是尝试将其解析为域名和服务名

-t :tcp 协议,选项用于指定虚拟服务的目标地址和端口,格式为 IP:PORT

-u:udp协议,选项用于指定虚拟服务的目标地址和端口,格式为 IP:PORT

-s :选项用于设置负载均衡调度算法(Scheduling Algorithm)

rr:轮询(Round Robin)

wrr:加权轮询(Weighted Round Robin)

lc:最少连接(Least Connections)

wlc:加权最少连接(Weighted Least Connections)

-r :选项用于指定真实服务器(Real Server)

-m :表示使用 NAT(Network Address Translation,网络地址转换)模式

-g :表示使用 DR(Direct Routing,直接路由)模式

-i :表示使用 TUN(IP Tunneling,IP 隧道)模式

-w:为真实服务器设置权重(Weight)

--rate:输出速率信息

3.4.添加规则

添加虚拟服务策略(VIP地址)

ipvsadm -A -t 192.168.13.100:80 -s rr

添加真实服务策略

ipvsadm -a -t 192.168.13.100:80 -r 192.168.67.10:80 -m ipvsadm -a -t 192.168.13.100:80 -r 192.168.67.20:80 -m

查看策略

ipvsadm -Ln

3.5.保存规则

添加保存命令到 ipvs 调度规则文件

ipvsadm-save > /etc/sysconfig/ipvsadm

 清空规则 ipvsadm -C 导入规则(恢复保存的规则) ipvsadm-restore < /etc/sysconfig/ipvsadm 规则不用手动保存在 /etc/sysconfig/ipvsadm 文件中,只要不用 ipvsadm -C 指令清空规则,规则就会在 ipvsadm.service 服务重启后自动写入  /etc/sysconfig/ipvsadm 文件中,重启后规则会自动设置

四、测试

客户端访问集群 VIP 地址

基于LVS-NAT模式,实现了HTTP/HTTPS请求的透明分发,支持动态流量分配至后端多台Web服务器。

验证了加权轮询(Weighted Round Robin)和最小连接(Least Connections)算法的有效性,确保服务器资源利用率均衡。

for i in {1..10} do curl 192.168.13.100 done

标签:

LVS负载均衡集群(NAT模式)由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“LVS负载均衡集群(NAT模式)