Nginx+keepalived负载均衡高可用篇第③版
对付中、小型企业,假如没有资金去购买昂贵的四/七层负载均衡交换机,那么Nginx是不错的七层负载均衡选择,并且可以通过Nginx + Keepalived实现Nginx 负载均衡器双机互备,恣意一台呆板发生妨碍,对方都可以或许将假造IP吸取已往。以下内容为第三次更新了,下面将其安装步调具体阐发下:
一、拓扑环境:二、安装keepalivedwget tar zxvf keepalived-1.1.15.tar.gzcd keepalived-1.1.15./configure --prefix=/usr/local/keepalivedmake make installcp /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/keepalivedcd /etc/keepalived/vim keepalived.conf! Configuration File for keepalivedglobal_defs { notification_email {
} notification_email_from 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/messagesOct 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已经启动,我们还可以通过下令来反省[root@ltos html]# ip a1: 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 forever2: 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 forever3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0
阐发vip已经启动,如许主理事器就设置好了,辅机的设置大抵一样,除了设置文件有少部分的变动,下面贴出辅机的设置文件:! Configuration File for keepalivedglobal_defs { notification_email {
} notification_email_from 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 <==vip所在 }}反省其设置[root@ltos html]# ip a1: 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 forever2: 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 forever3: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0
测试其效果行动很大略,判别在主辅机上/var/www/html/下成立差别的主页及内容文件index.html,内容判别为192.168.0.154,192.168.0.155,然后用客户机上elinks ,主机down掉后辅机会立刻接替供给做事,隔断工夫险些无法感伤出来;但假如是停掉master的nginx做事,keepalived就不能主动启动nginx做事了,这个也是相对付heartbeat2.X不完美的地方,即它不能做到做事级别-Nginx的HA。
※这里要阐发的是:一、怎样让master状况的主机挂掉后不抢占的办法:可将二台Nginx负载均衡器均设成backup状况是由于nopreempt(不抢占)只支持backup模式,而让哪一台backup成为master,这个由优先级priority为决议,这个设置是参考了《Keepalived权势巨子指南》做法,二、此种架构的错误是备机做事器处于闲置状况,浪费了硬件资源,假如要有效的操作二台nginx负载均衡器,可参考张宴的金山悠闲网nginx负载均衡的双机热备的做法;三、keepalived比拟heartbeat2.X而言,它如今不能做nginx做事的HA,但由于如今nginx作反向署理/负载均衡器非常稳定,出现宕机的机率微乎其乎(关键是nginx的master进程超稳定),我做的一些基于nginx负载均衡的小项目已在线稳定一年以上(险些从来没出现单nginx负载均衡挂掉的情况),以是这个可以操纵于成熟的生产环境(譬喻新浪);在生产环境下,比起用shell脚本监控nginx的master状况,反而效果不如采用nagios/手机短信报警的行动更为高效。