仓酷云
标题:
带来一篇CentOS下Keepalived+LVS完成高可用集群
[打印本页]
作者:
再见西城
时间:
2015-1-14 20:47
标题:
带来一篇CentOS下Keepalived+LVS完成高可用集群
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!
操作体系平台:CentOS5.2
软件:LVS+keepalived
LVS+Keepalived引见
LVS
LVS是LinuxVirtualServer的简写,意即Linux虚拟办事器,是一个虚拟的办事器集群体系。本项目在1998年5月由章文嵩博士成立,是中国国际最早涌现的自在软件项目之一。今朝有三种IP负载平衡技巧(VS/NAT、VS/TUN和VS/DR);
十种调剂算法(rrr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq)。
Keepalvied
Keepalived在这里重要用作RealServer的安康状况检讨和LoadBalance主机和BackUP主机之间failover的完成
IP设置装备摆设信息:
LVS-DR-Master192.168.2.166
LVS-DR-BACKUP192.168.2.167
LVS-DR-VIP192.168.2.170
WEB1-Realserver192.168.2.171
WEB2-Realserver192.168.2.172
GateWay192.168.2.253
装置LVS和Keepalvied软件包
1.下载相干软件包
#mkdir/usr/local/src/lvs
#cd/usr/local/src/lvs
#wget
http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#wget
http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2.装置LVS和Keepalived
#lsmod|grepip_vs
#uname-r
2.6.18-53.el5PAE
#ln-s/usr/src/kernels/2.6.18-53.el5PAE-i686//usr/src/linux
#tarzxvfipvsadm-1.24.tar.gz
#cdipvsadm-1.24
#make&&makeinstall
#find/-nameipvsadm#检查ipvsadm的地位
#tarzxvfkeepalived-1.1.15.tar.gz
#cdkeepalived-1.1.15
#./configure&&make&&makeinstall
#find/-namekeepalived#检查keepalived地位
#cp/usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
#cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
#mkdir/etc/keepalived
#cp/usr/local/etc/keepalived/keepalived.conf/etc/keepalived/
#cp/usr/local/sbin/keepalived/usr/sbin/
#servicekeepalivedstart|stop#做成体系启动办事便利治理.
四.设置装备摆设LVS完成负载平衡
1.LVS-DR,设置装备摆设LVS剧本完成负载平衡
vi/usr/local/sbin/lvs-dr.sh
#!/bin/bash
#description:startLVSofDirectorServer
#Writtenby:NetSeek
http://www.linuxtone.org
GW=192.168.2.253
#websitedirectorvip.
WEB_VIP=192.168.2.170
WEB_RIP1=192.168.2.171
WEB_RIP2=192.168.2.172
./etc/rc.d/init.d/functions
logger$0calledwith$1
case"$1"in
start)
#Clearalliptablesrules.
/sbin/iptables-F
#Resetiptablescounters.
/sbin/iptables-Z
#Clearallipvsadmrules/services.
/sbin/ipvsadm-C
#setlvsvipfordr
/sbin/ipvsadm--set30560
/sbin/ifconfigeth0:0$WEB_VIPbroadcast$WEB_VIPnetmask255.255.255.255up
/sbin/routeadd-host$WEB_VIPdeveth0:0
/sbin/ipvsadm-A-t$WEB_VIP:80-swrr-p3
/sbin/ipvsadm-a-t$WEB_VIP:80-r$WEB_RIP1:80-g-w1
/sbin/ipvsadm-a-t$WEB_VIP:80-r$WEB_RIP2:80-g-w1
touch/var/lock/subsys/ipvsadm>/dev/null2>&1
#setArp
/sbin/arping-Ieth0-c5-s$WEB_VIP$GW>/dev/null2>&1
;;
stop)
/sbin/ipvsadm-C
/sbin/ipvsadm-Z
ifconfigeth0:0down
routedel$WEB_VIP>/dev/null2>&1
rm-rf/var/lock/subsys/ipvsadm>/dev/null2>&1
/sbin/arping-Ieth0-c5-s$WEB_VIP$GW
echo"ipvsadmstoped"
;;
status)
if[!-e/var/lock/subsys/ipvsadm];then
echo"ipvsadmisstoped"
exit1
else
ipvsadm-ln
echo"..........ipvsadmisOK."
fi
;;
*)
echo"Usage:$0{start|stop|status}"
exit1
esac
exit0
2.设置装备摆设Realserver剧本.
#!/bin/bash
#WrittenbyNetSeek
#description:Configrealserverloandapplynoarp
WEB_VIP=192.168.2.170
./etc/rc.d/init.d/functions
case"$1"in
start)
ifconfiglo:0$WEB_VIPnetmask255.255.255.255broadcast$WEB_VIP
/sbin/routeadd-host$WEB_VIPdevlo: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/null2>&1
echo"RealServerStartOK"
;;
stop)
ifconfiglo:0down
routedel$WEB_VIP>/dev/null2>&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"RealServerStoped"
;;
status)
#StatusofLVS-DRrealserver.
islothere=`/sbin/ifconfiglo:0|grep$WEB_VIP`
isrothere=`netstat-rn|grep"lo:0"|grep$web_VIP`
if[!"$islothere"-o!"isrothere"];then
#Eithertherouteorthelo:0device
#notfound.
echo"LVS-DRrealserverStopped."
else
echo"LVS-DRRunning."
fi
;;
*)
#Invalidentry.
echo"$0:Usage:$0{start|status|stop}"
exit1
;;
esac
exit0
附上realserver机上的/etc/sysctl.conf:
#KernelsysctlconfigurationfileforRedHatLinux
#
#Forbinaryvalues,0isdisabled,1isenabled.Seesysctl(8)and
#sysctl.conf(5)formoredetails.
#ControlsIPpacketforwarding
net.ipv4.ip_forward=1
#Controlssourcerouteverification
net.ipv4.conf.default.rp_filter=1
#Donotacceptsourcerouting
net.ipv4.conf.default.accept_source_route=0
#ControlstheSystemRequestdebuggingfunctionalityofthekernel
kernel.sysrq=0
#ControlswhethercoredumpswillappendthePIDtothecorefilename.
#Usefulfordebuggingmulti-threadedapplications.
kernel.core_uses_pid=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
或许采取secondaryipaddress方法设置装备摆设
#vi/etc/sysctl.conf
添加以下内容如上所示:
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.lo.arp_announce=2
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
#sysctlCp
#ipaddradd61.164.122.8/32devlo
#ipaddlist检查能否绑定
3.启动lvs-dr剧本和realserver启本,在DR上可以检查LVS以后状况:
#watchipvsadmCln
五.应用Keepalvied完成负载平衡和和高可用性
1.设置装备摆设在主负载平衡办事器上设置装备摆设keepalived.conf
#vi/etc/keepalived/keepalived.conf(主调剂器)
!ConfigurationFileforkeepalived
global_defs{
notification_email{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from
Alexandre.Cassen@firewall.loc
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_instanceVI_1{
stateMASTER
interfaceeth0
virtual_router_id51
priority100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.2.170
}
}
virtual_server192.168.2.17080{
delay_loop6
lb_algowrr
lb_kindDR
persistence_timeout60
protocolTCP
real_server192.168.2.17180{
weight3
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
real_server192.168.2.17280{
weight3
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
}
在备用调剂器上:
#vi/etc/keepalived/keepalived.conf(备用调剂器)
!ConfigurationFileforkeepalived
global_defs{
notification_email{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from
Alexandre.Cassen@firewall.loc
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_instanceVI_1{
stateBACKUP
interfaceeth0
virtual_router_id51
priority99
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.2.170
}
}
virtual_server192.168.2.17080{
delay_loop6
lb_algowrr
lb_kindDR
persistence_timeout60
protocolTCP
real_server192.168.2.17180{
weight3
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
real_server192.168.2.17280{
weight3
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
}
2.BACKUP办事器同上设置装备摆设,先装置lvs再按装keepalived,仍后设置装备摆设/etc/keepalived/keepalived.conf,只需将白色标示的部门改一下便可.
3.vi/etc/rc.local
#/usr/local/sbin/lvs-dr.sh将lvs-dr.sh这个剧本正文失落。
#/usr/local/sbin/lvs-dr.shstop停滞lvs-dr剧本
#/etc/init.d/keepalivedstart启动keepalived办事,keepalived就可以应用keepalived.conf配
置文件,完成负载平衡和高可用.
4.检查lvs办事能否正常
#watchipvsadmCln
IPVirtualServerversion1.2.1(size=4096)
ProtLocalAddressortSchedulerFlags
->RemoteAddressortForwardWeightActiveConnInActConn
TCP61.164.122.8:80wrrpersistent60
->61.164.122.10:80Route300
->61.164.122.9:80Route300
复制代码
#tailCf/var/log/message监听日记,检查状况,测试LVS负载平衡及高可用性能否有用。
5.停Master办事器的keepalived办事,检查BAKCUP办事器能否能正常接收办事。
四.相干参考
1.LVS基本常识汇总
LVS的算法引见
http://www.linuxtone.org/viewthread.php?tid=69
进修LVS的三种转发形式
http://www.linuxtone.org/viewthread.php?tid=77
LVS中的IP负载平衡技巧
http://www.linuxtone.org/viewthread.php?tid=68
更多的请到
http://www.linuxtone.org
负载平衡版检查
Keepalived相干参考材料。
http://www.keepalived.org/documentation.html
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!
作者:
深爱那片海
时间:
2015-1-16 23:06
标题:
带来一篇CentOS下Keepalived+LVS完成高可用集群
以前觉得Linux就跟dos一样,全是用命令窗口,相对于窗口界面来说多麻烦呀。
作者:
仓酷云
时间:
2015-1-26 07:05
对Linux命令熟悉后,你可以开始搭建一个小的Linux网络,这是最好的实践方法。Linux是网络的代名词,Linux网络服务功能非常强大,不论是邮件服务器、Web服务器、DNS服务器等都非常完善。
作者:
只想知道
时间:
2015-2-4 20:26
在学习linux的工程中,linux学习方法有很多种,这里是小编的学习心得,给大家拿出来分享一下。
作者:
第二个灵魂
时间:
2015-3-1 09:22
直到学习Linux这门课以后,我才知道,原来我错了。?
作者:
兰色精灵
时间:
2015-3-10 17:59
即便是非英语国家的人发布技术文档,Linux也都首先翻译成英语在国际学术杂志和网络上发表。
作者:
海妖
时间:
2015-3-17 12:12
掌握硬件配置,如显卡,声卡,网卡等,硬件只要不是太老或太新一般都能被支持,作为一名Linux系统管理员建议多阅读有关硬件配置文章,对各种不支持或支持不太好的硬件有深刻的了解。
作者:
因胸联盟
时间:
2015-3-24 15:14
熟读Linux系统有关知识,如系统目录树,有关内容可购书阅读或搜索论坛。
欢迎光临 仓酷云 (http://ckuyun.com/)
Powered by Discuz! X3.2