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


用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.
[plain] 
apt-get install keepalived  
 
另外一台Ubuntu server物理IP是192.168.1.59, 也安装Nginx server和Keapalived. 后面称这台为backup server. Nginx的安装以及配置不是本文关注内容,请参考我的其他文章。
现在开始配置master server. 安装完成keepalived后,通过查看脚本/etc/init.d/keepalived里面发现,启动配置文件的路径是
[plain] 
CONFIG=/etc/keepalived/keepalived.conf  
但是该文件现在还不存在。所以我创建一个,内容如下:
[plain] 
# Settings for notifications                                                                                                                                                                                           
global_defs {  
    notification_email {  
        csfreebird@gmail.com     # Email address for notifications                                                                                                                                                     
    }  
    notification_email_from keepalived@your_company.com  # The from address for the notifications                                                                                                                     
    smtp_server 127.0.0.1  
    smtp_connect_timeout 15  
}  
  
# Define the script used to check if haproxy is still working                                                                                                                                                          
vrrp_script chk_http_port {  
    script "/etc/keepalived/check_nginx.sh" # check Nginx is alive or not                                                                                                                                              
    interval 2 #                                                                                                                                                                                                       
    weight 2  
}  
  
  
# Configuation for the virtual interface                                                                                                                                                                               
vrrp_instance VI_1 {  
    interface eth0  
    state MASTER        # set this to BACKUP on the other machine                                                                                                                                                      
    priority 101        # set this to 100 on the other machine                                                                                                                                                         
    virtual_router_id 51  
  
    smtp_alert          # Activate email notifications                                                                                                                                                                 
  
    authentication {  
    auth_type PASS  
    auth_pass 1111      # Set this to some secret phrase                                                                                                                                                           
    }  
  
    # The virtual ip address shared between the two loadbalancers                                                                                                                                                      
    virtual_ipaddress {  
    192.168.1.57  
    }  
  
    # Use the script above to check if we should fail over                                                                                                                                                             
    track_script {  
    chk_http_port  
    }  
}  
说明:
1. smtp_server必须要用127.0.0.1
2. 自己要创建一个检查nginx进程的脚本
[plain] 
/etc/keepalived/check_nginx.sh  
内容如下:
[plain] 
!/bin/bash  
  
# try to start nginx if nginx process is dead                                                                      
# shutdonw keepalived process if start nginx failed                                         
  
pid=`ps -C nginx --no-header |wc -l`  
if [ $pid -eq 0 ];then  
    service nginx start  
    sleep 3  
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then  
        service keepalived stop  
    fi  
fi  
3. 修改nginx的所有server配置,将server_name都改为虚拟IP
[plain] 
server_name  192.168.1.57;  
 
4. 注意,这里的虚拟IP不是用修改/etc/network/interfaces的方式,而是在keepalived配置文件中直接设置,要想判断是否成功,直接ping 就行了,ifconfig是看不到的。
[plain] 
ping 192.168.1.57  
PING 192.168.1.57 (192.168.1.57) 56(84) bytes of data.  
64 bytes from 192.168.1.57: icmp_req=1 ttl=64 time=0.024 ms  
64 bytes from 192.168.1.57: icmp_req=2 ttl=64 time=0.020 ms  
 
对192.168.1.59做相同的配置,略有变化的是:
[plain] 
vrrp_instance VI_1 {  
    interface eth0  
    state BACKUP  // changed  
    priority 100  // changed  
通过轮流关闭master和backup,证明keepalived已经有效工作了。
 

相关内容

    暂无相关文章