码迷,mamicode.com
首页 > 其他好文 > 详细

nginx+keepalived实现高可用

时间:2017-09-13 22:10:27      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:src   kconfig   rest   master   span   over   负载   can   from   

  (1)准备三台服务器

    正常来说,应该准备四台,两台安装ngnix+keeplived实现高可用以及负载均衡,两台做节点服务器,这里78充当一台节点服务器,172.31.113.77(nginx+keepalived),172.31.113.78(nginx+keepalived+tomcat),172.31.113.79(tomcat)。

  (2)安装nginx服务器

    77,78服务器安装ngnix以及配置负载均衡可以查看之前的博文。

  (3)77节点,安装keepalived

    1.首先安装keepalived rpm包:popt 以及popt-devel。

    2.下载keepalived安装包

cd /usr/local

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

    3.解压并编译keepalived

tar zvxf /usr/local keepalived-1.2.8.tar.gz

cd /usr/local/keepalived-1.2.8

./configure --prefix=/usr/local/keepalived --sysconf=/etc

    执行编辑的时候会报如下Openssl错误:

configure: error:  
  !!! OpenSSL is not properly installed on your system. !!!  
  !!! Can not include OpenSSL headers files.            !!!  

    解决方案:http://kusix.iteye.com/blog/1226892

    4.编译并安装

make && make install

    5.启动keepalived

cp /usr/local/keepalived/sbin/keepalived  /bin/

chkconfig --add keepalived

#设置开机启动
chkconfig keepalived on

#启动keepalive服务
/etc/init.d/keepalived start

  (4)配置keepalived

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak

vim /etc/keepalived/keepalived.conf

    (1)配置MASTER节点

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
         script "/root/monitor_nginx.sh" #执行脚本路径
         interval 2
         weight 2
}
vrrp_instance VI_1 {
    state MASTER  #标识为MASTER节点,从节点标识BACKUP
    interface eth0
    virtual_router_id 51 #两个节点的ID需要一致
    priority 100  #主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.31.113.110 #虚拟IP
    }
        track_script {
                Monitor_Nginx #执行脚本名字
        }
 }

    (2)编辑监控脚本monitor_nginx.sh,vim /root/monitor_nginx.sh,内容如下,并赋予执行权限:chmod  +x  /root/monitor_nginx.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
   # /usr/local/nginx/sbin/nginx
   # sleep 2
   # counter=$(ps -C nginx --no-heading|wc -l)
   # if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
   # fi
fi

  到此,keepalived的安装以及配置结束,78备用节点重复上面的操作,只是把配置文件keepalived.conf中的state修改为BACKUP , priority比MASTER稍低即可。

  (5)分别启动keepalived,nginx,以及tomcat服务器。

service keepalived restart

   在主服务器即78节点,可以通过 ip a查看虚拟IP,显示如下:

    技术分享

  (6)测试nginx+keepalive实现高可用

    访问172.31.113.110/index.jsp,首先将77服务器的nginx进程杀掉,查看77的keepalived进行也被停止掉了(因为上面我们的脚本当nginx为零的时候,停止keepalived,只有停止keepalived,78备用服务器才能得到虚拟IP),这时候查看78的虚拟IP也为172.31.113.110,继续对外提供服务。

   总结:这种方案只能实现当主节点的nginx进程结束了,主节点的脚本monitor_nginx.sh执行后,停止keepalived进程,备用节点才能得到虚拟IP,进而对外继续提供服务。后面测试了一下,直接关闭主节点,备用节点无法得到虚拟IP,无法对外服务。所以当主节点直接宕机的话,这种方案就不可行了,难道这种方案就是高可用?百度搜了好久,都是这样实现的。

    

nginx+keepalived实现高可用

标签:src   kconfig   rest   master   span   over   负载   can   from   

原文地址:http://www.cnblogs.com/gdpuzxs/p/7517742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!