Nginx+Keepalived实现站点高可用(主备)

原创
2016/05/15 23:09
阅读数 223

192.168.1.99  vip

192.168.1.111  nginx+keepalived

192.168.1.112  nginx+keepalived

一、分别在两台机器上安装nginx

请参考:http://my.oschina.net/chaun/blog/493769

二、分别在两台安装及配置keepalived

安装GCC编译器等工具:
# yum install -y gcc gcc-c++ autoconf automake libtool make openssl openssl-devel

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

# cd /usr/local/src/
# tar -zxvf keepalived-1.2.20.tar.gz
# cd keepalived-1.2.20
# ./configure --prefix=/usr/local/keepalived
# make && make install
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# ln -s /usr/local/sbin/keepalived /usr/sbin/
# ln -s /usr/local/keepalived/sbin/keepalived /sbin/
# mkdir -p /etc/keepalived
# cd /etc/keepalived/

主节点的keepalived配置文件添加如下内容:
# vim keepalived.conf

global_defs { 
    notification_email { 
                    12346789@xx.com 
    } 
   notification_email_from admin@haproxy01.com 
   smtp_server xxx.xxx.xxx.xxx 
   smtp_connect_timeout 30 
   router_id keepalived-master  #hostname

vrrp_script chk_http_port {
   script "/etc/keepalived/check_ng.sh"
   interval 2                   #检测的间隔
   weight 2
}

  
vrrp_instance VI_1 { 
            state MASTER #标示状态为MASTER
            interface eth0 
            virtual_router_id 51 
            mcast_src_ip 192.168.1.111  (主服务器的IP)
            priority 100 #MASTER权重要高于BACKUP
            nopreempt
            advert_int 1 
            authentication { 
                    auth_type PASS #主从服务器验证方式
                    auth_pass aaaaaa 
            } 
           track_script {
                   chk_http_port            (调用检测脚本)
           }
            virtual_ipaddress { 
                  192.168.1.99   #绑定的虚IP
            } 
}  

vim /etc/keepalived/check_ng.sh

#!/bin/bash
A='ps -C nginx --no-header |wc -l'        
if [ $A -eq 0 ];then                            
      /usr/local/nginx/sbin/nginx
      sleep 2
      if [ 'ps -C nginx --no-header |wc -l' -eq 0 ];then  
              killall keepalived                    
      fi
fi

给脚本执行权限

# chmod 777 /etc/keepalived/check_ng.sh

从节点的keepalived配置文件添加如下内容:
# vim keepalived.conf

global_defs { 
    notification_email { 
                    12346789@xx.com 
    } 
   notification_email_from admin@haproxy01.com 
   smtp_server xxx.xxx.xxx.xxx 
   smtp_connect_timeout 30 
   router_id keepalived-slave  #hostname

vrrp_script chk_http_port {
   script "/etc/keepalived/check_ng.sh"
   interval 2                   #检测的间隔
   weight 2
}

  
vrrp_instance VI_1 { 
            state BACKUP #标示状态为BACKUP
            interface eth0 
            virtual_router_id 51 
            mcast_src_ip 192.168.1.112  (从服务器的IP)
            priority 80 #MASTER权重要高于BACKUP
            nopreempt
            advert_int 1 
            authentication { 
                    auth_type PASS #主从服务器验证方式
                    auth_pass aaaaaa 
            } 
           track_script {
                   chk_http_port            (调用检测脚本)
           }
            virtual_ipaddress { 
                  192.168.1.99   #绑定的虚IP
            } 
}  

该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix

vim /etc/keepalived/check_ng.sh

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    sleep 2
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi

给脚本执行权限

# chmod 777 /etc/keepalived/check_ng.sh

分别在两台节点上启动keepalived

# /etc/init.d/keepalived start 
或者
# service keepalived start

可以将192.168.1.111的nginx关闭,这时通过脚本会自动重启nginx

开始nginx负载均衡测试,停掉其中一台的任何服务,不影响整个系统的运作

参考:

http://pesen.blog.51cto.com/4575807/819246 

http://www.ahlinux.com/centos/20076.html 

http://debugo.com/keepalived-nginx/ 

http://my.oschina.net/HeAlvin/blog/552469 

keepalived配置文件参考:

http://my.oschina.net/HeAlvin/blog/542471

上面的模式是主备模式,双主配置请参考:

http://blog.csdn.net/zhu_tianwei/article/details/43603771

展开阅读全文
打赏
2
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
2
分享
返回顶部
顶部