Linux服务器高可用:LVS+Keepalived实战配置指南
想象一下这样的场景:你的网站流量突增,单台服务器已经不堪重负,甚至宕机。或者,你需要进行服务器维护,但又不想中断服务。在这些情况下,高可用架构就显得至关重要。今天,我们就来聊聊如何使用 LVS (Linux Virtual Server) 和 Keepalived 构建一个高可用的 Linux 服务器集群。
什么是LVS和Keepalived?
简单来说,LVS 是一个基于内核的负载均衡器,负责将客户端的请求分发到后端的多台真实服务器上。而 Keepalived 则是一个高可用解决方案,它通过 VRRP (Virtual Router Redundancy Protocol) 协议来实现主备服务器之间的故障转移。当主服务器出现故障时,备服务器会自动接管,从而保证服务的连续性。
LVS的工作模式
LVS 提供了多种工作模式,常见的有:
- NAT (Network Address Translation): 客户端请求到达 LVS 服务器,LVS 服务器将请求转发给后端服务器,后端服务器处理完请求后,将响应包发送回 LVS 服务器,LVS 服务器再将响应包发送给客户端。这种模式配置简单,但性能相对较低。
- DR (Direct Routing): 客户端请求到达 LVS 服务器,LVS 服务器将请求转发给后端服务器,后端服务器处理完请求后,直接将响应包发送给客户端。这种模式性能较高,但需要后端服务器配置 VIP (Virtual IP)。
- TUN (Tunneling): 客户端请求到达 LVS 服务器,LVS 服务器将请求封装成 IP 隧道报文,然后转发给后端服务器,后端服务器解封装后处理请求,并将响应包发送回客户端。
在实际应用中,DR 模式由于其高性能而被广泛使用。我们将在后续的配置示例中使用 DR 模式。
Keepalived的工作原理
Keepalived 的核心是 VRRP 协议。VRRP 协议允许将多个路由器组成一个虚拟路由器,对外表现为一个 VIP (Virtual IP)。在集群中,有一台服务器被选为 Master,负责响应客户端的请求。其他服务器则处于 Backup 状态,监听 Master 的状态。当 Master 出现故障时,Backup 服务器会通过选举算法选出一个新的 Master,接管 VIP,从而保证服务的连续性。
实战配置:LVS+Keepalived (DR模式)
接下来,我们通过一个实例来演示如何配置 LVS 和 Keepalived。假设我们有三台服务器:
- LVS Master: 192.168.1.100
- LVS Backup: 192.168.1.101
- Real Server 1: 192.168.1.102
- Real Server 2: 192.168.1.103
- VIP: 192.168.1.200
1. 安装 LVS 和 Keepalived
在 LVS Master 和 LVS Backup 服务器上安装 LVS 和 Keepalived:
sudo apt update
sudo apt install ipvsadm keepalived -y
2. 配置 LVS Master
编辑 LVS Master 服务器上的 /etc/keepalived/keepalived.conf 文件:
global_defs {
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER
interface eth0 # 修改为你的网卡名称
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200/24 # VIP
}
#LVS 配置
virtual_server 192.168.1.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 300
protocol TCP
real_server 192.168.1.102 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.103 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
}
3. 配置 LVS Backup
编辑 LVS Backup 服务器上的 /etc/keepalived/keepalived.conf 文件:
global_defs {
router_id LVS_BACKUP
}
vrrp_instance VI_1 {
state BACKUP
interface eth0 # 修改为你的网卡名称
virtual_router_id 51
priority 90 # 优先级低于 Master
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200/24 # VIP
}
#LVS 配置
virtual_server 192.168.1.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 300
protocol TCP
real_server 192.168.1.102 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.103 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
}
4. 配置 Real Server
在 Real Server 1 和 Real Server 2 上,需要配置 VIP 地址,并禁用 ARP 请求:
# 添加 VIP 到 lo 接口
sudo ip addr add 192.168.1.200/32 dev lo
# 禁用 ARP 请求
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce