nginx+keepalive双VIP高可用搭建配置
Posted 龙叔运维
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+keepalive双VIP高可用搭建配置相关的知识,希望对你有一定的参考价值。
介绍一个比较常用,也比较实用的nginx在生产中的架构模式:nginx+keepalive+双VIP
架构图如下:
主机1异常时,VIP1切换到主机2,此时两个VIP都指向主机2,等主机1恢复,VIP1自动切回主机1
nginx的安装这里就不过多描述了,主要讲讲keepalive的配置
假设主机和VIP如下:
主机:1.1.1.1,2.2.2.2
VPI:11.11.11.11,22.22.22.22
具体keepalive配置 以及 keepalive使用到的nginx健康检查脚本 如下:
【1.1.1.1主机 keepalive配置】
! Configuration File for keepalived
global_defs {
router_id sz-lvs1
}vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -50
}vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 246
priority 100
authentication {
auth_type PASS
auth_pass 2461
}
virtual_ipaddress {
11.11.11.11
}
track_script {
check_nginx
}
}vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 247
priority 70
authentication {
auth_type PASS
auth_pass 2471
}
virtual_ipaddress {
22.22.22.22
}
track_script {
check_nginx
}
}
【2.2.2.2主机 keepalive配置】
! Configuration File for keepalived
global_defs {
router_id sz-lvs1
}vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -50
}vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 246
priority 70
authentication {
auth_type PASS
auth_pass 2461
}
virtual_ipaddress {
11.11.11.11
}
track_script {
check_nginx
}
}vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 247
priority 100
authentication {
auth_type PASS
auth_pass 2471
}
virtual_ipaddress {
22.22.22.22
}
track_script {
check_nginx
}
}
【keepalive配置的nginx健康检查脚本check_nginx.sh】
#!/bin/sh
count=`ps aux | grep -v grep |grep -v "check_nginx\\|nginx.conf" | grep nginx | wc -l`
if [ $count -gt 0 ]; then
exit 0
else
exit 1
fi
这样就完成高可用的nginx的搭建了
欢迎关注我的公众号:龙叔运维
持续分享运维经验
以上是关于nginx+keepalive双VIP高可用搭建配置的主要内容,如果未能解决你的问题,请参考以下文章
Nginx+keepalive局域网其它主机ping vip不通