VIP:192.168.1.100
Master:192.168.1.238
Slave:192.168.1.239
真实IP:
RIP1:192.168.1.235
RIP2:192.168.1.236
以下为安装步骤第一步:安装haproxy下载http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.18.tar.gz
#tarzxvfhaproxy-1.4.18.tar.gz
#makeTARGET=
linux26
#makeinstall
第二步:安装ipvs根据ipvs官网介绍,不同
版本内核适用的ipvs版本不一样。http://www.linuxvirtualserver.org/software/ipvs.html
2.6.18适用于1.24的各个版本
ipvsadm-1.24-6.src.rpm(forkernelbetween2.6.10and2.6.27.4)-December10,2005
ipvsadm-1.24-5.src.rpm(for1.2.0orlater)-October27,2004
ipvsadm-1.24-4.src.rpm(for1.1.8orlater)-January10,2004
ipvsadm-1.24-3.src.rpm(for1.1.8orlater)-December20,2003
ipvsadm-1.24.tar.gz-December10,2005
安装之前,软连接系统内核目录
ln-s/usr/src/kernels/2.6.18-194.11.3.el5-i686//usr/src/linux
查看系统当前的内核版本:
uname-r
2.6.18-164.el5
有时你找不到这个内核的路径,系统安装后并没有kernerls的目录
解决方法:yuminstallkernel-devel
接下来在进行连接编译就ok了!
我们使用最新的ipvsadm-1.24.tar.gz,下载安装。
tarzxvfipvsadm-1.24.tar.gz
cdipvsadm-1.24
make
makeinstall
第三步,安装keepalived从http://www.keepalived.org上下载最新的keepalived-1.2.2.tar.gz。
tarzxvfkeepalived-1.2.2.tar.gz
cdkeepalived-1.2.2
./configure
make
makeinstall
安装完毕。配置
(1):
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/
ln-s/usr/local/sbin/keepalived/usr/sbin/
servicekeepalivedstart
如果安装时的编译参数为:./configure--prefix=/usr/local/keepalived
则操作步骤如下:
cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
mkdir/etc/keepalived
cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/
ln-s/usr/local/keepalived/sbin/keepalived/usr/sbin/
servicekeepalivedstart
以下为详细的
配置文件二、haproxy(1)、haproxy配置文件haproxy.cfg
global
daemon
chroot/usr/local/haproxy
maxconn256
userhaproxy
grouphaproxy
log-send-hostnamexiaokk.com
log-tagmy_xiaokk
nbproc1
pidfile/var/run/haproxy.pid
log127.0.0.1local0info
defaults
maxconn256
modehttp
timeoutconnect5000ms
timeoutclient50000ms
timeoutserver50000ms
timeoutcheck2000
retries3
optionhttpclose
optionhttplog
optiondontlognull
optionforwardfor
optionredispatch
optionabortonclose
frontendhttp-www
bind192.168.1.100:80
aclha_policyhdr_reg(host)-i^(www.test-haproxy.com|test-haproxy.com|demo.test-haproxy.com|www.hellobaby.com)
aclha_babyhdr_dom(host)-ihellobaby.com
use_backendha_wwwifha_policy
use_backendha_wwwifha_baby
log127.0.0.1local0info
listenadmin_status
bind192.168.1.100:1080
modehttp
log127.0.0.1local0info
statsuri/admin?status
statsrefresh30s
statsrealmHaproxy\Admin\Center
statsauthadmin:admin
statshide-version
backendha_www
modehttp
balancesource
cookieSERVERID
optionhttpchkHEAD/index.html
serverw1192.168.1.235:80cookie1checkinter1500rise3fall3weight1
serverw2192.168.1.236:85cookie1checkinter1500rise3fall3weight2
(2)、haproxy启却
脚本haproxy.sh
#!/bin/sh
#
#chkconfig:-8515
#description:HA-ProxyisaTCP/HTTPreverseproxywhichisparticularly\
#suitedforhighavailabilityenvironments.
#
#processname:haproxy
#config:/usr/local/haproxy/conf/haproxy.cfg
#pidfile:/var/run/haproyx.pid
#
#Sourcefunctionlibrary.
if[-f/etc/init.d/functions];then
./etc/init.d/functions
elif[-f/etc/rc.d/init.d/functions];then
./etc/rc.d/init.d/functions
else
exit0
fi
#Sourcenetworkingconfiguration.
./etc/sysconfig/network
#Checkthatnetworkingisup
[${NETWORKING}="no"]&&exit0
[-f/usr/local/haproxy/conf/haproxy.cfg]||exit1
RETVAL=0
#startHaproxy
start(){
/usr/local/haproxy/sbin/haproxy-c-q-f/usr/local/haproxy/conf/haproxy.cfg
if[$?-ne0];then
echo"Errorsfoundinconfigurationfile."
return1
fi
echo-n"StartingHaproxy:"
daemon/usr/local/haproxy/sbin/haproxy-D-f/usr/local/haproxy/conf/haproxy.cfg-p/var/run/haproxy.pid
RETVAL=$?
echo
[$RETVAL-eq0]&&touch/var/lock/subsys/haproxy
return$RETVAL
}
#stopHaproxy
stop(){
echo-n"ShuttingdownHaproxy:"
killprochaproxy-USR1
RETVAL=$?
echo
[$RETVAL-eq0]&&rm-f/var/lock/subsys/haproxy
[$RETVAL-eq0]&&rm-f/var/run/haproxy.pid
return$RETVAL
}
#restartHaproxy
restart(){
/usr/local/haproxy/sbin/haproxy-c-q-f/usr/local/haproxy/conf/haproxy.cfg
if[$?-ne0];then
echo"ERRfoundinconfigurationfile,checkitwith'haproxycheck'."
return1
fi
stop
start
}
#checkhaproxy
check(){
/usr/local/haproxy/sbin/haproxy-c-q-V-f/usr/local/haproxy/conf/haproxy.cfg
}
#rsstatus
rhstatus(){
statushaproxy
}
#condrestart
condrestart(){
[-e/var/lock/subsys/haproxy]&&restart||:
}
#Seehowwewerecalled.
case"$1"in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
restart
;;
condrestart)
condrestart
;;
status)
rhstatus
;;
check)
check
;;
*)
echo$"Usage:haproxy{start|stop|restart|reload|condrestart|status|check}"
RETVAL
esac
exit$RETVAL
三、keepalived配置文件
Master的配置文件:
!ConfigurationFileforkeepalived
global_defs{
router_idLVS_1
}
vrrp_scriptchk_haproxy{
script"/etc/keepalived/check_haproxy.sh"
interval2
weight2
}
vrrp_instanceVI_1{
stateMASTER
priority99
advert_int1
virtual_router_id50
garp_master_delay1
interfaceeth0
authentication{
auth_typePASS
auth_passKxiaokk345Pix
}
track_interface{
eth0
}
virtual_ipaddress{
192.168.1.100
}
track_script{
chk_haproxy
}
notify_master"/etc/keepalived/mailnotify.pymaster"
notify_backup"/etc/keepalived/mailnotify.pybackup"
notify_fault"/etc/keepalived/mailnotify.pyfault"
}
Slave的配置文件
!ConfigurationFileforkeepalived
global_defs{
router_idLVS_2
}
vrrp_scriptchk_haproxy{
script"/etc/keepalived/check_haproxy.sh"
interval5
weight2
}
vrrp_instanceVI_1{
stateBACKUP
priority96
advert_int1
virtual_router_id50
garp_master_delay1
interfaceeth0
authentication{
auth_typePASS
auth_passKxiaokk345Pix
}
track_interface{
eth0
}
virtual_ipaddress{
192.168.1.100
}
track_script{
chk_haproxy
}
notify_master"/etc/keepalived/mailnotify.pymaster"
notify_backup"/etc/keepalived/mailnotify.pybackup"
notify_fault"/etc/keepalived/mailnotify.pyfault"
}
######---haproxy存活状态检测脚本
#!/bin/bash
#
#desc:checkhaproxyservice
#
A=`ipaddressshoweth0|grep192.168.1.100|wc-l`
B=`ps-Chaproxy--no-heading|wc-l`
if[$A-eq1];then
if[$B-eq0];then
/usr/local/haproxy/haproxy.shstart
sleep3
fi
fi
######----
主备切换时邮件提醒程序(此程序源自litus,有小改动,调试通过)
#!/usr/bin/python
#coding:utf-8
fromemail.MIMEMultipartimportMIMEMultipart
fromemail.MIMETextimportMIMEText
fromemail.MIMEImageimportMIMEImage
fromemail.HeaderimportHeader
importsys
importsmtplib
#-------------------------------
#file:mailnotify.py
#desc:sendnotifyemail
#
#-------------------------------
strFrom='toplover@sina.com'
strTo='toplover@126.com'
smtp_server='smtp.sina.com'
smtp_user='toplover'
smtp_pass='xxx***xxx'
ifsys.argv[1]!="master"andsys.argv[1]!="backup"andsys.argv[1]!="fault":
sys.exit()
else:
notify_type=sys.argv[1]
mail_title="[crt]Haproxy-notify-info"
mail_body_plain=notify_type+'alive,pleasecheckitnow'
mail_body_html='<b><fontcolor=red>'+notify_type+'alive,pleasecheckitnow'
msgRoot=MIMEMultipart('related')
msgRoot['Subject']=Header(mail_title,'utf-8')
msgRoot['From']=strFrom
msgRoot['To']=strTo
msgAlternative=MIMEMultipart(' alternative')
msgRoot.attach(msgAlternative)
msgText=MIMEText(mail_body_plain,'plain','utf-8')
msgAlternative.attach(msgText)
msgText=MIMEText(mail_body_html,'html','utf-8')
msgAlternative.attach(msgText)
smtp=smtplib.SMTP()
smtp.connect(smtp_server)
smtp.login(smtp_user,smtp_pass)
smtp.sendmail(strFrom,strTo,msgRoot.as_string())
smtp.quit()
到此完成了简单的haproxy+keepalived
负载均衡主从热备功能。
不明之处请参阅haproxy官方cofiguration.txt文件及keepalived官方文档。