找回密码
 注册
广告投放 虚位以待【阿里云】2核2G云新老同享 99元/年,续费同价做网站就用糖果主机-sugarhosts.comJtti.com-新加坡服务器,美国服务器,香港服务器
查看: 664|回复: 0

Nginx +keepalived 实现高可用和负载均衡

[复制链接]
发表于 2010 年 10 月 2 日 14:30:55 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×

一、软件安装
1、  安装nginx  参考nginx 安装
2、  安装keepalived 参考keepalived+lvs 的安装
这里提供两个经过我打包的rpm包
1、  nginx 0.7.64  下载
2、  keepalived 1.1.20  下载

二、拓扑结构

二、实现负载均衡和高可用的说明
1、当用户访问test.woyo.com 时,通过DNS 轮循机制,用户的请求会分别分发到两个keepalived 为master 的服务器上。 实现负载均衡的功能

2、keepalived  通过发送和接收组播包中的同一个virtual_router_id 的中的成员的存活,来确定对方的不可用,一旦检测到对方的不可用,即会切换他的备份角色为主,

即:当真实机192.168.200.143上的keepalived 检测到 真实机192.168.200.144上的keepalived 不可用时,143上对 192.168.200.144的VIP 156的backup 会变为master

3、keepalived 只能实现针对网络层的一个高可用,即 一台装有keepalived的进程检测不到另一台装有keepalived 的存活是,才会发生切换。

4、针对应用层的一个检测keepalived 需要通过vrrp_scripts 来实现

三、看如配置文件样例
1、关建点
         1.互为主备关系的两台机器的  vrrp_instance VI_1 里面的 virtual_router_id 52必需相同

1、 143上的keepalived.conf

  1. # Configuration File for keepalived
    # vrrp master master
    global_defs {
    notification_email {
    root@mkrss.com
    }
    notification_email_from root@mkrss.com
    smtp_server localhost
    smtp_connect_timeout 30
    router_id LVS_DEVEL
    }
    vrrp_script chk_nginx {
    script "/tmp/check.sh"
    interval 2
    weight -2
    }
    vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    track_script {
    chk_nginx 10
    }
    virtual_ipaddress {
    192.168.200.145
    }
    }
    vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 53
    priority 80
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.200.146
    }
    }
复制代码

2、 144上的keepalived.conf

  1. # Configuration File for keepalived
    # vrrp master master
    global_defs {
    notification_email {
    root@mkrss.com
    }
    notification_email_from root@mkrss.com
    smtp_server localhost
    smtp_connect_timeout 30
    router_id LVS_DEVEL2
    }
    #vrrp_script chk_nginx {
    # script "/tmp/check.sh"
    # interval 2
    # weight 2
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 52
    priority 80
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    # track_script {
    # chk_nginx
    # }
    virtual_ipaddress {
    192.168.200.145
    }
    }
    vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 53
    priority 180
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 1111
    }
    virtual_ipaddress {
    192.168.200.146
    }
    }
复制代码

三、脚本

  1. #!/bin/sh
    A=`ps -C nginx --no-header |wc -l`
    if [ $A -eq 0 ]
    then
    /usr/local/nginx/sbin/nginx
    sleep 1
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
    then
    killall keepalived
    fi
    fi
复制代码

四、测试方法

1、分别在两台机器上写测试文件两台机器创建测试文件

  1. echo " php server a " > /usr/local/nginx/html/index.php
    echo " php server b " > /usr/local/nginx/html/index.php
复制代码

2、分两在两台机器上启动nginx 和keepalived

  1. Server nginx restart
    Server keepalived restart
复制代码

3、 分别在两台机器上tail –f  /var/log/messages

  1. Jun 13 17:19:35 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
    Jun 13 17:19:35 rs_200_143 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(9,10)]
    Jun 13 17:19:36 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_2) Transition to MASTER STATE
    Jun 13 17:19:37 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_2) Entering MASTER STATE
    Jun 13 17:20:40 rs_200_142 Keepalived_vrrp: VRRP_Instance(VI_2) Entering BACKUP STATE
    Jun 13 17:20:40 rs_200_142 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(9,10)]
    Jun 13 17:20:41 rs_200_142 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
    Jun 13 17:20:42 rs_200_142 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
    Jun 13 17:20:42 rs_200_142 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
复制代码

4、在一台机器上跑下面的语句

  1. for num in {1..10000} ;do curl http://192.168.200.145; echo ;done
    php server a
    php server a
    php server a
复制代码

在143上执行server nginx stop
我们会看到如下

  1. curl: (7) couldn't connect to host
    curl: (7) couldn't connect to host
    约9个
    php server b
    php server b
    php server b
复制代码

同时在143上也会看到如下日志的滚动

  1. un 13 17:19:37 rs_200_143 avahi-daemon[1940]: Registering new address record for 192.168.200.146 on eth0.
    Jun 13 17:19:42 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_2) Sending gratuitous ARPs on eth0 for 192.168.200.146
    Ju 13 17:28:19 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
    Jun 13 17:28:20 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
    Jun 13 17:28:20 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
    Jun 13 17:28:20 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.200.145
    Jun 13 17:28:20 rs_200_143 avahi-daemon[1940]: Registering new address record for 192.168.200.145 on eth0.
    Jun 13 17:28:25 rs_200_143 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.200.145
复制代码



Jgwy.Com - Free Web Hosting Guide & Directory In China since 2001! Jgwy.Net-Jglt.Net
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|金光论坛

GMT+8, 2025 年 2 月 3 日 15:54 , Processed in 0.025958 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表