共计 4743 个字符,预计需要花费 12 分钟才能阅读完成。
高可用负载均衡之 LVS+Keepalived
一、LVS+Keepalived 介绍:
LVS是 Linux Virtual Server 的简称,也就是 Linux 虚拟服务器,通过 LVS 提供的负载均衡技术和 Linux 操作系统实现一个高性能、高可用的服务器群集,它具有良好可靠性、可扩展性和可操作性。从而以低廉的成本实现最优的服务性能。
Keepalived就是一个类似于 3~5 层交换机制的软件,具有 3~5 层交换功能,其主要作用是检测 web 服务器的健康状态,如果某台 web 服务器故障,Keepalived将检测到并将其从系统中剔除,当该 web 服务器工作正常后 Keepalived 自动将其加入到服务器群中,这些工作全部自动完成,而不需要人工干预,只需要人工修复故障的 web 服务器即可。
二、搭建 LVS(DR 模式)+ Keepalived 高可用
1. 服务器 IP 规划:
Master DIP(192.168.126.1) VIP(192.168.126.5)
Backup DIP(192.168.126.2) VIP(192.168.126.5)
RIP (192.168.126.3) VIP(192.168.126.5)
RIP (192.168.126.3) VIP(192.168.126.5)
2.MASTER配置部分:
编译安装LVS:Make && make install
报错:make[1]: ***[libipvs.o] Error 1
make: ***[libs] Error 2
解 决:yum installlibnl*
编译安装Keepalived
./configure–prefix=/usr/local/keepalived
cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
配置在主负载均衡服务器上配置keepalived.conf
#vi /etc/keepalived/keepalived.conf (master)
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.132/32 dev eth0 label eth0:0
}
}
virtual_server 192.168.126.132 80 {
delay_loop 6 #(每隔 6 秒查询 realserver 状态)
lb_algo rr #(lvs 算法)
lb_kind DR #(Direct Route)
nat_mask 255.255.255.0 #(同一 IP 的连接 60 秒内被分配到同一台realserver)
protocol TCP #(用 TCP 协议检查 realserver 状态)
real_server 192.168.126.131 80 {
weight 1 #(权重)
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.126.149 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
ipvsadm–C 清除所有规则
ipvsadm-A -t 192.168.126.1132:80 -s wlc
ipvsadm-a -t 192.168.126.132:80 -r 192.168.126.131 -g -w 2
ipvsadm-a -t 192.168.126.132:80 -r 192.168.126.149 -g -w 1
ipvsadm-L –n 查看规则
ipvsadm–S 保存规则
3.BACKUP配置部分:
编译安装LVS:Make && make install
报错:make[1]: ***[libipvs.o] Error 1
make: ***[libs] Error 2
解 决:yum installlibnl*
编译安装Keepalived
./configure–prefix=/usr/local/keepalived
cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp/usr/local/keepalived/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
配置在主负载均衡服务器上配置 keepalived.conf
#vi /etc/keepalived/keepalived.conf (备调 度器Backup)
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.170/32 eth0 label eth0:0
}
}
virtual_server 192.168.126.13280 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.126.131 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.126.149 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
ipvsadm–C 清除所有规则
ipvsadm -A -t 192.168.126.1132:80 -s wlc
ipvsadm -a -t 192.168.126.132:80 -r 192.168.126.131 -g -w 2
ipvsadm -a -t 192.168.126.132:80 -r 192.168.126.149 -g -w 1
ipvsadm -L –n 查看规则
ipvsadm–S 保存规则
4.Real-Server1配置部分:
Echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
Echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
Echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_arp_announce
Echo 2 > /proc/sys/net/ipv4/conf/all/arp_arp_announce
限定 linux 主机对 arp 广播请求的响应级别,以及向外通告自己的 IP 地址的,通告级别!
配置虚拟IP:
ifconfiglo:0 192.168.100.1(VIP) netmask 255.255.255.255 broadcast 192.168.1.255(广播地址) up
Routeadd –host ip(vip) dev eth0:0
RS上要做路由,要是不做路由响应客户端时会用 eth0 响应,而不是VIP
Service httpd restart
Echo“this isreal-server1”> /var/www/html/index.html
开启路由转发
5.Real-Server2配置部分:
Echo 1 >/proc/sys/net/ipv4/conf/eth0/arp_ignore
Echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
Echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_arp_announce
Echo 2 > /proc/sys/net/ipv4/conf/all/arp_arp_announce
限定 linux 主机对 arp 广播请求的响应级别,以及向外通告自己的 IP 地址的,通告级别!
配置虚拟IP:
ifconfiglo:0 192.168.100.1(VIP) netmask 255.255.255.255 broadcast 192.168.1.255(广播地址) up
Routeadd –host ip(vip) dev eth0:0
RS上要做路由,要是不做路由响应客户端时会用 eth0 响应,而不是VIP
Service httpdrestart
Echo“this isreal-server1”> /var/www/html/index.html
开启路由转发
注意:Rlear-Server限定 linux 主机对 arp 广播请求的响应级别后在配置VIP
Keepalived.conf配置文件定要 copy 到/etc/目录下
Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里
推荐阅读:
CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm
Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm
Keepalived+Haproxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm
Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm