Nginx+keepalived实现高可用

1、规划准备

操作系统: Centos6.7_x86_64

主机名称

服务器地址

角色状态

服务

vip地址

Jason1

10.18.199.176

MASTER

nginx

10.18.199.106

Jason2

10.18.199.180

BACKUP

nginx

2、安装keepalived与nginx

在两个节点上均安装keepalived与nginx软件。

Nginx安装

tar zxvf nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure--sbin-path=/usr/local/nginx/nginx--conf-path=/usr/local/nginx/nginx.conf

--pid-path=/usr/loacal/nginx/nginx.pid

--with-http_ssl_module

--with-http_realip_module

--with-http_sub_module

--with-http_gunzip_module 

--with-http_gzip_static_module

--with-http_stub_status_module

--with-openssl=../openssl-1.0.2d

--with-pcre=../pcre-8.36

--with-zlib=../zlib-1.2.8

make

make install

nginx配置:

访问节点jasan1页面时显示

NginX+keepalived实现高可用_keepalived nginx  HA


访问节点jasan2页面时显示


NginX+keepalived实现高可用_keepalived nginx  HA_02

Keepalived安装

tarzxvf keepalived-1.2.19.tar.gz

cd keepalived-1.2.19

./configure --prefix=/usr/local/keeplived

Make

makeinstall

cd/usr/local/keeplived/etc/

cp-rp sysconfig/keepalived /etc/sysconfig/

cp-rp keepalived /etc/

cp -rp rc.d/init.d/keepalived /etc/init.d

3、配置keepalived

master:

global_defs{ 

   notification_email { 

     client@examge.com

   } 

   notification_email_from admin@examge.com 

   smtp_server 127.0.0.1 

   smtp_connect_timeout 30 

   router_id lvs_ha

vrrp_scriptchk_http_port { 

    script"</dev/tcp/127.0.0.1/80" 

    interval 1 

vrrp_instance VI_1 { 

    state master

    nopreempt

    interface eth0 

    virtual_router_id 50 

    priority 100 

    advert_int 1 

    authentication { 

        auth_type PASS 

        auth_pass 1111 

    } 

    virtual_ipaddress { 

        10.18.199.106 

    } 

    track_script { 

        chk_http_port 

    }

}

backup:

global_defs{ 

   notification_email { 

     client@examge.com

   } 

   notification_email_fromadmin@examge.com 

   smtp_server 127.0.0.1 

   smtp_connect_timeout 30 

   router_id lvs_ha

vrrp_scriptchk_http_port { 

    script"</dev/tcp/127.0.0.1/80" 

    interval 1 

vrrp_instance VI_1 { 

    state BACKUP 

    interface eth0 

    virtual_router_id 50 

    priority 80 

    advert_int 1 

    authentication { 

        auth_type PASS 

        auth_pass 1111 

    } 

    virtual_ipaddress { 

        10.18.199.106 

    } 

    track_script { 

        chk_http_port 

    }

}

4、测试

(1)启动keepalived

查看日志jason1为master状态,jason2为backup状态,飘移地址位于jason1上。

jason1:

NginX+keepalived实现高可用_keepalived nginx  HA_03查看vip

NginX+keepalived实现高可用_keepalived nginx  HA_04

jason2:

NginX+keepalived实现高可用_keepalived nginx  HA_05通过vip地址访问站点显示

NginX+keepalived实现高可用_keepalived nginx  HA_06

(2)、关闭jason1节点nginx

关闭主机nginx,keepalived检测到httpd失败,由master状态转为fault状态,发生切换。

jason1:

NginX+keepalived实现高可用_keepalived nginx  HA_07jason2:

状态由backup转为master,并添加漂移地址。

NginX+keepalived实现高可用_keepalived nginx  HA_08查看vip

NginX+keepalived实现高可用_keepalived nginx  HA_09

刷新访问106的页面显示如下:

NginX+keepalived实现高可用_keepalived nginx  HA_10

(3)、恢复jason1节点nginx

    重新启动nginx,jason1主机仍未backup状态,jason2主机仍为master,角色未发生切换。这是因为jason1主机配置文件中设置了nopreempt,当资源恢复时不发生切回。

jason1:

keepalived检测到htppd正常,状态由fault转换为backup。

NginX+keepalived实现高可用_keepalived nginx  HA_11jason2:

日志没有任何变化,vip仍然在jason2上,jason2节点仍为master。

NginX+keepalived实现高可用_keepalived nginx  HA_09

刷新访问106页面,未发生切换。

NginX+keepalived实现高可用_keepalived nginx  HA_10

(4)、关闭jason2节点nginx

jason1:jason1节点由backup状态转换为master状态。

NginX+keepalived实现高可用_keepalived nginx  HA_14jason2:keepalived检测到httpd失败,由master状态转为fault状态,发生切换。

NginX+keepalived实现高可用_keepalived nginx  HA_15访问vip地址页面显示如下:

NginX+keepalived实现高可用_keepalived nginx  HA_06

(5)、恢复jason2节点nginx

jason1:日志没有任何变化,vip仍然在jason1上,jason1节点仍为master。

jason2:keepalived检测到htppd正常,状态由fault转换为backup。

NginX+keepalived实现高可用_keepalived nginx  HA_17

备注:

(1)、关闭、重启服务器、关闭keepalived软件同样会发生切换,这里没有进行测试,重点测试了资源失败进行切换状态分析。

(2)、资源的状态检测有多种方法,可以根据自己的实际情况编写脚本。以下为举列常见的检测方法。

通过killall命令探测服务运行状态。

vrrp_script chk_http {

       script "killall -0 nginx"   

       interval 2                    

}

通过检测端口运行状态。

vrrp_script chk_http_port {

       script "</dev/tcp/127.0.0.1/80" 

       interval 1                      

}

(3)、本实验vrrp_script中未使用weight参数。当设置weight参数之后,当master资源失败后可以切换至备机,当主机恢复资源后不发生切换,但是当备机资源失败时不会发生切换。不知道什么原因,因此取消该参数,测试切换一切正常。