网上看了许多例子,都有些小毛病,小笔误导致不能配置成功,我再REMIX一版希望对后来者有帮助。
LVS:软负载均衡软件,在网络7层中第4层进行转发,效率很高
KeepLived:通过VRRP协议,选举中一台MASTER机器使用设定的虚IP,并且在MASTER掉线时,BACKUP机器自动托管该虚IP,对外界透明,外界只和虚IP进行通讯
安装软件:
yum install ipvsadm
yum install keepalived
相关文件位置:
keepalived{
配置文件:
/etc/keepalived/keepalived.conf
LOG文件:/var/log/messages
}
keepalived配置文件样例:
========
====
====
====
keepalived.conf (MASTER)
============
====
====
global_defs
{
notification_email
{
admin@company.com
}
notification_email_from
admin@company.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_NODE_A # MASTER & BACKUP结点该ID可以不同
}
vrrp_sync_group lb
{
group
{
loadbalance
}
}
vrrp_instance
loadbalance
{
state MASTER # MASTER, BACKUP
interface eth0
virtual_router_id 51 # master & backup must have same value
priority 100
advert_int 1
authentication
{
auth_type PASS
auth_pass 1111
}
virtual_ipaddress
{
192.168.1.100 # Virtual IP Address
}
}
virtual_server
192.168.1.100 8080 {
delay_loop 5
lb_algo wlc
lb_kind DR # NAT, TUN, DR
persistence_timeout 300
protocol TCP # UDP, TCP
real_server
192.168.1.228 8080
{
weight 100
TCP_CHECK
{
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8080
}
}
real_server
192.168.1.229 8080
{
weight 100
TCP_CHECK
{
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 8080
}
}
}
【P1】
{
执行相关命令:
启动keepalived:/etc/init.d/keepalived start 或者 service keepalived start/stop/restart
设置自启动:chkconfig keepalived on
查看ipvs规则是否生效:ipvsadm -L -n 会显示类似下面结果
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.33:80 wlc persistent 50
-> 192.168.2.29:80 Route 100 0 0
-> 192.168.2.30:80 Route 100 0 0
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.2.33:80 wlc persistent 50
-> 192.168.2.29:80 Route 100 0 0
-> 192.168.2.30:80 Route 100 0 0
}
第二台LVS机器,安装完软件后,使用下面配置文件
========
====
====
====
keepalived.conf (BACKUP)
============
====
====
拷贝MASTER配置文件
修改{
state => BACKUP
priority => 90
}
执行
【P1】
命令段
测试部分
测试主备切换功能
在MASTER上,执行 ip addr 结果表明MASTER服务器拥有虚IP地址
在BACKUP上,执行 ip addr 结果表明BACKUP服务器不拥有虚IP地址
在MASTER上执行 service keepalived stop
在MASTER上,执行 ip addr 结果表明MASTER服务器不拥有虚IP地址
在BACKUP上,执行 ip addr 结果表明BACKUP服务器拥有虚IP地址
在MASTER上执行 service keepalived start
在MASTER上,执行 ip addr 结果表明MASTER服务器拥有虚IP地址
在BACKUP上,执行 ip addr 结果表明BACKUP服务器不拥有虚IP地址
配置WEB服务器(在keepalived概念中,web服务器也叫 real server)
例子中采用DR方式调度(LVS中的概念,查LVS中文站点可知其详细意义)
在Real Server上需要进行配置,
即在每个Real Server上都需要进行相同的配置
1,编写lvsrs脚本放置/etc/init.d下面:vi /etc/init.d/lvsrs
脚本文件内容:
================================================================
#!/bin/bash
# description: Config realserver lo and apply noarp
SNS_VIP=
192.168.1.100
sh /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
================================================================
2,加权限 chmod 755 /etc/init.d/lvsrs
3,启动 service lvsrs start
4,加入自启动 echo "service lvsrs start" >> /etc/rc.local
FQA
Q: 两台LVS主机都变成了MASTER,导致无法使用虚IP进行方法,俗称「脑裂」现象,怎么回事?
A: 这是由于没有关闭防火墙的缘故。LVS+KeepAlived原理是使用VRRP协议进行互相通讯,默认防火墙会进行拦截。
关闭防火墙即可解决该问题。
不关闭防火墙的解决办法如下:
/sbin/iptables -A INPUT -i eth0 -d 224.0.0.0/8 -j ACCEPT
/sbin/iptables -A INPUT -i eth0 -p 112 -j ACCEPT
service iptables save
service iptables restart
Q: 为什么访问不了Real Server页面呢?
A: 因为你忘记了在Real Server上运行lvsrs脚本了,DR模式下Response不通过LVS转发,而直接将Response发给客户端,所以在RealServer上需要配置识别虚IP