Nginx双机热备

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx双机热备相关的知识,希望对你有一定的参考价值。

nginx双机热备

Keepalived高可用概述

1.什么是高可用

什么是高可用双击热备, 一般指2台机器启动着相同的业务系统,当有一台机器down机了, 另外一台服务器能快速的接管, 对于访问的用户是无感知的。

2.高可用使用场景

那么高可用使用在什么场景,业务系统需要保证7x24小时不DOWN机, 作为业务来说随时都可用, 让你的业务系统更顽强。

Keepalived高可用安装

1.环境准备

服务器系统   角色  外网IP    内网IP
CentOS 7.5  keepalived-master   eth0:10.0.0.5   eth1:172.16.1.5
CentOS 7.5  keepalived-slave    eth0:10.0.0.6   eth1:172.16.1.6

2.在lb01与lb02上分别安装keepalived

[[email protected] ~]# yum install keepalived -y
[[email protected] ~]# yum install keepalived -y

3.配置lb01, keepalived-master

[[email protected] ~]# cat /etc/keepalived/keepalived.conf
global_defs {     
    router_id lb01   
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}
    virtual_ipaddress {
        10.0.0.3/24 dev eth0
    }
}

4.配置lb02, keepalived-backup

[[email protected] ~]# cat /etc/keepalived/keepalived.conf
global_defs {
    router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev eth0
    }
}

5.对比keepalived的master与backup配置的区别

Keepalived配置区别  Master配置    Backup节配置
route_id(唯一标识)  route_id lb01   route_id lb02
state(角色状态) state Master    state Backup
priority(竞选优先级) priority 150    priority 100

6.启动lb01与lb02的keepalived

#lb01
[[email protected] ~]# systemctl enable keepalived
[[email protected] ~]# systemctl start keepalived

#lb02
[[email protected] ~]# systemctl enable keepalived
[[email protected] ~]# systemctl start keepalived

7.检查keepalived的虚拟IP地址是否漂移
在lb01上进行如下操作

# lb01存在vip地址
[[email protected] ~]# ip addr |grep 10.0.0.3
    inet 10.0.0.3/24 scope global secondary eth0

# 停止lb01上的keepalived, 检测vip已不存在
[[email protected] ~]# systemctl stop keepalived
[[email protected] ~]# ip addr |grep 10.0.0.3

在lb02上进行如下操作

[[email protected] ~]# ip addr|grep 10.0.0.3
    inet 10.0.0.3/24 scope global secondary eth0

lb01重新启动keepalived,发现地址被重新接管

[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# ip addr |grep 10.0.0.3
    inet 10.0.0.3/24 scope global secondary eth0

Keepalived高可用配置

技术分享图片

keepalived高可用列脑

由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方的心跳消息,各自取得资源及服务的所有权,而此时的两台高可用服务器又都还活着。

服务器网线松动等网络故障
服务器硬件故障发生损坏现象而崩溃
主备都开启firewalld防火墙
Nginx服务死掉等

1.在备上编写检测脚本, 测试如果能ping通主并且备节点还有VIP的话则认为产生了列脑

[[email protected] ~]# cat check_split_brain.sh
#!/bin/sh
lb01_vip=10.0.0.3
lb01_ip=10.0.0.5
while true;do
    ping -c 2 -W 3 $lb01_ip &>/dev/null
    if [ $? -eq 0 -a `ip add|grep "$lb01_vip"|wc -l` -eq 1 ];then
        echo "ha is split brain.warning."
    else
        echo "ha is ok"
    fi
sleep 5
done

2.如果Nginx宕机, 会导致用户请求失败, 但Keepalived并不会进行切换, 所以需要编写一个脚本检测Nginx的存活状态, 如果不存活则kill nginx和keepalived

[[email protected] ~]# mkdir /server/scripts
[[email protected] ~]# vim /server/scripts/check_web.sh
#!/bin/sh
#使用while死循环
while true;do
nginxpid=$(ps -C nginx --no-header|wc -l)
#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
if [ $nginxpid -eq 0 ];then
    systemctl start nginx
    sleep 5
    #2.5秒后再次获取一次Nginx状态
    nginxpid=$(ps -C nginx --no-header|wc -l) 
    #3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本  
    if [ $nginxpid -eq 0 ];then
        systemctl stop keepalived
        exit 1
   fi
fi
     sleep 5
done

[[email protected] ~]# chmod +x /server/scripts/check_web.sh

在keepalived配置文件中调用此脚本,lb01与lb02都需操作

[[email protected] ~]# cat /etc/keepalived/keepalived.conf
global_defs {
         router_id LVS_01
      }
vrrp_script check_web {
   script "/server/scripts/check_web.sh"
   interval 2
   weight 50
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3/24 dev ens33
    }
    track_script {
        check_web
    }
}

以上是关于Nginx双机热备的主要内容,如果未能解决你的问题,请参考以下文章

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

Nginx+keepalived双机热备

keepalived双机热备nginx

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

keepalived+nginx双机热备+负载均衡

Nginx双机热备