putaochengshu 2012-12-30
keepalived + nginx 实现负载均衡
keepalived用途
keepalived是一个类似于layer3,4&5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,
并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,
这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器
环境说明:centos6
192.168.1.102(keepalived==>nginx)master
192.168.1.132(keepalived==>nginx)slave
192.168.1.100虚拟ip
102安装如下:
首先安装keepalived
下载地址:http://www.keepalived.org/download.html
KeepalivedforLinux-Version1.2.7-August29,2012
下载到文件:keepalived-1.2.7.tar.gz
1.tar-xzvfkeepalived-1.2.7.tar.gz
2.
cdkeepalived-1.2.7
./configure--prefix=/home/soft/keepalived
错误1:
configure:error:
!!!OpenSSLisnotproperlyinstalledonyoursystem.!!!
!!!CannotincludeOpenSSLheadersfiles.!!!
解决:yum-yinstallopenssl-devel
错误2:
configure:error:Poptlibrariesisrequired
解决:yuminstallpopt-devel
如果编译成功:
Keepalivedconfiguration
------------------------
Keepalivedversion:1.2.7
Compiler:gcc
Compilerflags:-g-O2
ExtraLib:-lpopt-lssl-lcrypto
UseIPVSFramework:Yes
IPVSsyncdaemonsupport:Yes
IPVSuselibnl:No
UseVRRPFramework:Yes
UseVRRPVMAC:Yes
SNMPsupport:No
UseDebugflags:No
make
提示:
Makecomplete
make[1]:Leavingdirectory`/home/soft/source/keepalived-1.2.7/genhash'
makeinstall
3.配置keepalived
cp/home/soft/keepalived/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
cp/home/soft/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
mkdir/etc/keepalived
cp/home/soft/keepalived/sbin/keepalived/usr/sbin/
vi/etc/keepalived/keepalived.conf
global_defs{
notification_email{
}
smtp_serversmtp.163.com
#smtp_connect_timeout30
router_idLVS_DEVEL
}
#VIP1
vrrp_instanceVI_1{
stateMASTER#备份服务器上将MASTER改为BACKUP
interfaceeth0
lvs_sync_daemon_intefaceeth0
virtual_router_id51
priority100#备份服务上将100改为90
advert_int5
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.1.102
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server192.168.1.10280{
delay_loop6#(每隔10秒查询realserver状态)
lb_algowlc#(lvs算法)
lb_kindDR#(DirectRoute)
persistence_timeout60#(同一IP的连接60秒内被分配到同一台realserver)
protocolTCP#(用TCP协议检查realserver状态)
real_server192.168.1.13280{
weight100#(权重)
TCP_CHECK{
connect_timeout10#(10秒无响应超时)
nb_get_retry3
delay_before_retry3
connect_port80
}
}
real_server192.168.1.13380{
weight100
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
}
4.servicekeepalivedstart|stop
5.启动keepalived!,查看虚拟IP是否绑定!
ipaddr
6.nginx源码安装看nginx/nginx源码安装.txt
102配置完成
--------------------------------------
下面配置132
安装keepalived如上:
vi/etc/keepalived/keepalived.conf这里有变动,具体内容如下:
global_defs{
notification_email{
}
smtp_serversmtp.163.com
#smtp_connect_timeout30
router_idLVS_DEVEL_2
}
#VIP1
vrrp_instanceVI_1{
stateBACKUP#备份服务器上将MASTER改为BACKUP
interfaceeth0
lvs_sync_daemon_intefaceeth0
virtual_router_id51
priority90#备份服务上将100改为90
advert_int5
authentication{
auth_typePASS
auth_pass1111
}
virtual_ipaddress{
192.168.1.100
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server192.168.1.10080{
delay_loop6#(每隔10秒查询realserver状态)
lb_algowlc#(lvs算法)
lb_kindDR#(DirectRoute)
persistence_timeout60#(同一IP的连接60秒内被分配到同一台realserver)
protocolTCP#(用TCP协议检查realserver状态)
real_server192.168.1.10280{
weight100#(权重)
TCP_CHECK{
connect_timeout10#(10秒无响应超时)
nb_get_retry3
delay_before_retry3
connect_port80
}
}
real_server192.168.1.13280{
weight100
TCP_CHECK{
connect_timeout10
nb_get_retry3
delay_before_retry3
connect_port80
}
}
}
同样安装nginx
--------------------------
测试:102上执行servicekeepalivedstop
检测132是否接替任务