nginx+keepalived 高可用

Posted

tags:

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

nginx 安装


rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx

#设置开机启动
systemctl enable nginx.service

#启动服务
systemctl start nginx.service


keepalived 安装
#安装keepavlived
yum -y install keepalived

#设置开机启动
systemctl enable nginx.service

#启动服务
service keepalived start


keepavlived mater设置

global_defs {
notification_email {br/>[email protected]
}
notification_email_from [email protected]
smtp_server [email protected]
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 3
weight 2
}

vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 151
priority 100
advert_int 5
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
#VIP
virtual_ipaddress {
172.16.101.193
}
track_script {
chk_nginx
}
}


keepavlived backup 设置
cat keepalived.conf

global_defs {
notification_email {
br/>[email protected]
}
notification_email_from [email protected]
smtp_server [email protected]
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 3 #这个必须要大于脚本里sleep 2 要不脚本不生效。
weight 2
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_interface eth0
virtual_router_id 151
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
#VIP
virtual_ipaddress {
172.16.101.193
}
track_script {
chk_nginx
}
}


脚本:
cat /etc/keepalived/chk_nginx.sh

#!/bin/sh
A=ps -C nginx --no-header |wc -l
if [ $A -eq 0 ];then
systemctl start nginx.service
sleep 2
if [ ps -C nginx --no-header |wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi



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

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

keepalived对nginx高可用演练脚本

配置Keepalived双实例高可用Nginx

Nginx+Keepalived实现站点高可用

nginx和keepalived实现nginx高可用

nginx+keepalived高可用