说明:

keepalived实现nginx发生故障时,自动切换,实现nginx反向代理的高可用。


环境:

CentOS6.4  64bit;
Jdk6.tar.gz
Aapache-tomcat-6.0.41.tar.gz
nginx-1.4.2.tar.gz 
keepalived-1.1.19.tar.gz


主机规划:

keepalived-master:  192.168.115.10
keepalived-slave :  192.168.115.20
tomcat1          :  192.168.115.30
tomcat2          :  192.168.115.40


JDK安装:

tar  zxvf   JDK6.tar.gz
vi /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk6
export CALASS_PATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_BIN=$JAVA_HOME/bin
export PATH=$PATH:$JAVA_HOME/bin



Tomcat安装:

tar zxvf  Aapache-tomcat-6.0.41.tar.gz  
mv  ./tomcat   /usr/local/tomcat1
cp  -r  /usr/local/tomcat1   /usr/local/tomcat2
修改tomcat2中server.xml定义的三个端口:
shutdown端口"8005
ajp端口:    8009
http端口:   8080


nginx的安装:

http://yujianglei.blog.51cto.com/7215578/1725587




启动脚本:

http://yujianglei.blog.51cto.com/7215578/1561565



安装keepalived:

http://yujianglei.blog.51cto.com/7215578/1725586




修改配置文件:

! Configuration File for keepalived
global_defs {
   notification_email {
     1075841124@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_nginx {    script "kill all -0 nginx"    interval 1    weight -2    fall 2    rise 1}             这是keepalived对nginx的健康监测
vrrp_instance VI_1 {
    state MASTER        主为MASTER。备为BACKUP
    interface eth0
    virtual_router_id 51  主和备相同
    priority 100           主高备低
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.115.222
        192.168.115.111
        192.168.115.99
    }
  track_script {
   chk_nginx
}
}

观察:

MASTER:

/etc/init.d/keepalived start

ip addr show看到如下信息:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:7e:20:10 brd ff:ff:ff:ff:ff:ff
    inet 192.168.115.10/24 brd 192.168.115.255 scope global eth0
    inet 192.168.115.222/32 scope global eth0
    inet 192.168.115.111/32 scope global eth0
    inet 192.168.115.99/32 scope global eth0
    inet6 fe80::20c:29ff:fe7e:2010/64 scope link 
       valid_lft forever preferred_lft forever


BACKUP:

/etc/init.d/keepalived start



2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:05:ff:b3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.115.20/24 brd 192.168.115.255 scope global eth0
    inet6 fe80::20c:29ff:fe05:ffb3/64 scope link 
       valid_lft forever preferred_lft forever


停止master上的keepalived后在观察backup:

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:05:ff:b3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.115.20/24 brd 192.168.115.255 scope global eth0
    inet 192.168.115.222/32 scope global eth0
    inet 192.168.115.111/32 scope global eth0
    inet 192.168.115.99/32 scope global eth0
    inet6 fe80::20c:29ff:fe05:ffb3/64 scope link 
       valid_lft forever preferred_lft forever



配置nginx代理tomcat

http模块中添加:

upstream  tomcat {
        server 192.168.115.30:8080 weight=3;
        server 192.168.115.30:8081 weight=3;
        server 192.168.115.40:8080 weight=3;
        server 192.168.115.40:8081 weight=3;
location / {
             proxy_pass http://tomcat; 
           }


Nginx配置完毕。此时测试:

192.168.115.10:MASTER
            /etc/init.d/keepalived start
            server nginx start
192.168.115.20:SLAVE
            /etc/init.d/keepalived start
            server nginx start



注意此时,在keepalived上配置的虚拟IP是:

192.168.115.222
192.168.115.111
192.168.115.99


在浏览器中访问任何一个VIP,nginx都能代理到后方。并且能正常访问到TOMCAT主页面。wKiom1RHWlLAlXY1AAcRzH78aTU759.jpg


在把MASTER上的nginx stop,观察

192.168.115.10 MASTER:
     service  nginx stop


ip addr show 观察:发现VIP消失

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:7e:20:10 brd ff:ff:ff:ff:ff:ff
    inet 192.168.115.10/24 brd 192.168.115.255 scope global eth0
    inet6 fe80::20c:29ff:fe7e:2010/64 scope link 
       valid_lft forever preferred_lft forever



于是观察BACKUP上,ip addr show 观察已经获取VIP了。

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:05:ff:b3 brd ff:ff:ff:ff:ff:ff

    inet 192.168.115.20/24 brd 192.168.115.255 scope global eth0

    inet 192.168.115.222/32 scope global eth0

    inet 192.168.115.111/32 scope global eth0

    inet 192.168.115.99/32 scope global eth0

    inet6 fe80::20c:29ff:fe05:ffb3/64 scope link 

       valid_lft forever preferred_lft forever

再用浏览器访问VIP,看是否能问到tomcat页面。

wKiom1RHWlLAlXY1AAcRzH78aTU759.jpg

依然能访问到,说明keepalived起到作用了。
这就是keepalived双机热备,故障转移的表现功能所在。在Nginx+Keepalived的架构上,nginx双
机实现了反向代理的高可用。一台nginx挂掉后,依然不会影响访问。前端负载均衡已经解决了,但是后
端的tomcat服务器挂掉后,该如何呢?keepalived是无法做到对后端服务器的健康检测的。这需要
Nginx的功能。