nginx+keepalived双机热备&负载均衡

原创
2017/03/05 14:07
阅读数 500

一.keepalived简介

1.1Keepalived介绍

 Keepalived是Linux下一个轻量级别的高可用解决方案。高可用(High Avalilability,HA),其实两种不同的含义:广义来讲,是指整个系统的高可用行,狭义的来讲就是之主机的冗余和接管,

 它与HeartBeat RoseHA 实现相同类似的功能,都可以实现服务或者网络的高可用,但是又有差别,HeartBeat是一个专业的、功能完善的高可用软件,它提供了HA 软件所需的基本功能,比如:心跳检测、资源接管,检测集群中的服务,在集群节点转移共享IP地址的所有者等等。HeartBeat功能强大,但是部署和使用相对比较麻烦,

Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三、第四层、第五层交换机制检测每个服务节点的状态,如果某个服务器节点出现异常,或者工作出现故障,Keepalived将检测到,并将出现的故障的服务器节点从集群系统中剔除,这些工作全部是自动完成的,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。

后来Keepalived又加入了VRRP的功能,VRRP(Vritrual Router Redundancy Protocol,虚拟路由冗余协议)出现的目的是解决静态路由出现的单点故障问题,通过VRRP可以实现网络不间断稳定运行,因此Keepalvied 一方面具有服务器状态检测和故障隔离功能,另外一方面也有HA cluster功能,下面介绍一下VRRP协议实现的过程。

1.2 vrrp:虚拟冗余路由协议

vrrp的工作方式:将两个或两个以上的物理路由设备定义成一个虚拟的路由器,这种方式称作为一个路由组,这组路由设备共同构建成为一个虚拟的路由,在此路由上配置一个VIP和与此VIP相对应的VMAC,在此路由组中,每个路由器都有自己的优先级,当路由器开启时都会自我初始化,然后发送广播通告路由组中的所有路由器自己的优先级,以此推选出主节点MASTER和备用节点BACKUP级别的路由器,只有当主节点故障或优先级降低时才会通告其他节点,然后其他节点再次通过比较优先级推选出另外一个主节点,另外一个主节点会去夺取之前主节点的IP地址等信息

1.3 keepalived中有三种节点类型

    MASTER:主节点类型

    BACKUP:备用节点类型

    Initialized:表初始化

 

二.环境准备

2.1安装环境

Server 1  :  ubuntu-server 8.04.4          192.168.6.162

Server 2  :  userver-server 8.04.4          192.168.6.188

 

2.2需要下载的软件包

Keepalived 1.1.15

nginx-0.8.35

pcre-8.02

 

三.安装Nginx+KeepAlived

3.1.分别在两台服务器上安装nginx

tar jxvf pcre-8.02.tar.bz2 
cd pcre-8.02 
./configure --prefix=/usr --enable-utf8 --enable-pcregrep-libbz2 --enable-pcregrep-libz 
make 
make install 
tar zxvf nginx-0.8.35.tar.gz 
cd nginx-0.8.35 
--prefix=/usr/local/nginx --with-pcre --user=www --group=www --with-file-aio --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt=' -O3' 
make 
make install

 

3.2分别在两台服务器编写配置文件

#vim /usr/local/nginx/conf/nginx.conf 
user    www www; 
worker_processes    1; 
error_log    logs/error.log    notice; 
pid                logs/nginx.pid; 
events { 
        worker_connections    1024; 
} 
http { 
        include             mime.types; 
        default_type    application/octet-stream; 
        sendfile                on; 
        tcp_nopush         on; 
        keepalive_timeout    65; 
        gzip    on; 
        server { 
                listen             80; 
                server_name    localhost; 
                index     index.html index.htm; 
                root        /var/www; 
                error_page     500 502 503 504    /50x.html; 
                location = /50x.html { 
                        root     html; 
                } 
        } 
} 

 

3.3分别在两台机器创建测试文件

#第一台

echo "192.168.6.162" > /var/www/index.html 

#第二台
echo "192.168.6.188" > /var/www/index.html

 

3.4安装 keepalived 

apt-get install keepalived

 

3.5.在server 1服务器编写配置文件

vrrp_script chk_http_port { 
                script "/opt/nginx_pid.sh"         ###监控脚本 
                interval 2                             ###监控时间 
                weight 2                                ###目前搞不清楚 
} 
vrrp_instance VI_1 { 
        state MASTER                            ### 设置为 主 
        interface eth0                             ### 监控网卡    
        virtual_router_id 51                    ### 这个两台服务器必须一样 
        priority 101                                 ### 权重值 MASTRE 一定要高于 BAUCKUP 
        authentication { 
                     auth_type PASS             ### 加密 
                     auth_pass eric                ### 加密的密码,两台服务器一定要一样,不然会出错 
        } 
        track_script { 
                chk_http_port                     ### 执行监控的服务 
        } 
        virtual_ipaddress { 
             192.168.6.7                            ###    VIP 地址 
        } 
} 

 

3.6.在 server 2 服务器 keepalived 配置

vrrp_script chk_http_port { 
                script "/opt/nginx_pid.sh" 
                interval 2 
                weight 2 
} 
vrrp_instance VI_1 { 
        state BACKUP                                ### 设置为 辅机 
        interface eth0 
        virtual_router_id 51                        ### 与 MASTRE 设置 值一样 
        priority 100                                     ### 比 MASTRE权重值 低 
        authentication { 
                     auth_type PASS 
                     auth_pass eric                    ### 密码 与 MASTRE 一样 
        } 
        track_script { 
                chk_http_port 
        } 
        virtual_ipaddress { 
                 192.168.6.7 
        } 
}

 

3.7编写监控nginx监控脚本

vim /opt/nginx_pid.sh 
#!/bin/bash

# varsion 0.0.2

# 根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived 杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios 当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本
A=`ps -C nginx --no-header |wc -l`                ## 查看是否有 nginx进程 把值赋给变量A 
if [ $A -eq 0 ];then                                         ## 如果没有进程值得为 零

                /usr/local/nginx/sbin/nginx

                sleep 3

                if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

                       killall keepalived                        ## 则结束 keepalived 进程

                fi
fi 

 

3.8测试,分别在两个服务器 启动 nginx 和 keepalived

/usr/local/nginx/sbin/nginx 
/etc/init.d/keepalived start

 

监控 server 1 的日志

Apr 20 18:37:39 nginx Keepalived_vrrp: Registering Kernel netlink command channel 
Apr 20 18:37:39 nginx Keepalived_vrrp: Registering gratutious ARP shared channel 
Apr 20 18:37:39 nginx Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. 
Apr 20 18:37:39 nginx Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. 
Apr 20 18:37:39 nginx Keepalived_healthcheckers: Configuration is using : 3401 Bytes 
Apr 20 18:37:39 nginx Keepalived_vrrp: Configuration is using : 35476 Bytes 
Apr 20 18:37:40 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE 
Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE 
Apr 20 18:37:41 nginx Keepalived_vrrp: Netlink: skipping nl_cmd msg... 
Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded

监控 server 2的日志 

Apr2018:38:23 varnish Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'. 
Apr 20 18:38:23 varnish Keepalived_healthcheckers: Configuration is using : 3405 Bytes 
Apr 20 18:38:23 varnish Keepalived_vrrp: Using MII-BMSR NIC polling thread... 
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink reflector 
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink command channel 
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering gratutious ARP shared channel 
Apr 20 18:38:23 varnish Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'. 
Apr 20 18:38:23 varnish Keepalived_vrrp: Configuration is using : 35486 Bytes 
Apr 20 18:38:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE 
Apr 20 18:38:25 varnish Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded 

 

看日志可以看出,两台服务器的 MASTRE 和 BACUKUP 已经都正常了

现在我们在 server 1 把 nginx 服务器停到

Server 1 $> killall nginx

 

这时候看server 1的日志

Apr 20 18:41:26 nginx Keepalived_healthcheckers: Terminating Healthchecker child process on signal 
Apr 20 18:41:26 nginx Keepalived_vrrp: Terminating VRRP child process on signal 

 

可以看出keepalived 的进程已经停到

这时候看server 2的日志,看是否已经接管

Apr 20 18:41:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE 
Apr 20 18:41:24 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE 
Apr 20 18:41:24 varnish Keepalived_vrrp: Netlink: skipping nl_cmd msg... 

 

很明显的看出 server 2 已经接管了,已经变为 MASTER 了

 

四.负载均衡

1. nginx负载均衡

Nginx本身具有负载均很的功能,也有集群健康监测的策略

2.keepalived负载均衡

首先,keepalived中的virtual_server只能对LVS服务,从而实现LVS负载均衡,keepalived不存在负载均衡的概念

keepalived最初目的是为了LVS集群健康监测等服务,同样后期增加的vrrp也主要是为了LVS服务,可以和LVS完美兼容,但是这点也限制了他与haproxy和nginx实现负载均衡,当然,这些问题在当今环境中都可以完美解决。

 

五.参考文献

Keepalived安装与配置

http://www.keepalived.org/download.html

Nginx +keepalived

LVS+Keepalived负载均衡主备&双主架构全攻略

keepalived基础及使用DR模型构建一个real_server

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部