上篇keepalived+MySQL双主实现高可用性介绍了使用Keepalived配置MySQL透明高可用的方法,本文趁热打铁,实现Nginx+Keepalived的高可用性架构,相比MySQL的高可用更加简单。Nginx的配置方法可以参考Nginx基本服务与upstream配置详解,并使用里面的环境。其中debugo01和debugo02作为负载均衡,而debugo03和debugo04作为web server。
1. nginx监控脚本
该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix。
1 2 3 4 5 6 7 8 9 |
#!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/local/bin/nginx counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/keepalived stop fi fi |
2. 配置keepalived
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 |
vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 51 priority 100 advert_int 1 track_script { chk_nginx } authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.62.228.200 } } |
一个为MASTER,一个为BACKUP。启动debugo01和debugo02的keepalived
1 2 |
service keepalived start Starting keepalived: [ OK ] |
下面关闭nginx并占用nginx的80端口,阻止nginx顺利重启。通过debugo02的日志可以看出,状态和VIP已经成功切换
1 2 3 4 5 6 7 8 9 |
nginx -s stop && nc -l 80 #debugo02 tail -f /var/log/messages Jan 21 22:30:38 debugo02 Keepalived_vrrp[3842]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 21 22:30:39 debugo02 Keepalived_vrrp[3842]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 21 22:30:39 debugo02 Keepalived_vrrp[3842]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 21 22:30:39 debugo02 Keepalived_healthcheckers[3841]: Netlink reflector reports IP 10.62.228.200 added Jan 21 22:30:39 debugo02 Keepalived_vrrp[3842]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.62.228.200 Jan 21 22:30:44 debugo02 Keepalived_vrrp[3842]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth1 for 10.62.228.200 |
^^
参考:
详解利用Keepalived+Nginx解决站点高可用性
Pingback: Nginx+Keepalived实现站点高可用 - 月松博客月松博客
Pingback: 内部OA系统:Nginx+Keepalived实现站点高可用(1)-紫金星
Pingback: Nginx+Keepalived实现站点高可用 | 写代码度日的骚年
我这边就是KEEPALIVED 无法执行检测NGINX脚本
每次重启后都不会去启动NGINX
该脚本是ROOT用户,KEEPALIVE 也是ROOT用户启动.
人工执行该脚本是可以的.
是不是没有添加执行权限。
Pingback: Nginx+Keepalived实现站点高可用 | 张彬技术博客
这个更刺j激,准备好手纸哦 A 片。。 http://T.CN/RcWeTaH
烫衬衫和擦皮鞋都算是有用,叠被子真他 妈 机掰用都没有。
不老不懂这种情怀 不死不懂这点可惜