您的位置:首页 > 运维架构 > Nginx

基于nginx+keepalived实现nginx高可用环境的搭建

2017-12-26 21:14 951 查看
在互联网环境中,网络中的主机不可避免的会出现单点故障,当我们在使用nginx进行动静分离、反向代理、https配置时,如果部署nginx的主机宕机,那么这个服务将会出现不可用的状态。所以我们在生产环境中,需要解决nginx部署出现单点故障的问题,那么利用基于VRRP(虚拟路由器冗余协议)的KeepAlived可以有效解决此问题。

什么是VRRP?

虚拟路由冗余协议(Virtual Router Redundancy Protocol,简称VRRP)是由IETF提出的解决局域网中配置静态网关出现单点失效现象的路由协议,1998年已推出正式的RFC2338协议标准。VRRP广泛应用在边缘网络中,它的设计目标是支持特定情况下IP数据流量失败转移不会引起混乱,允许主机使用单路由器,以及及时在实际第一跳路由器使用失败的情形下仍能够维护路由器间的连通性

–摘自百度百科-虚拟路由器冗余协议

什么是KeepAlived?

keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层、第4层和第7层交换。Keepalived是自动完成,不需人工干涉。

–摘自百度百科-Keepalived

KeepAlived的安装

下载地址KeepAlived 1.3.2

安装

yum -y install keepalived

KeepAlived主从配置

架构图



MASTER的配置

! Configuration File for keepalived

global_defs {
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.250
}
}

virtual_server 192.168.0.250 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP

real_server 192.168.0.10 80 {
weight 1
TCP_CHECK {
connect_timeout 10
delay_before_retry 3
connect_port 80
}
}
}


BACKUP的配置

! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.250
}
}

virtual_server 192.168.0.250 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP

real_server 192.168.0.11 80 {
weight 1
TCP_CHECK {
connect_timeout 10
delay_before_retry 3
connect_port 80
}
}
}


此时分别启动主机192.168.0.10与192.168.0.11的keepalived服务,通过
ip addr
查看是否获取到VIP

此时访问 192.168.0.250将会显示192.168.0.10的nginx数据,然后手动kill掉192.168.0.10主机的keepalived,会自动切换到192.168.0.11的nginx数据

Nginx状态监控

在KeepAlived的主从配置中,当Master的KeepAlived宕机之后,会自动路由到Backup的KeepAlived,但是如果Nginx挂掉之后,KeepAlived就会找不到Nginx的服务,这样会造成Nginx的服务不可用,此时我们需要在KeepAlived中添加检测Nginx心跳的脚本,如果脚本检测不到Nginx的服务,就关闭KeepAlived经常,这样Backup的KeepAlived的服务就可以被调用。

如何监控Nginx的状态?

监控Nginx进程

检测Nginx的端口

检测url能否获取到页面

如何尝试恢复服务?

如果监控到Nginx的服务不正常,可以尝试重启,然后sleep一段时间之后再次检测,仍然失败则不再尝试,直接kill掉KeepAlived

监控Nginx状态的脚本

#!/bin/sh

#  nginx_check.sh
#  Shell
#
#  Created by sam.liu on 23/12/2017.
#  Copyright © 2017 sam. All rights reserved.
#  Check Nginx Server Status

# nginx shell script‘s location
NGINX=/root/opt/nginx/sbin/nginx
# nginx’s port
PORT=80

nmap localhost -p $PORT | grep "$PORT/tcp open"
if [ $? -ne 0 ]; then
$NGINX -s stop
$NGINX
sleep 3
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne 0 ] && /etc/init.d/keepalived stop
fi


注意:需要将keepalived添加到系统服务

压缩包安装可能会遇到的问题

./configure
时提示错误

configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files.            !!!


解决方法:

yum -y install openssl-devel


由于我测试的主机事先安装了nginx,所以已经安装gcc依赖,如果测试的主机未安装请使用
yum -y install gcc


make && make install
时提示错误

vrrp_iproute.c:42:33: fatal error: linux/mpls_iptunnel.h: No such file or directory

#include <linux/mpls_iptunnel.h>

^
compilation terminated.
make[2]: *** [vrrp_iproute.o] Error 1
make[2]: Leaving directory `/root/keepalived-1.3.2/keepalived/vrrp'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/root/keepalived-1.3.2/keepalived'
make: *** [all-recursive] Error 1


解决方法:

yum -y install keepalived


使用yum直接安装keepalived

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx