Nginx高可用-Keepalived

Nginx高可用-Keepalived

Keepalived:专门做高可用的软件,不只是做Nginx高可用

什么是高可用

一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的

高可用通常使用什么软件

  • keepalived
  • heartbeat
  • RoseHA

keepalived是如何实现高可用的?

keepalived软件是基于VRRP协议实现的,VRRP协议:虚拟路由冗余协议,主要用于解决单点故障问题

VRRP原理又是什么?

我们的VRRP其实是通过软件或者硬件的形式在Master和Backup外面增加一个虚拟的MAC地址(VMAC)与虚拟IP地址(VIP),那么在这种情况下,PC请求VIP的时候,无论是Master处理还是Backup处理,PC仅会在ARP缓存表中记录VMAC与VIP的信息。

高可用keepalived使用场景

通常业务系统需要保证7×24小时不DOWN机,比如公司内部的OA系统,每天公司人员都需要使用,则不允许Down机,作为业务系统来说随时都可用

Nginx高可用-Keepalived

高可用keepalived核心概

  • 1、优先级

    • 如何确定谁是主节点谁是备节点
  • 2、抢占试、非抢占式

    • 如果Master故障,Backup自动接管,那么Master回复后会夺权吗
  • 3、脑裂

    • 如果两台服务器都认为自己是Master会出现什么问题

部署keepalived高可用软件

环境准备

主机 角色 外网IP 内网IP 软件
lb01 主节点(master) 10.0.0.5 172.16.1.5 nginx、keepalived
lb02 备节点(backup) 10.0.0.6 172.16.1.6 nginx、keepalived
VIP 虚拟IP 10.0.0.3

keeplived工作原理

1.哪些机器需要做高可用,就要在哪些机器上安装keepalived

2.keepalived的主节点会心跳检测(想要证明应用或者主机是否存活)

3.如果心跳检测失败,就杀掉自己(keepalived)

4.VIP到备节点

安装keepalived

# 1.安装keepalived软件
[root@lb01 ~]# yum install -y keepalived
[root@lb02 ~]# yum install -y keepalived

# 2.修改配置文件
## 修改master主节点配置文件
[root@lb01 ~]# vim /etc/keepalived/keepalived.conf 

global_defs {                   #全局配置
    router_id lb01              #标识身份->名称
}

vrrp_instance VI_1 {
    state MASTER                #标识角色状态
    interface eth0              #网卡绑定接口
    virtual_router_id 50        #虚拟路由id
    priority 150                #优先级
    advert_int 1                #监测间隔时间
    authentication {            #认证
        auth_type PASS          #认证方式
        auth_pass 1111          #认证密码
    }
    virtual_ipaddress {
        10.0.0.3                #虚拟的VIP地址
    }
}

## 修改backup备节点配置文件
[root@lb02 ~]# vim /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
    }
}

# 3.启动keepalived服务并加入开机自启
[root@lb01 ~]# systemctl start keepalived.service 
[root@lb01 ~]# systemctl enable keepalived.service

[root@lb02 ~]# systemctl start keepalived.service
[root@lb02 ~]# systemctl enable keepalived.service

# 4.查看服务是否开启
[root@lb01 ~]# ps -ef|grep [k]eepalive
root       7363      1  0 18:33 ?        00:00:00 /usr/sbin/keepalived -D
root       7364   7363  0 18:33 ?        00:00:00 /usr/sbin/keepalived -D
root       7365   7363  0 18:33 ?        00:00:00 /usr/sbin/keepalived -D

[root@lb02 ~]# ps -ef|grep [k]eepalive
root       7284      1  0 18:33 ?        00:00:00 /usr/sbin/keepalived -D
root       7285   7284  0 18:33 ?        00:00:00 /usr/sbin/keepalived -D
root       7286   7284  0 18:33 ?        00:00:00 /usr/sbin/keepalived -D

# 5.查看vip(虚拟IP)
[root@lb01 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d5:75:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.5/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/32 scope global eth0
       valid_lft forever preferred_lft forever

# 6.检测vIP是否会漂移
[root@lb01 ~]# systemctl stop keepalived
[root@lb01 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d5:75:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.5/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever

[root@lb02 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a3:bc:3e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.6/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/32 scope global eth0
       valid_lft forever preferred_lft forever

主节点和备节点的配置文件区别

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

抢占式配置文件

分为主节点和备节点,

主节点配置:state MASTER,

备节点配置:state BACKUP

## 修改master主节点配置文件
[root@lb01 ~]# vim /etc/keepalived/keepalived.conf 

global_defs {                   #全局配置
    router_id lb01              #标识身份->名称
}

vrrp_instance VI_1 {
    state MASTER                #标识角色状态
    interface eth0              #网卡绑定接口
    virtual_router_id 50        #虚拟路由id
    priority 150                #优先级
    advert_int 1                #监测间隔时间
    authentication {            #认证
        auth_type PASS          #认证方式
        auth_pass 1111          #认证密码
    }
    virtual_ipaddress {
        10.0.0.3                #虚拟的VIP地址
    }
}

## 修改backup备节点配置文件
[root@lb02 ~]# vim /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
    }
}

非抢占是配置

1、两个节点的state都必须配置为BACKUP

2、两个节点都必须加上配置 nopreempt

3、其中一个节点的优先级必须要高于另外一个节点的优先级。 两台服务器都角色状态启用nopreempt后,必须修改角色状态统一为BACKUP,唯一的区分就是优先级。

[root@lb01 ~]# vim /etc/keepalived/keepalived.conf 
global_defs {
    router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    nopreempt
    interface eth0
    virtual_router_id 50
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.3
    }
}

[root@lb02 ~]# vim /etc/keepalived/keepalived.conf 
global_defs {
    router_id lb02
}

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

keepalived结合nginx实现负载均衡的高可用

为什么域名解析到VIP就可以访问nginx?

Nginx默认监听在所有的IP地址上,VIP会飘到一台节点上,相当于那台nginx多了VIP这么一个网卡,所以可以访问到nginx所在机器

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

## 1.编写监听nginx的脚本
[root@lb01 ~]# vim check.sh 
#!/bin/sh
nginx_count=$(ps -ef|grep [n]ginx|wc -l)
if [ $nginx_count -eq 0 ];then
     systemctl stop keepalived
fi

#1.判断Nginx是否存活,如果不存活则尝试启动Nginx
#if [ $nginx_count -eq 0 ];then
#    #systemctl start nginx
#    #sleep 3
#    #2.等待3秒后再次获取一次Nginx状态
#    nginx_count=$(ps -ef|grep [n]ginx|wc -l)
#    #3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本  
#    if [ $nginx_count -eq 0 ];then
#        systemctl stop keepalived
#   fi
#fi

## 2.修改主节点的配置文件
[root@lb01 ~]# vim /etc/keepalived/keepalived.conf 
global_defs {                   #全局配置
    router_id lb01              #标识身份->名称
}
## 注意:执行脚本的名字,最好不要带进程名
vrrp_script check_web {
    # 脚本路径
    script "/root/check.sh"
    # 检测时间(每5秒执行一次检测脚本)
    interval 5
}

vrrp_instance VI_1 {
    state MASTER                #标识角色状态
    interface eth0              #网卡绑定接口
    virtual_router_id 50        #虚拟路由id
    priority 150                #优先级
    advert_int 1                #监测间隔时间
    authentication {            #认证
        auth_type PASS          #认证方式
        auth_pass 1111          #认证密码
    }

    track_script {
        check_web
    }

    virtual_ipaddress {
        10.0.0.3                #虚拟的VIP地址
    }
}

## 3.重启keepalived
[root@lb01 ~]# systemctl restart keepalived

## 4.给脚本执行权限
[root@lb01 ~]# chmod +x /root/check.sh

## 5.检测负载均衡是否可以高可用
# 关闭nginx服务(检测vIP是否会漂移)
[root@lb01 ~]# systemctl stop nginx
[root@lb01 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d5:75:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.5/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever

[root@lb02 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a3:bc:3e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.6/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/32 scope global eth0
       valid_lft forever preferred_lft forever

高可用keepalived故障脑裂

由于某些原因,导致两台keepalived高可用服务器在指定时间内,无法检测到对方的心跳,而此时的两台高可用服务器又都还活着。

脑裂故障原因

  • 1、服务器网线松动等网络故障
  • 2、服务器硬件故障发生损坏现象而崩溃
  • 3、主备都开启firewalld防火墙

实现脑裂故障现象

# 将节点1和节点2的防火墙都打开,两台负载都有10.0.0.3IP,出现脑裂现象
[root@lb01 ~]# systemctl start firewalld
[root@lb01 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d5:75:e6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.5/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/32 scope global eth0
       valid_lft forever preferred_lft forever


[root@lb02 ~]# systemctl start firewalld
[root@lb02 ~]# ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:a3:bc:3e brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.6/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.3/32 scope global eth0
       valid_lft forever preferred_lft forever

打开http://blog.dsr.com/,博客直接访问不了

Nginx高可用-Keepalived

解决脑裂故障方法

# 如果发生脑裂,则随机kill掉一台即可
# 在备节点上编写检测脚本, 测试如果能ping通主节点并且备节点还有VIP的话则认为产生了脑裂
[root@lb02 ~]# cat check_split_brain.sh
#!/bin/sh
vip=10.0.0.3
lb01_ip=10.0.0.5
while true;do
    ping -c 2 $lb01_ip &>/dev/null
    if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then
        echo "ha is split brain.warning."
    else
        echo "ha is ok"
    fi
sleep 5
done

Nginx高可用-Keepalived

上一篇:【常见坑】postMessage传递到iframe 第一次无效


下一篇:匿名对象