天天看点

nginx + keepalive

目的: 学习nginx + keepalive结合实现双机热备。

【实现图】

<a href="http://s5.51cto.com/wyfs02/M01/86/AA/wKioL1fG1n6ieUYGAABEkJkEL8A459.png" target="_blank"></a>

【环境】

master系统配置:

[root@master html]# ifconfig |grep -A 1 eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:65:14:0F  

          inet addr:192.168.100.10  Bcast:192.168.100.255  Mask:255.255.255.0

[root@master html]# hostname                

master

[root@master html]# cat /etc/issue | head -1

entOS release 6.4 (Final)

master nginx提前web环境:

[root@master html]# curl 192.168.100.10        

&lt;!DOCTYPE html&gt;

&lt;html&gt;

  &lt;h1&gt;192.168.100.10 -- lnmp master -- bbs.test.com&lt;/h1&gt;

&lt;/html&gt;

slave系统配置:

[root@master ~]# ifconfig |grep -A 2 eth0

          inet6 addr: fe80::20c:29ff:fe65:140f/64 Scope:Link

[root@master ~]# hostname                

[root@master ~]# ifconfig |grep -A 1 eth0

[root@master ~]# head -1 /etc/issue

slave nginx配置:

[root@slave ~]# curl 192.168.100.13

&lt;h1&gt;192.168.100.13 -- lnmp slave -- bbs.test.com&lt;/h1&gt;

【安装keepalive】

yum install  keepalived

keepalived-1.2.13-5.el6_6.x86_64

版本号:

[root@master html]# keepalived -v

Keepalived v1.2.13 (03/19,2015)

安装的重要文件:

/etc/keepalived   #配置文件目录

/etc/keepalived/keepalived.conf  #配置文件

/etc/rc.d/init.d/keepalived     #启动文件

/etc/sysconfig/keepalived      #keepalived的系统初识化文件

/usr/bin/genhash            #不知道 hash相关的吧

/usr/sbin/keepalived         #keepalived的可执行文件

【配置文件的编写】

去除默认的配置文件

 &gt; /etc/keepalived/keepalived.conf    

master配置配置文件

! Configuration File for keepalived

#core的定义

global_defs {

   notification_email {

        [email protected]

      }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

#定义检查脚本

vrrp_script check_http {

        script "/root/check_nginx.sh"    # verify the pid existance

        interval 2                    # check every 2 seconds

        weight   2                    # add 2 points of prio if OK 如果检测监本是成功的则优先级加2

#配置实例

vrrp_instance VI_1 {

    state MASTER    #主机为MASTER,备用机为BACKUP

    interface eth0                   #interface to monitor   #HA监测网络接口

    virtual_router_id 51             # Assign one ID for this route 主、备机的virtual_router_id必须相同

    priority 101                     # 101 on master,100 on backup

    nopreempt  #非抢占(因为默认如果master挂了,backup顶上去,即使master恢复也不抢占!

    debug

    authentication {

            auth_type PASS          ##VRRP认证方式

            auth_pass mynginx       #密码为mynginx

        }

#VIP地址

    virtual_ipaddress {

            192.168.100.12

    track_script {

                 check_http  (调用nginx进程检测脚本)

###############check_http的脚本

[root@master html]# cat /root/check_nginx.sh 

<code>#!/bin/bash</code>

<code>A=`</code><code>ps</code> <code>-C nginx --no-header | </code><code>wc</code> <code>-l`</code>

<code>if</code> <code>[ $A -</code><code>eq</code> <code>0 ];</code><code>then</code>

<code>   </code><code>/etc/init</code><code>.d</code><code>/nginx</code> <code>start</code>

<code>   </code><code>sleep</code> <code>3</code>

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

<code>       </code><code>killall keepalived</code>

<code>   </code><code>fi</code>

<code>fi</code>

当检测到nginx进程不存在的时候,就干掉所有的keepalived,这时候,请求将会由keepalived的backup接管!!

注意: 脚本一定要有执行权限

chmod +x /root/check_nginx.sh

slave配置配置文件:

......

state BACKUP    #主机为MASTER,备用机为BACKUP

...

priority 100 

【启动】

master和slave 的keepalive和nginx都启动

/etc/init.d/keepalived start

/etc/init.d/nignx start

此时可以看到vip在master机器行,因为优先级高

[root@master html]# ip addr show eth0

2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:65:14:0f brd ff:ff:ff:ff:ff:ff

    inet 192.168.100.10/24 brd 192.168.100.255 scope global eth0

    inet 192.168.100.12/32 scope global eth0

    inet6 fe80::20c:29ff:fe65:140f/64 scope link 

       valid_lft forever preferred_lft forever

【会导致切换的情况】

1 master挂了(机器挂了或者keepalive进程没了),终归到底keepalive进程没了,此时会vrrp检查对端没包,此时backup接管VIP。

2 check_nginx.sh检查脚本,当检测到nginx进程挂了,且起不来的时候,就把keepalived全部杀掉。

这样当然,就切换到了backup咯。

【疑问】

1 抓包如何抓到vrrp包?

<code>tcpdump  -vvn |</code><code>grep</code> <code>-i vrrp</code>

可以看到优先级的详细信息。

或者通过指定vrr协议抓取

<code>tcpdump -vnn vrrp</code>

2 keepalive的日志如何弄?

    tail -f /var/log/messages

3 非强制nopreempt好像不起作用。先关掉master的keepalivd再启动master 的keepalived和nginx,master依然会把VIP抢占古来

    据说是非强占,只能在backup中起作用,即要将初识状态都设置成为backup,但是实验未成功。 这里不细细研究。

4 weight像没啥用?

  在这个地方,确实没用。但是weight的只要脚本执行返回值为0(即echo $?为0就表示脚本执行成功),所以以上的哪个实验,master的pri优先级为101+2=103,backup的pri优先级为100+2=102。

  其实上面切换的主要关键,是用到关闭keepalived。进行切换。

messgages可以看到的信息。

VRRP_Script(check_http) succeeded

5 检查时间

6 track_script其实就是用来检测nginx的。

简单VRRP的原理:

看vrrp包信息:

21:47:12.591586 IP (tos 0xc0, ttl 255, id 431, offset 0, flags [none], proto VRRP (112), length 40)

    192.168.100.10 &gt; 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 103, authtype simple, intvl 1s, length 20, addrs: 192.168.100.12 auth "mynginx^@"

是master通过组播地址224.0.0.18通告自己的route id和优先级。收到包的节点,并且认证通过的,比较自己和收到的优先级,进行选举新的mater。

本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1844976如需转载请自行联系原作者

cuizhiliang

继续阅读