keepalived是一个类似于layer3, 4, 5 交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
官网地址:http://www.keepalived.org/
截止2011-08-29 最新版本1.2.2 附件提供下载:keepalived-1.2.2.tar.gz
keepalived 中文权威指南:KEEPALIVED权威指南(中文)
本文目录:
- [一]、实验环境介绍
- [二]、服务器层的双机热备(比如服务器宕机、keepalived宕、网络不通)
- [三]、应用层(web)的双机热备(比如web应用端口不通、 nginx进程被意外kill )
[一]、实验环境介绍
1.1、软件信息:
- S-A 192.168.8.183 centos5 keepalived nginx
- S-B 192.168.8.150 centos5 keepalived nginx
- VirtualIP (虚拟IP):192.168.8.191
1.2、keepalived 标准的安装步骤:
1 2 3 4 5 6 7 |
# 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://www.micmiu.com/enterprise-app/cluster/nginx-install-lb/)
修改nginx.conf 中监听端口80->8088:
listen 8088;
server_name localhost;
修改/usr/local/nginx/html/index.html的内容:
192.168.8.183
1 2 3 4 5 6 7 8 |
<html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx! 192.168.8.183</h1></center> </body> </html> |
192.168.8.150:
1 2 3 4 5 6 7 8 |
<html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx! 192.168.8.150</h1></center> </body> </html> |
两台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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
! Configuration File for keepalived global_defs { notification_email { #admin@michael-demo.com } #notification_email_from admin@micmiu.com #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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
! Configuration File for keepalived global_defs { notification_email { #admin@michael-demo.com } #notification_email_from admin@micmiu.com #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:
- state 参数值:主的是MASTER、备用的是BACKUP
- priority 参数值: MASTER > BACKUP
- virtual_router_id: 参数值要一样
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的日志信息如下:
master :192.168.8.183
master :192.168.8.183
2.4.3、浏览器打开虚拟ip访问:http://192.168.8.191:8088/
2.4.4、kill 192.168.8.183(master) 的keepalived进程,日志信息如下:
master :192.168.8.183
backup:192.168.8.150
2.4.5、浏览器打开虚拟ip访问:http://192.168.8.191:8088/
2.4.6、再次启动192.168.8.183的keepalived进程,192.168.8.183会自动接管成为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.183)配置文件/etc/keepalived/keepalived.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { #admin@michael-demo.com } #notification_email_from admin@michael-demo.com #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8088" interval 1 weight -2 } 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 } track_script { chk_http_port } } |
3.2.2、Backup(S-B 192.168.8.150)配置文件/etc/keepalived/keepalived.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { #admin@michael-demo.com } #notification_email_from admin@michael-demo.com #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "</dev/tcp/127.0.0.1/8088" interval 1 weight -2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.8.191 } track_script { chk_http_port } } |
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、测试过程和上面本文第二部分的过程基本一致,本人测试均通过。
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/enterprise-app/server/keepalived-hot-standby/
script "</dev/tcp/127.0.0.1/8088"
interval 1
weight -2
很有收获~,我以前是用wget来实现的检测
哦,好吧,谢谢,打扰了。
楼主,我设置之后,两台机器日志显示都是 master。
比如说两台机器,A和B,A开启keepalived,然后B开启,似乎后开启的B就抢占了master,而且这个master如果掉了,虚拟IP就连不上了,但是在掉之前如果在A上再启一次keepalived,B掉就没有事了。
你遇到这种情况吗,为啥会这样?
没有遇到过这样的 只会一个master
你可以把配置文件 发给我看看
给你添麻烦了!
两台服务器192.168.21.168和169;虚拟IP192.168.21.167
当主服务断掉,备用为主后,修复好的主机器不抢占当前的备用的主状态。
主配置:
! Configuration File for keepalived
global_defs {
notification_email {
test@163.com
}
notification_email_from test@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.21.167
}
notify_master /usr/share/master.sh
notify_backup /usr/share/backup.sh
notify_fault /usr/share/fault.sh
notify_stop /usr/share/fault.sh
smtp_alert
}
virtual_server 192.168.21.167 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.21.168 3306 {
weight 3
notify_down /usr/share/mysql_stop.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
从配置:
! Configuration File for keepalived
global_defs {
notification_email {
test@163.com
}
notification_email_from test@163.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 50
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.21.167
}
notify_master /usr/share/master.sh
notify_backup /usr/share/backup.sh
notify_fault /usr/share/fault.sh
notify_stop /usr/share/fault.sh
smtp_alert
}
virtual_server 192.168.21.167 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server 192.168.21.169 3306 {
weight 3
notify_down /usr/share/mysql_stop.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
把两个配置的 state 都设置为:MASTER 试试