免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3046 | 回复: 2
打印 上一主题 下一主题

Nginx+keepalived负载均衡篇 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-17 11:28 |只看该作者 |倒序浏览
由于nginx的url hash功能可以很好的提升squid的性能,所以我把squid前端的负载均衡器更换为nginx,但是一台nginx就形成了单点,现在使用keepalived来解决这个问题,keepalived的故障转移时间很短,而且配置简单,这也是选择keepalived的一个主要原因,建议日PV值小的中小型企业web均可采用如下方案实行,下面直接上安装步骤:



一、环境:
centos5.3、nginx-0.7.51、keepalived-1.1.19
主nginx负载均衡器:192.168.0.154
辅nginx负载均衡器:192.168.9.155
vip:192.168.0.188



二、安装keepalived


#tar zxvf keepalived-1.1.19.tar.gz
#cd keepalived-1.1.19
#./configure --prefix=/usr/local/keepalived
#make
#make install
#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
#mkdir /etc/keepalived
#cd /etc/keepalived/

vim keepalived.conf



! Configuration File for keepalived
global_defs {
    notification_email {
    yuhongchun027@163.com
         }
    notification_email_from keepalived@chtopnet.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
     state MASTER
     interface eth0
     virtual_router_id 51
     mcast_src_ip 192.168.0.154    <==主nginx的IP地址
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass chtopnet
     }
     virtual_ipaddress {
         192.168.0.188                      <==VIP地址
     }
}

#service keepalived start
我们来看一下日志:
[root@ltos ~]# tail /var/log/messages


Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:03 ltos avahi-daemon[2306]: Registering HINFO record with values 'I686'/'LINUX'.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Withdrawing address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Withdrawing address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Host name conflict, retrying with <ltos-31>
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for fe80::20c:29ff:feb9:eeab on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.188 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering new address record for 192.168.0.154 on eth0.
Oct 6 03:25:23 ltos avahi-daemon[2306]: Registering HINFO record with values 'I686'/'LINUX'.



很显然vrrp已经启动,我们还可以通过命令:#ip a 来检查

[root@ltos html]# ip a


1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
     inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
     link/ether 00:0c:29:ba:9b:e7 brd ff:ff:ff:ff:ff:ff
     inet 192.168.0.154/24 brd 192.168.0.255 scope global eth0
     inet 192.168.0.188/32 scope global eth0
     inet6 fe80::20c:29ff:feba:9be7/64 scope link
        valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
     link/sit 0.0.0.0 brd 0.0.0.0


说明vip已经启动,这样主服务器就配置好了,辅机的配置大致一样,除了配置文件有少部分的变化,下面贴出辅机的配置文件:



! Configuration File for keepalived
global_defs {
    notification_email {
    yuhongchun027@163.com
         }
    notification_email_from keepalived@chtopnet.com
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
     state BACKUP
     interface eth0
     virtual_router_id 51
     mcast_src_ip 192.168.0.155             <==从nginx的IP的地址
     priority 100
     advert_int 1
     authentication {
         auth_type PASS
         auth_pass chtopnet
     }
     virtual_ipaddress {
         192.168.0.188
     }
}



检查其配置
[root@ltos html]# ip a


1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
     inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
     link/ether 00:0c:29:ba:9b:e7 brd ff:ff:ff:ff:ff:ff
     inet 192.168.0.155/24 brd 192.168.0.255 scope global eth0
     inet 192.168.0.188/32 scope global eth0
     inet6 fe80::20c:29ff:feba:9be7/64 scope link
        valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
     link/sit 0.0.0.0 brd 0.0.0.0


测试其效果方法很简单,分别在主辅机上建立不同的主页,index.html的内容分别为192.168.0.154,192.168.0.155,然后用客户机上elinks http://192.168.0.188,主机down掉后辅机会马上接替提供服务,间隔时间几乎无法感觉出来,

论坛徽章:
0
2 [报告]
发表于 2012-07-31 20:34 |只看该作者
这么做我发现了一个问题 就是假如服务DOWN了 但网络正常通信 服务是不会飘过来的

论坛徽章:
0
3 [报告]
发表于 2013-07-02 10:50 |只看该作者
还是做一个脚本来检测服务,可以去查询一下 keepalived 中执行脚本就可以解决楼上的问题
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP