blog迁移至 :http://www.micmiu.com
keepalived是一个类似于layer3, 4, 5 交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
官网地址:http://www.keepalived.org/
截止目前最新版本1.2.2 附件提供下载:keepalived-1.2.2.tar.gz
keepalived 中文权威指南:KEEPALIVED权威指南_中文_.pdf
本文目录:
[一]、实验环境介绍
1.1、软件信息:
1.2、keepalived 标准的安装步骤:
# tar -zxvf keepalived-1.2.2.tar.gz # cd keepalived-1.2.2 # ./configure # make # make install # mkdir /etc/keepalived/ # cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
1.3、nginx的安装和keepalived类似
不熟悉的可参考:nginx安装及负载均衡配置 (http://sjsky.iteye.com/blog/1156195)
修改nginx.conf 中监听端口80->8088:
listen 8088; server_name localhost;
修改/usr/local/nginx/html/index.html的内容:
192.168.8.183
Welcome to nginx!
Welcome to nginx! 192.168.8.183
192.168.8.150:
Welcome to nginx!
Welcome to nginx! 192.168.8.150
两台server(S-A,S-B)都需要安装这两款软件。
[二]、服务器层的双机热备(比如服务器宕机、keepalived宕了)
2.1、测试要求:
主机宕机、keepalived宕了、网络不通时,备机能自动接管,不影响VIP:192.168.8.191的访问
2.2、Master(S-A 192.168.8.181)配置文件/etc/keepalived/keepalived.conf:
! Configuration File for keepalived global_defs { notification_email { #[email protected] } #notification_email_from [email protected] #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.8.191 } }
2.3、Backup(S-B 192.168.8.150)配置信息/etc/keepalived/keepalived.conf:
! Configuration File for keepalived global_defs { notification_email { #[email protected] } #notification_email_from [email protected] #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.8.191 } }
Tips:
2.4、测试:
两台测试机S-A、S-B均启动keepalived和nginx
2.4.1、验证nginx启动正常:
访问 master : http://192.168.8.183:8088/
访问 backup: http://192.168.8.150:8088/
2.4.2、查看keepalived的日志信息如下:
backup:192.168.8.150
2.4.3、浏览器打开虚拟ip访问:http://192.168.8.191:8088/
2.4.4、kill 192.168.8.181(master) 的keepalived进程,日志信息如下:
2.4.5、浏览器打开虚拟ip访问:http://192.168.8.191:8088/
2.4.6、再次启动192.168.8.181的keepalived进程,192.168.8.181会自动接管成为master,192.168.8.150自动转为backup,从测试结果看,备机能成功接管,已经实现了热备。
Tips: 如果我们停掉master上的nginx进程,你会发现此时应用访问就失败了,很显然这不是我们希望看到的结果,那如何实现web应用的热备呢,在下一节中我们将详细讲述。
[三]、应用层(web)的双机热备(比如nginx进程被意外kill、web端口不通)
3.1.主要是利用keepalived的track_script检测脚本的原理实现的。
3.2.配置文件的修改
3.2.1、Master(S-A 192.168.8.181)配置文件/etc/keepalived/keepalived.conf:
/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
#[email protected]
}
#notification_email_from [email protected]
#smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "
3.2.2、Backup(S-B 192.168.8.150)配置文件/etc/keepalived/keepalived.conf:
/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
#[email protected]
}
#notification_email_from [email protected]
#smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "
3.2.3、注意点(非常重要)
1)、为什么主备的参数state都是MASTER,对的你没有看错确实要都设置成一样的,不然并不能实现我们想要的VIP漂浮的效果,我测试很久才发现的.state都设置成MASTER后,会根据priority的值大小竞争来决定谁是真正的MASTER,脚本检测也是在失败的时候会把权重减去相应的值,比如原来master(181)的priority=100,如果脚本检测到端口8088无法连接,就会priority-2=98,< S-B(150)的priority(99),此时 S-B(150) 将竞争成为master,这样就实现了web应用的热备。
2)、配置文件格式一定要注意,比如:track_script {,在“track_script”和“{”之间一定要加空格 ,本来测试时少了个空格,一直无法运行检测脚本,调试了很久,非常郁闷
3.3、测试过程和上面的过程基本一致。
本文连接:http://sjsky.iteye.com/blog/1156403
转载请注明来自:Michael's blog @ http://sjsky.iteye.com
----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------