深入理解LVS篇:搭建基于LVS的负载均衡服务器

本文详细介绍如何在DR模型下,使用三台虚拟机搭建负载均衡环境,通过配置VIP、修改HTTPD服务及调整ARP协议参数,实现客户端请求在两台web服务器间的平衡分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本次实验基于DR模型来搭建,我们需要准备至少三台机器,这里全部使用虚拟机。这三台机器的IP分别是192.168.40.129 192.168.40.132 192.168.40.133。我们先来看一下我们要搭建的模型的整体架构:

架构图
我们希望实现的效果是,当用户请求打在192.168.40.200:80的时候能够将node2和node3的web服务以负载均衡的方式显示出来。其中192.168.40.200:80是一个虚拟IP也就是VIP。如果不知道什么是VIP或者其作用的,可以去看看我之前写的文章。

在node2和node3上安装httpd

yum install -y httpd
然后直接运行httpd -k start
为什么要用httpd? 因为部署快,我们只需要下载然后运行即可看到效果。

修改httpd的默认主页:
node2:echo '192.168.40.132' > /var/www/html/index.html
node3:echo '192.168.40.133' > /var/www/html/index.html
这一步主要是为了能够在负债均衡的时候看到ip地址的变化

尝试访问我们的web服务,在浏览器上输入192.168.40.133192.168.40.132
133
132

在node2和node3上添加VIP

node2和node3都需要在本地网卡记录VIP,因为它们都需要能够接收到客户端发送过来的数据包,如果没有记录相应的目标地址IP的话会被丢弃掉。
我们可以使用ifconfig lo:2 192.168.40.200 netmask 255.255.255.255来添加,这句命令的语义是:
在虚拟网卡的2端口添加一个源ip是192.168.40.200的路由条目,其子网掩码的是255.255.255.255。为什么要配置的是虚拟网卡,其实和arp协议有关,如果配置的虚拟网卡,才有可能做到隐藏VIP的目的,后面会讲到。

在node2和node3执行完后我们在打开ifconfig会发现多了我们刚刚配置的信息:
在这里插入图片描述

修改node2和node3的arp协议的传播类型

我们的node2和node3节点已经有了VIP,但这是不够的,因为一旦拥有了这个IP,服务上线的时候就会将这条地址广播给局域网当中的所有主机,这么一来当我们的客户端发送请求的时候,就无法辨别出数据包究竟该发送给node2还是node3,而实际上,数据包应该是交给master节点处理,并且整个局域网当中也只有master节点能对外暴露自己的VIP
那么我们该怎么做才能既让node2和node3拥有VIP,又不让这个IP让别人知道呢?答案是修改arp协议的传播类型arp_ignorearp_announce。这两个参数什么意思,我把英语原文解释贴在下面了:

arp_ignore - INTEGER
Define different modes for sending replies in response to
received ARP requests that resolve local target IP addresses:
0 - (default): reply for any local target IP address, configured
on any interface
1 - reply only if the target IP address is local address
configured on the incoming interface
2 - reply only if the target IP address is local address
configured on the incoming interface and both with the
sender's IP address are part from same subnet on this interface
3 - do not reply for local addresses configured with scope host,
only resolutions for global and link addresses are replied
4-7 - reserved
8 - do not reply for all local addresses
The max value from conf/{all,interface}/arp_ignore is used
when ARP request is received on the {interface}


arp_announce - INTEGER
Define different restriction levels for announcing the local
source IP address from IP packets in ARP requests sent on
interface:
0 - (default) Use any local address, configured on any interface
1 - Try to avoid local addresses that are not in the target's
subnet for this interface. This mode is useful when target
hosts reachable via this interface require the source IP
address in ARP requests to be part of their logical network
configured on the receiving interface. When we generate the
request we will check all our subnets that include the
target IP and will preserve the source address if it is from
such subnet. If there is no such subnet we select source
address according to the rules for level 2.
2 - Always use the best local address for this target.
In this mode we ignore the source address in the IP packet
and try to select local address that we prefer for talks with
the target host. Such local address is selected by looking
for primary IP addresses on all our subnets on the outgoing
interface that include the target IP address. If no suitable
local address is found we select the first local address
we have on the outgoing interface or on all other interfaces,
with the hope we will receive reply for our request and
even sometimes no matter the source IP address we announce.
The max value from conf/{all,interface}/arp_announce is used.

脑阔疼,大概翻译一下,我们只需要关系前两个参数:
arp_ignore控制收到arp请求后的响应方式

  • 0代表收到arp报文后,会毫无保留的把自己的本地目标ip告诉对方
  • 1代表只对指定了本地目标ip的报文进行相应

arp_announce控制的是主动发送arp数据的方式

  • 允许使用任意网卡上的IP地址作为arp请求的源IP
  • 尽量不使用本地网卡的子网卡的ip地址作为arp请求的源IP地址

从上面的描述可以看出,如果我们的VIP是配置在物理网卡上,无疑还是会把自己的IP暴露出去,这也是我们选择在node2和node3节点的虚拟网卡上创建VIP的原因。

很明显,我们应该把这俩参数都设置成1:
echo 1 > /proc/sys/net/ipv4/conf/eth2/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore

配置master

先配置VIP,与node2和node3不同的是,master要在物理网卡上配:
ifconfig eth2:2 192.168.40.200/24
网卡配置2
安装ipvsadm:yum install ipvsadm
在ipvsadm中配置VIP和RIP:

ipvsadm -A  -t  192.168.40.200:80  -s rr #配置虚拟IP
ipvsadm -a  -t 192.168.40.200:80  -r  192.168.40.133 -g -w 1 #配置虚拟IP对应需要负载的真实服务器地址
ipvsadm -a  -t 192.168.40.200:80  -r  192.168.40.132 -g -w 1 #配置虚拟IP对应需要负载的真实服务器地址

然后可以使用ipvsadm -ln查看配置的信息:

在这里插入图片描述

大功告成,我们访问192.168.40.200试试效果吧!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值