Nginx+keepalived双机热备(主从模式)

简单介绍:

Keepalived是Linux下面实现VRRP备份路由的高可靠性运行软件,能够真正做到

主服务器和备份服务器故障时IP瞬间无缝交接;

Keepalived的目的是模拟路由器的高可用;

Heartbeat或Corosync的目的是实现Service的高可用.

那heartbaet与corosync又应该选择哪个好?

corosync的运行机制更优于heartbeat,从heartbeat分离出来的pacemaker在以后的开发当中

更倾向于corosync,所以现在corosync+pacemaker是最佳组合.

环境说明:

master机器(master-node):10.0.0.5/172.16.1.5

slave机器(slave-node):10.0.0.6/172.16.1.6

公用的虚拟IP(VIP):10.0.0.3

网站URL:

svn-------dev.qingfeng.com/svn(10.0.0.8:801/svn)

svn web---dev.qingfeng.com/submin(10.0.0.8:801/submin)

网站------www.qingfeng.com(10.0.0.7:80&10.0.0.8:80)

oa--------oa.qingfeng.com(10.0.0.7:802&10.0.0.8:802)

反向代理总结:

多域名指向是通过虚拟主机的不同server实现;

同一域名的不同虚拟目录是通过每个server下面的不同location实现;

反向代理到后端的服务器需要在vhost/LB.conf下面配置upstream,

然后在server或location中通过proxy_pass引用.

我们的目的:

如果master主服务器的keepalived停止服务,slave从服务器会自动接管VIP对外服务;

一旦主服务器的keepalived恢复,会重新接管VIP.

这并不是我们需要的,我们需要的是当NginX停止服务,无法启动时,能够自动切换.

# ip addr add 10.0.0.3/24 dev eth0 label eth0:0

yum -y install keepalived

1.master的keepalived配置

cat /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
    [email protected] 
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb01
}

vrrp_script chk_http_port {
    script "/service/scripts/chk_nginx.sh"  # 通过脚本监测
    interval 2  # 脚本执行间隔,每2s检测一次
    weight -5   # 脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
    fall 2      # 检测连续2次失败才算确定是真失败,会用weight减少优先级(1-255之间)
    rise 1      # 检测1次成功就算成功,不修改优先级
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev eth0 label eth0:1
    }

    track_script {
        chk_http_port  # 这个设置不能紧挨着写在vrrp_script配置块的后面,否则nginx监控失效.
    }
}

2.监测nginx状态的脚本

mkdir -p /service/scripts
cat /service/scripts/chk_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /application/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
        exit 1
    fi
fi
chmod +x chk_nginx.sh

3.slave的keepalived配置

global_defs {
   notification_email {
    [email protected] 
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id lb02
}

vrrp_script chk_http_port {
    script "/service/scripts/chk_nginx.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev eth0 label eth0:1
    }

    track_script {
        chk_http_port
    }
}

4.总结:

测试机10.0.0.5

cat /etc/hosts
10.0.0.3      dev.qingfeng.com www.qingfeng.com oa.qingfeng.com
[root@db01 ~]# curl dev.qingfeng.com/svn/
this is the page of svn-10.0.0.8
[root@db01 ~]# curl dev.qingfeng.com/submin/
this is the page of submin-10.0.0.8
[root@db01 ~]# curl www.qingfeng.com
www-10.0.0.7:80
[root@db01 ~]# curl www.qingfeng.com
www-10.0.0.8:80

用ifconfig无法查看到虚拟ip时,可以用ip addr

无论master还是slave,当其中的一个keepalived服务停止后,vip都会漂移到keepalived还存活的节点上;

如果master上的nginx服务挂了,则nginx会自动重启,重启失败后会自动关闭keepalived,vip也会转移到slave上.

主从模式参考博客:https://www.cnblogs.com/kevingrace/p/6138185.html

猜你喜欢

转载自www.cnblogs.com/fawaikuangtu123/p/10168014.html