Keepalived + Nginx 高可用

Posted 菜鸟厚非

tags:

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

部署

服务器角色
192.168.91.135master
192.168.91.136backup
192.168.91.166VIP

一、nginx 部署

分别在 master、backup 部署 Nginx,请参照 Nginx 部署

二、Keepalived 部署

分别在 master、backup 部署 Nginx,请参照 Keepalived 部署

三、配置

3.1 master

1 . 配置 keepalived.conf

vrrp_script:指定检查nginx工作状态脚本(根据nginx状态判断是否故障转移)
virtual_ipaddress:虚拟IP(VIP)

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     acassen@firewall.loc 
     failover@firewall.loc 
     sysadmin@firewall.loc 
   } 
   notification_email_from Alexandre.Cassen@firewall.loc  
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id NGINX_MASTER
} 

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state MASTER 
    interface ens33  # 修改为实际网卡名
    virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的 
    priority 100    # 优先级,备服务器设置 90 
    advert_int 1    # 指定VRRP 心跳包通告间隔时间,默认1秒 
    authentication { 
        auth_type PASS      
        auth_pass 1111 
    }  
    # 虚拟IP
    virtual_ipaddress { 
        192.168.91.166/24
    } 
    track_script {
        check_nginx
    } 
}
EOF

2 . 配置检测 nginx 脚本,提供给 keepalived

cat > /etc/keepalived/check_nginx.sh  << "EOF"
#!/bin/bash
count=$(ss -antp |grep 6443 |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    exit 1
else
    exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh

注:keepalived根据脚本返回状态码(0为工作正常,非0不正常)判断是否故障转移。

3 . 重启 keepalived

systemctl restart keepalived

3.2 Backup

1 . 配置 keepalived.conf

cat > /etc/keepalived/keepalived.conf << EOF
global_defs { 
   notification_email { 
     acassen@firewall.loc 
     failover@firewall.loc 
     sysadmin@firewall.loc 
   } 
   notification_email_from Alexandre.Cassen@firewall.loc  
   smtp_server 127.0.0.1 
   smtp_connect_timeout 30 
   router_id NGINX_BACKUP
} 

vrrp_script check_nginx {
    script "/etc/keepalived/check_nginx.sh"
}

vrrp_instance VI_1 { 
    state BACKUP 
    interface ens33
    virtual_router_id 51 # VRRP 路由 ID实例,每个实例是唯一的 
    priority 90
    advert_int 1
    authentication { 
        auth_type PASS      
        auth_pass 1111 
    }  
    virtual_ipaddress { 
        192.168.91.166/24
    } 
    track_script {
        check_nginx
    } 
}
EOF

2 . 配置检测 nginx 脚本,提供给 keepalived

cat > /etc/keepalived/check_nginx.sh  << "EOF"
#!/bin/bash
count=$(ss -antp |grep 6443 |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then
    exit 1
else
    exit 0
fi
EOF
chmod +x /etc/keepalived/check_nginx.sh

注:keepalived根据脚本返回状态码(0为工作正常,非0不正常)判断是否故障转移。

3 . 重启 keepalived

systemctl restart  keepalived

四、测试

1 . 查看 master、backup 主机 ip


2 . 停止 master 主机的 nginx,然后查看 master、backup 主机 ip

 ./nginx -s stop


可以看到在 master 服务器 nginx 停止后,VIP 漂移到了 backup 服务器


3 . 然后启动 master 服务器 nginx,然后查看 master、backup 主机 ip,VIP 漂回到了 master 服务器

异常排查

  1. 都已启动,master、backup 无 VIP
    检查脚本检测的端口是否正确

以上是关于Keepalived + Nginx 高可用的主要内容,如果未能解决你的问题,请参考以下文章

keepalived高可用nginx/htttpd,双主模型

keepalived对nginx高可用演练脚本

配置Keepalived双实例高可用Nginx

Nginx+Keepalived实现站点高可用

nginx和keepalived实现nginx高可用

nginx+keepalived高可用