Nginx+keepalived双机热备
- 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
- #vim /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
- #vim /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 <span style="white-space:pre"> </span>## 如果没有进程值得为 零
- <span style="white-space:pre"> </span>/usr/local/nginx/sbin/nginx
- <span style="white-space:pre"> </span>sleep 3
- <span style="white-space:pre"> </span>if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
- <span style="white-space:pre"> </span>killall keepalived ## 则结束 keepalived 进程
- <span style="white-space:pre"> </span>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.