Keepalived+nginx 实现双机主从互备

摘要

Keepalived 主页

http://www.keepalived.org/

nginx主页

http://nginx.org/

用Keepalived来实现HA功能,实现主从互备,通过VIP来进行切换,nginx来进行对realserver的七层负载均衡或者是cache功能,或者realserver直接对外,通过DNS对VIP进行轮训。

VIP:192.18.1.222

nginx_master:192.18.1.36

nginx_slave:192.18.1.37

下面开始安装keepalived

wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

#tar zxvf keepalived-1.1.15.tar.gz

#cd keepalived-1.1.15

#./configure  && make && make install

#find / -name keepalived  # 查看keepalived位置

#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

#mkdir /etc/keepalived

#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

#cp /usr/local/sbin/keepalived /usr/sbin/

#service keepalived start|stop     #做成系统启动服务方便管理.

下面进行keepalived进行配置,拿两台机器进行主从互备配置nginx_master  and nginx_slave

nginx_master配置

01 global_defs {

02     notification_email {
03         admin@arezone.cn
04     }

05     notification_email_from keepalived@arezone.cn
06     smtp_server 127.0.0.1

07     smtp_connect_timeout 30

08     route_id LVS_DEVEL

09 }
10

11 vrrp_instance VI_120 {
12     state MASTER

13     interface eth1

14     virtual_router_id 50
15     priority 200
16     advert_int 1

17     smtp_alert
18     garp_master_delay 1

19     preempt_delay 1

20     authentication {
21         auth_type PASS

22         auth_pass keepalived

23     }
24     virtual_ipaddress {

25 e>       192.168.1.222

26     }
27 }

28

29 }

nginx_slave的keepalived.conf配置文件

01 global_defs {
02     notification_email {

03         admin@arezone.cn

04     }
05     notification_email_from keepalived@arezone.cn
06     smtp_server 127.0.0.1

07     smtp_connect_timeout 30

08     route_id LVS_DEVEL

09 }

10

11 vrrp_instance VI_1 {
12     state BACKUP

13     interface eth1

14     virtual_router_id 50
15     priority 50

16     advert_int 1

17     smtp_alert
18     garp_master_delay 1

19     preempt_delay 1

20     authentication {
21         auth_type PASS

22         auth_pass keepalived

23     }
24     virtual_ipaddress {

25       192.168.1.222
26     }

27 }

28

29 }

启动keepalived ,ip a 进行查看vip,查看系统日志,有问题解决问题,没问题当然更好,至此keepalived配置完毕。

nginx配置我在这里就不多说了,网上一搜一大堆。此配置我们已经在线上跑了半年多没出过问题,而且很稳定,

简单总结,当nginx_master down机时,192.168.1.36会自动切换到nginx_slave上,当nginx_slave down机时,192.168.1.37会自动切换到nginx_master上,期间不会影响到服务,keepalived切换速度非常快,linux下ping可能会出现丢一个包的情况,一般都是有一跳延迟大一些,但不会丢包,好了先罗嗦到这.

Keepalived 主页

http://www.keepalived.org/

nginx主页

http://nginx.org/

用Keepalived来实现HA功能,实现主从互备,通过VIP来进行切换,nginx来进行对realserver的七层负载均衡或者是cache功能,或者realserver直接对外,通过DNS对VIP进行轮训。

VIP:192.18.1.222

nginx_master:192.18.1.36

nginx_slave:192.18.1.37

下面开始安装keepalived

wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

#tar zxvf keepalived-1.1.15.tar.gz

#cd keepalived-1.1.15

#./configure  && make && make install

#find / -name keepalived  # 查看keepalived位置

#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

#mkdir /etc/keepalived

#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

#cp /usr/local/sbin/keepalived /usr/sbin/

#service keepalived start|stop     #做成系统启动服务方便管理.

下面进行keepalived进行配置,拿两台机器进行主从互备配置nginx_master  and nginx_slave

nginx_master配置

01 global_defs {

02     notification_email {
03         admin@arezone.cn
04     }

05     notification_email_from keepalived@arezone.cn
06     smtp_server 127.0.0.1

07     smtp_connect_timeout 30

08     route_id LVS_DEVEL

09 }
10

11 vrrp_instance VI_120 {
12     state MASTER

13     interface eth1

14     virtual_router_id 50
15     priority 200
16     advert_int 1

17     smtp_alert
18     garp_master_delay 1

19     preempt_delay 1

20     authentication {
21         auth_type PASS

22         auth_pass keepalived

23     }
24     virtual_ipaddress {

25 e>       192.168.1.222

26     }
27 }

28

29 }

nginx_slave的keepalived.conf配置文件

01 global_defs {
02     notification_email {

03         admin@arezone.cn

04     }
05     notification_email_from keepalived@arezone.cn
06     smtp_server 127.0.0.1

07     smtp_connect_timeout 30

08     route_id LVS_DEVEL

09 }

10

11 vrrp_instance VI_1 {
12     state BACKUP

13     interface eth1