注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

冷月寒星

 
 
 

日志

 
 

Nginx+keepalived双机热备  

2010-09-01 22:56:31|  分类: 网站架构 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Nginx Master: 192.168.128.134

Nginx Backup: 192.168.128.138

VIP:192.168.128.177

Nginx安装见nginx安装配置文档

Master与Backup的Nginx配置一致.

在MASTER跟BACKUP上安装Keepalived:

#tar zxvf keepalived-1.1.15.tar.gz -C ../software

#cd ../software/keepalived-1.1.15

#./configure –prefix=/usr/local/keepalived && make && make install

#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/

#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

#service keepalived start|stop #做成系统启动服务方便管理.

修改MASTER 的keepalived.conf

# /etc/keepalived/keepalived.conf

vrrp_script chk_http_port {

script "/usr/local/scripts/nginx_pid.sh" ###监控脚本

interval 2 ###监控时间

weight 2

}

vrrp_instance VI_1 {

state MASTER ### 设置为 主

interface eth0 ### 监控网卡

virtual_router_id 51 ### 这个两台服务器必须一样

priority 101 ### 权重值 MASTRE 一定要高于 BAUCKUP

authentication {

auth_type PASS ### 加密

auth_pass test ### 加密的密码,两台服务器一定要一样,不然会出错

}

track_script {

chk_http_port ### 执行监控的服务

}

virtual_ipaddress {

192.168.128.177 ### VIP 地址

}

}

修改BACKUP keepalived.conf

# /etc/keepalived/keepalived.conf

vrrp_script chk_http_port {

script "/opt/nginx_pid.sh"

interval 2

weight 2

}

vrrp_instance VI_1 {

state BACKUP ### 设置为备份机

interface eth0

virtual_router_id 51 ### 与 MASTRE 设置 值一样

priority 80 ### 比 MASTRE权重值 低

authentication {

auth_type PASS

auth_pass test ### 密码 与 MASTRE 一样

}

track_script {

chk_http_port

}

virtual_ipaddress {

192.168.128.177

}

}

编写监控nginx监控脚本

#vim /usr/local/scripts/nginx_pid.sh

#!/bin/bash

A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A

if [ $A -eq 0 ];then ## 如果没有进程值得为 零

/usr/local/nginx/sbin/nginx

sleep 3

if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

killall keepalived ## 则结束 keepalived 进程

fi

fi

测试,分别在两个服务器 启动 nginx 和 keepalived

#/usr/local/nginx/sbin/nginx

#/etc/init.d/keepalived start

监控 Nginx Mastaer 的日志

May 12 17:33:44 localhost Keepalived_vrrp: Configuration is using : 35676 Bytes

May 12 17:33:44 localhost Keepalived: Starting VRRP child process, pid=1245

May 12 17:33:44 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]

May 12 17:33:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

May 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177

May 12 17:33:46 localhost avahi-daemon[2344]: Registering new address record for 192.168.128.177 on eth0.

May 12 17:33:46 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.128.177 added

监控 Nginx Backup 的日志

May 11 22:28:21 localhost Keepalived: Starting Keepalived v1.1.15 (05/11,2010)

May 11 22:28:21 localhost Keepalived_vrrp: Using MII-BMSR NIC polling thread...

May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink reflector

May 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink command channel

May 11 22:28:21 localhost Keepalived_vrrp: Registering gratutious ARP shared channel

May 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040

May 11 22:28:21 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.

May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes

May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]

May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded

看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了

现在我们把Master的Nginx停掉.查看Backup的日志

May 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040

May 11 22:28:21 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.

May 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes

May 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

May 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]

May 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded

May 11 22:29:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

May 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177

可以看出backup已经变成master

现在再启动原来master的nginx,再查看backup的日志

May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert

May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

May 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.

May 11 22:30:32 localhost avahi-daemon[2409]: Withdrawing address record for 192.168.128.177 on eth0.

可以看出又变回了backup.

  评论这张
 
阅读(830)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018