LVS+Keepalived实现负载均衡+高可用性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVS+Keepalived实现负载均衡+高可用性相关的知识,希望对你有一定的参考价值。

一、实验环境
LVS-DR-MASTER:10.0.0.1
LVS-DR-BACKUP:10.0.0.5
LVS-DR-VIP:10.0.0.200
LVS-RS1:10.0.0.2
LVS-RS2:10.0.0.3
LVS-RS4:10.0.0.4
技术分享
二、实验原理
LVS使用RD模式做负载均衡,keepalived做高可用性+资源健康检查,其中LVSDR端配置由keepalived配置完成,无需单独编写配置脚本。
三、实验步骤
在LVS-DR-MASTER和LVS-DR-BACKUP上均安装ipvsadm和keepalived
yum install -y ipvsadm keepalived

编辑LVS-DR-MASTER上的keepalived配置文件/etc/keepalived/keepalived.conf (LVS-DR-BACKUP上的配置与MASTER上的基本一致,只需要修改两处:修改state为BACKUP,修改priority小于MASTER即可。)

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
 
#定义VIP1
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 {
10.0.0.200
}
}
 
virtual_server 10.0.0.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
 
real_server 10.0.0.2 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
 
real_server 10.0.0.3 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
 
real_server 10.0.0.4 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
分别启动MASTER和BACKUP上的keepalived程序
service keepalived start
在RealServer上编写配置文件(注意要先配置ARP忽略再配置网卡)
[[email protected] ~]# vim /etc/init.d/lvsdr_rs
 
#!/bin/bash
 
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
 
VIP=10.0.0.200
ifconfig lo:0 down
ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
route add -host $VIP dev lo:0
执行lvsdr_rs脚本
[[email protected] ~]# chmod +x /etc/init.d/lvsdr_rs
[[email protected] ~]# /etc/init.d/lvsdr_rs
四、测试效果
高可用性:正常情况下,VIPMASTER上,当MASTER出现问题时,VIP会漂移到BACKUP上,如果MASTER恢复正常,VIP会重新漂移到MASTER上。
负载均衡:keepalived会定时检查资源的可用性,当资源不可用时,keepalived会自动剔除这个资源,当资源恢复可用后,keepalived会自动把资源添加回资源池。
结论:高可用性和负载均衡工作正常,OK!!!
 
 

 

以上是关于LVS+Keepalived实现负载均衡+高可用性的主要内容,如果未能解决你的问题,请参考以下文章

LVS+Keepalived实现负载均衡+高可用性

LVS基础及LVS+Keepalived实现双主高可用负载均衡

lvs+keepalived实现高可用负载均衡

LVS/DR + keepalived负载均衡高可用实现

LVS(DR) + Keepalived 实现负载均衡

lvs+keepalived 高可用负载均衡模式分析