LVS实验(实现服务器集群的负载均衡)

基本概念

LVS(Linux Virtual Server)是Linux虚拟服务器的简称。

LVS通过将一个真实服务器集群虚拟成一台服务器来对外提供服务,同时在集群内部实现负载均衡。这种技术能够显著提高服务的处理能力和可靠性,降低单台服务器的负载压力。

核心组件与术语


IPVS(IP Virtual Server):LVS集群的核心软件模块,安装在负载均衡器上,负责虚拟出一个IP地址和端口对外提供服务。
专业术语:
VS(Virtual Server):虚拟服务器,通常指负载均衡器。
DS(Director/Balancer):分发器/负载均衡器,负责接收客户端请求并根据调度算法分发到后端服务器。
RS(Real Server):后端请求处理服务器,负责处理分发器转发的请求。
CIP(Client IP):客户端IP地址。
VIP(Virtual IP):虚拟IP地址,对外提供服务的IP地址。
DIP(Director IP):分发器(负载均衡器)的IP地址。
RIP(Real Server IP):后端服务器的IP地址。

工作模式


LVS支持多种工作模式,以适应不同的应用场景,主要包括:

NAT(Network Address Translation)模式:
在该模式下,负载均衡器不仅需要修改请求报文的目标地址,还需要修改响应报文的源地址。
适用于小规模集群,但请求和响应报文都需要经过负载均衡器,可能成为性能瓶颈。

DR(Direct Routing)模式:
在该模式下,负载均衡器只修改请求报文的目标MAC地址,而不修改IP地址。
后端服务器处理完请求后,直接将响应报文发回客户端,无需经过负载均衡器。
适用于大规模集群,处理效率高。

TUN(IP Tunneling)模式:
通过IP隧道将请求转发到后端服务器,后端服务器处理完请求后直接将响应报文发回客户端。
适用于地理位置分散的集群,但需要所有服务器支持IP Tunneling协议。

Full-NAT模式:
在请求和响应报文中都进行源地址和目标地址的转换。
允许RIP和DIP不在同一IP网络,但要能通信。

实验共部署nat和dr模式和lvs防火墙标记解决轮询调度

部署net模式

1.架构图

一共需要四台虚拟机,两台为RS,一台为lvs,最后一台为客户机

 2.在lvs

配置网卡IP
[root@bb ~]# vim /etc/NetworkManager/system-connections/eth1.connection 


[connection]

id=eth1

type=ethernet

interface-name=eth1



[ipv4]

method=manual

address1=192.168.0.100/24




[root@bb ~]# vim /etc/NetworkManager/system-connections/eth0.connection


[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=172.25.254.254/24,172.25.254.2

dns=114.114.114.114;

[root@bb ~]# nmcli connection reload 
[root@bb ~]# nmcli connection up eth1


[root@bb ~]# nmcli connection reload 
[root@bb ~]# nmcli connection up eth0
打开lvs的内核功能
[root@bb ~]# 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).
net.ipv4.ip_forward=1
 


[root@bb ~]# sysctl -p
net.ipv4.ip_forward = 1
在lvs安装ipvsadm
[root@bb ~]# yum install ipvsadm -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

AppStream                                        94 MB/s | 6.1 MB     00:00    
BaseOS                                          128 MB/s | 1.7 MB     00:00    
依赖关系解决。
================================================================================
 软件包           架构            版本                 仓库                大小
================================================================================
安装:
 ipvsadm          x86_64          1.31-6.el9           AppStream           54 k

事务概要
================================================================================
安装  1 软件包

总计:54 k
安装大小:89 k
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : ipvsadm-1.31-6.el9.x86_64                                       1/1 
  运行脚本: ipvsadm-1.31-6.el9.x86_64                                       1/1 
  验证    : ipvsadm-1.31-6.el9.x86_64                                       1/1 
已更新安装的产品。

已安装:
  ipvsadm-1.31-6.el9.x86_64                                                     

完毕
lvs做策略
[root@bb ~]# ipvsadm -A -t 172.25.254.254:80 -s rr
[root@bb ~]# ipvsadm -a -t 172.25.254.254:80 -r 192.168.0.10:80 -m
[root@bb ~]# ipvsadm -a -t 172.25.254.254:80 -r 192.168.0.20:80 -m

测试结果

结果成功

3.在rs

配置网卡IP
vim /etc/NetworkManager/system-connections/eth0.connection


[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=192.168.0.10/24,192.168.0.100
[root@bb ~]# nmcli connection reload 
[root@bb ~]# nmcli connection up eth0

安装httpd
[root@bb ~]# yum install httpd -y
正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

AppStream                                       247 MB/s | 6.1 MB     00:00    
BaseOS                                           99 MB/s | 1.7 MB     00:00    
依赖关系解决。
================================================================================
 软件包                  架构        版本                  仓库            大小
================================================================================
安装:
 httpd                   x86_64      2.4.53-7.el9          AppStream       54 k
安装依赖关系:
 apr                     x86_64      1.7.0-11.el9          AppStream      127 k
 apr-util                x86_64      1.6.1-20.el9          AppStream       98 k
 apr-util-bdb            x86_64      1.6.1-20.el9          AppStream       15 k
 httpd-core              x86_64      2.4.53-7.el9          AppStream      1.5 M
 httpd-filesystem        noarch      2.4.53-7.el9          AppStream       18 k
 httpd-tools             x86_64      2.4.53-7.el9          AppStream       89 k
 redhat-logos-httpd      noarch      90.4-1.el9            AppStream       18 k
安装弱的依赖:
 apr-util-openssl        x86_64      1.6.1-20.el9          AppStream       17 k
 mod_http2               x86_64      1.15.19-2.el9         AppStream      153 k
 mod_lua                 x86_64      2.4.53-7.el9          AppStream       64 k

事务概要
================================================================================
安装  11 软件包

总计:2.1 M
安装大小:5.9 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : apr-1.7.0-11.el9.x86_64                                        1/11 
  安装    : apr-util-bdb-1.6.1-20.el9.x86_64                               2/11 
  安装    : apr-util-openssl-1.6.1-20.el9.x86_64                           3/11 
  安装    : apr-util-1.6.1-20.el9.x86_64                                   4/11 
  安装    : httpd-tools-2.4.53-7.el9.x86_64                                5/11 
  安装    : redhat-logos-httpd-90.4-1.el9.noarch                           6/11 
  运行脚本: httpd-filesystem-2.4.53-7.el9.noarch                           7/11 
  安装    : httpd-filesystem-2.4.53-7.el9.noarch                           7/11 
  安装    : httpd-core-2.4.53-7.el9.x86_64                                 8/11 
  安装    : mod_http2-1.15.19-2.el9.x86_64                                 9/11 
  安装    : mod_lua-2.4.53-7.el9.x86_64                                   10/11 
  安装    : httpd-2.4.53-7.el9.x86_64                                     11/11 
  运行脚本: httpd-2.4.53-7.el9.x86_64                                     11/11 
  验证    : apr-1.7.0-11.el9.x86_64                                        1/11 
  验证    : apr-util-1.6.1-20.el9.x86_64                                   2/11 
  验证    : apr-util-bdb-1.6.1-20.el9.x86_64                               3/11 
  验证    : apr-util-openssl-1.6.1-20.el9.x86_64                           4/11 
  验证    : httpd-2.4.53-7.el9.x86_64                                      5/11 
  验证    : httpd-core-2.4.53-7.el9.x86_64                                 6/11 
  验证    : httpd-filesystem-2.4.53-7.el9.noarch                           7/11 
  验证    : httpd-tools-2.4.53-7.el9.x86_64                                8/11 
  验证    : mod_http2-1.15.19-2.el9.x86_64                                 9/11 
  验证    : mod_lua-2.4.53-7.el9.x86_64                                   10/11 
  验证    : redhat-logos-httpd-90.4-1.el9.noarch                          11/11 
已更新安装的产品。

已安装:
  apr-1.7.0-11.el9.x86_64                apr-util-1.6.1-20.el9.x86_64          
  apr-util-bdb-1.6.1-20.el9.x86_64       apr-util-openssl-1.6.1-20.el9.x86_64  
  httpd-2.4.53-7.el9.x86_64              httpd-core-2.4.53-7.el9.x86_64        
  httpd-filesystem-2.4.53-7.el9.noarch   httpd-tools-2.4.53-7.el9.x86_64       
  mod_http2-1.15.19-2.el9.x86_64         mod_lua-2.4.53-7.el9.x86_64           
  redhat-logos-httpd-90.4-1.el9.noarch  

完毕!
写入测试内容
[root@bb ~]# echo  web1-10 > /var/www/html/index.html
启用
[root@bb ~]# systemctl enable  --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@bb ~]# 

rs2 和rs1 的一样

部署dr模式

1.架构图

从架构图可以看出,实验需要用到五台虚拟机

其中两台作为RS,一台为LVS,一台router且需要两个网卡,一个为仅主机模式,一个为NAT模式,最后一台作为客户机测试

注意,配置过程中的IP要根据自己的来设定

1,在lvs

配置好网卡
#把lvs的网卡设置上IP

[root@lvs ~]# vim /etc/NetworkManager/system-connections/eth1.connection 


[connection]

id=eth1

type=ethernet

interface-name=eth1



[ipv4]

method=manual

address1=192.168.0.50/24,192.168.0.100

nmcli connection reload
nmcli connection up eth1
之后安装ipvsadm并做策略
#在lvs安装ipvsadm

[root@lvs ~]# yum install ipvsadm -y
[root@lvs ~]# ipvsadm -A -t 192.168.0.200:80 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.10:80 -g 
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:80 -r 192.168.0.20:80 -g 
给lvs的环回配置vip
[root@lvs ~]# ip a  a 192.168.0.200/32 dev lo

2,在RS1

在RS1里配置好网卡IP
[root@web1 ~]# vim /etc/NetworkManager/system-connections/eth0.connection 



[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=192.168.0.10/24,192.168.0.100

[root@bb ~]# nmcli connection reload 
[root@bb ~]# nmcli connection up eth0
安装好httpd,
[root@web1 ~]# yum install httpd -y
在他默认访问的路径下写进内容,以在测试的时候观察,并启用
[root@web1 ~]# echo web1-10 > /var/www/html/index.html 

systemctl enable --now httpd
使RS主机不对外响应VIP
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore 
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce 
[root@web1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce 
[root@web1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore 

给环回配置vip

[root@web1 ~]# ip a a 192.168.0.200/32 dev lo

另外一台的RS配置一样,不过IP要改

3.在router的配置

[root@rou2 ~]# vim /etc/NetworkManager/system-connections/eth1.connection 

[connection]

id=eth1

type=ethernet

interface-name=eth1



[ipv4]

method=manual

address1=192.168.0.100/24




[root@rou2 ~]# vim /etc/NetworkManager/system-connections/eth0.connection 



[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=172.25.254.100/24,172.25.254.2

dns=114.114.114.114;

把router的内核功能打开
[root@rou2 ~]# 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).
net.ipv4.ip_forward = 1


[root@rou2 ~]# sysctl -p
net.ipv4.ip_forward = 1

4.在客户机

配置IP
[root@bb ~]$ vim /etc/NetworkManager/system-connections/eth0.connection 


[connection]

id=eth0

type=ethernet

interface-name=eth0



[ipv4]

method=manual

address1=172.25.254.200/24,172.25.254.100

测试

测试结果为成功

lvs防火墙标记解决轮询调度

在lvs的dr模式的基础上进行配置

1.在rs 1

安装mod_ssl让rs支持https
[root@bb ~]# yum install mod_ssl -y

重启httpd

root@bb ~]# systemctl restart httpd

两台RS的配置一样

2.在lvs上

清空之前的ipvsadm策略
[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 ~]# iptables -t mangle -A PREROUTING -d 192.168.0.200 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 666

在客户机测试

测试结果为成功

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值