用Keepalived搭建双Nginx server集群,防止单点故障

综述:

浏览器访问虚拟IP: 192.168.1.57, 该虚拟IP被Keepalived接管,两个Keepalived进程分别运行在物理IP为192.168.1.56和192.168.1.59服务器上,这两个服务器上都运行着Nginx server。Nginx server都监听虚拟IP 192.168.1.57. Nginx背后有一些web app集群,已经配置成upstream.

 

为了防止Nginx自己成为单点瓶颈,这里采用了双Nginx server的方式。每个Server都是Ubuntu 12.04. 

假定我的第一台Ubuntu server物理IP地址是192.168.1.56,已经安装了Nginx server,现在安装Keepalived,后面称这台为master server.

 

  1. apt-get install keepalived  


另外一台Ubuntu server物理IP是192.168.1.59, 也安装Nginx server和Keapalived. 后面称这台为backup server. Nginx的安装以及配置不是本文关注内容,请参考我的其他文章。

 

现在开始配置master server. 安装完成keepalived后,通过查看脚本/etc/init.d/keepalived里面发现,启动配置文件的路径是

 

  1. CONFIG=/etc/keepalived/keepalived.conf  

但是该文件现在还不存在。所以我创建一个,内容如下:

 

 

  1. # Settings for notifications                                                                                                                                                                                           
  2. global_defs {  
  3.     notification_email {  
  4.         csfreebird@gmail.com     # Email address for notifications                                                                                                                                                     
  5.     }  
  6.     notification_email_from keepalived@your_company.com  # The from address for the notifications                                                                                                                     
  7.     smtp_server 127.0.0.1  
  8.     smtp_connect_timeout 15  
  9. }  
  10.   
  11. # Define the script used to check if haproxy is still working                                                                                                                                                          
  12. vrrp_script chk_http_port {  
  13.     script "/etc/keepalived/check_nginx.sh" # check Nginx is alive or not                                                                                                                                              
  14.     interval 2 #                                                                                                                                                                                                       
  15.     weight 2  
  16. }  
  17.   
  18.   
  19. # Configuation for the virtual interface                                                                                                                                                                               
  20. vrrp_instance VI_1 {  
  21.     interface eth0  
  22.     state MASTER        # set this to BACKUP on the other machine                                                                                                                                                      
  23.     priority 101        # set this to 100 on the other machine                                                                                                                                                         
  24.     virtual_router_id 51  
  25.   
  26.     smtp_alert          # Activate email notifications                                                                                                                                                                 
  27.   
  28.     authentication {  
  29.     auth_type PASS  
  30.     auth_pass 1111      # Set this to some secret phrase                                                                                                                                                           
  31.     }  
  32.   
  33.     # The virtual ip address shared between the two loadbalancers                                                                                                                                                      
  34.     virtual_ipaddress {  
  35.     192.168.1.57  
  36.     }  
  37.   
  38.     # Use the script above to check if we should fail over                                                                                                                                                             
  39.     track_script {  
  40.     chk_http_port  
  41.     }  
  42. }  

说明:

 

1. smtp_server必须要用127.0.0.1

2. 自己要创建一个检查nginx进程的脚本

  1. /etc/keepalived/check_nginx.sh  

内容如下:

 

 

  1. !/bin/bash  
  2.   
  3. # try to start nginx if nginx process is dead                                                                      
  4. # shutdonw keepalived process if start nginx failed                                         
  5.   
  6. pid=`ps -C nginx --no-header |wc -l`  
  7. if [ $pid -eq 0 ];then  
  8.     service nginx start  
  9.     sleep 3  
  10.     if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
  11.         service keepalived stop  
  12.     fi  
  13. fi  

3. 修改nginx的所有server配置,将server_name都改为虚拟IP

 

 

  1. server_name  192.168.1.57;  

 

 

4. 注意,这里的虚拟IP不是用修改/etc/network/interfaces的方式,而是在keepalived配置文件中直接设置,要想判断是否成功,直接ping 就行了,ifconfig是看不到的。

 

  1. ping 192.168.1.57  
  2. PING 192.168.1.57 (192.168.1.57) 56(84) bytes of data.  
  3. 64 bytes from 192.168.1.57: icmp_req=1 ttl=64 time=0.024 ms  
  4. 64 bytes from 192.168.1.57: icmp_req=2 ttl=64 time=0.020 ms  

 

 

对192.168.1.59做相同的配置,略有变化的是:

 

  1. vrrp_instance VI_1 {  
  2.     interface eth0  
  3.     state BACKUP  // changed  
  4.     priority 100  // changed  

 

通过轮流关闭master和backup,证明keepalived已经有效工作了。

 

参考资料:Keepalived官方文档真是够差的。

http://www.leaseweblabs.com/2011/09/setting-up-keepalived-on-ubuntu-load-balancing-using-haproxy-on-ubuntu-part-2/

http://blog.csdn.net/zmj_88888888/article/details/8825471

posted on 2015-04-22 13:49  为努力骄傲  阅读(368)  评论(0编辑  收藏  举报

导航