本文通过keepalived实现lvs服务器的的双机热备和真实服务器之间的负载均衡。这方面的blog挺多,但是每个人搭建集群的环境不同,遇到的问题也不同,本文简述配置的一些过程以及配置过程中遇到问题时的解决方案。
1. 创建Linux虚拟机 本文是基于Vmare虚拟环境搭建集群的,所以首先在Vmare中创建一些虚拟机(安装一个Linux系统,其他的复制第一次安装的即可),本文创建4台 虚拟机。网卡模式设置成桥接方式,这样可以保证虚拟机中的网络和主机IP在同一个网段,这样的好处是,给每台虚拟机设完IP之后,就可以通过同一网段的另 一台主机登陆到Linux,就不用在虚拟机之间来回切换了。(Linux发行版本为CentOS 6.6) 2. 配置yum库若本机没有yum库,最好配置安装yum库,方便软件的安装,yum库创建基本步骤
①安装createrepo
②yum -y installl createrepo
③用createrepo创建
createrepo命令用于生成yum仓库所需要的一些基本配置信息。
具体请参考“yum库的安装于配置”
在Vmare虚拟机中虚拟四个Linux主机
LvsMaster:192.168.10.101
LvsBak: 192.168.10.102
RealServer1: 192.168.10.121
RealServer2: 192.168.10.122
主机之间的拓扑关系图
yum -y install *ipvsadm*
下载地址:wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar
安装依赖库:yum install -y openssl openssl-devel
下载keepalived:wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
tar zxvf keepalived-1.2.6.tar.gz
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived /etc/keepalived.conf
LvsMaster配置
global_defs {
notification_email {
[email protected] #emial通知
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_sync_group test { #设置vrrp组
group {
loadbalance
}
}
vrrp_instance loadbalance {
state MASTER #设置主机类型,MASTER和BACKUP两种,必须大写 ,即太主机一台备用机
interface eth0 #设置对外服务网卡
virtual_router_id 51
priority 100 #设置优先级,备用主机的优先级要比主机低即可
advert_int 1#设置同步时间间隔
authentication {#设置验证类型和密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {#设置虚拟ip,即浮动ip
192.168.10.115
}
virtual_server 192.168.10.115 80 {
delay_loop 6 #健康检查时间间隔
lb_algo rr#负载调度算法,默认为轮训算法
lb_kind DR#负载均衡转发规则,也就是IP转发规则,DR转发方式最快,但是所有机器必须在一个网段内,还有NAT和TUNEL两种转发方式
protocol TCP
real_server 192.168.10.121 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.10.122 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
LvsBakr配置
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_sync_group test {
group {
loadbalance
}
}
vrrp_instance loadbalance {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.115
}
}
virtual_server 192.168.10.115 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.121 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.10.122 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
LvsMastrer: /etc/init.d/keepalived start
LvsBak : /etc/init.d/keepalived start
查看启动状态
LvsMaster: tail -f /var/log/messages
LvsBak: tail -f /var/log/messages
用ip addr和ipvsadm查看路由情况
LvsMaster: ip addr
LvsMaster: ipvsadm
LvsBak:ip addr
LvsBak:ipvsadm
RealServer1和ResalServer2的脚本设置相同,都是realserver.sh,设置脚本如下SNS_VIP=192.168.10.115
source /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP up
/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
脚本编辑完成后启动realserver.sh
RealServer1: ./realserver.sh start RealServer2: ./realserver.sh start查看IP
安装yum -y install httpd配置ServerName www.example.com NameVirtualHost 192.168.10.121:80 <VirtualHost 192.168.10.121:80>
DocumentRoot /var/www/html
ServerName www.example.com
</VirtualHost>
在 /var/www/hmtl/下添加index.html测试主页启动apache :/etc/init.d/httpd start
用ipvsadm 查看 LvsMaster 和 LvsBak的路由情况
LvsMaster: ipvsadmLvsBak: ipvsadm
① 配置keepalvied应注意的问题LvsMaster和LvsBak只有两处不同 state MASTER " BACKUP (主Lvs是MASTER,备用Lv是BACKUP)
priority 100 | 90 (主Lvs是100,备用LVS只要小于100即可)
② 要关闭iptables或者开启相应的服务端口,否则无法通过浏览器访问