ubuntu下keepalived 和nginx配置主备切换

版本:ubuntu12.04 desktop

 

 

                   keepalived+nginx双机热备

一、整体介绍

双机热备概念:

从广义上讲,双机热备(双机容错)就是对于重要的服务,使用两台服务器,互相备份,共同执行同一服务。当一台服务器出现故障时,可以由另一台服务器承担服务任务,从而在不需要人工干预的情况下,自动保证系统能持续提供服务双机热备由备用的服务器解决了在主服务器故障时服务不中断的问题。但在实际应用中,可能会出现多台服务器的情况,即服务器集群双机热备一般情况下需要有共享的存储设备。但某些情况下也可以使用两台独立的服务器实现双机热备,需要通过专业的集群软件或双机软件

从狭义上讲,双机热备特指基于active/standby方式的服务器热备。服务器数据包括数据库数据同时往两台或多台服务器写,或者使用一个共享的存储设备。在同一时间内只有一台服务器运行。当其中运行着的一台服务器出现故障无法启动时,另一台备份服务器会通过双机软件的诊测(一般是通过心跳诊断)将standby机器激活,保证应用在短时间内完全恢复正常使用

双机热备的工作原理

故障隔离,简单的讲,高可用(热备)就是一种利用故障点转移的方式来保障业务连续性。其业务的恢复不是在原服务器,而是在备用服务器。热备不具有修复故障服务器的功能,而只是将故障隔离

keepalived+nginx实现高可用性+负载均衡

nginx进程基于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会被Master中止,属于非阻塞式任务模型。

KeepalivedLinux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接。

二者结合,可以构架出比较稳定的软件主辅热备方案

两台服务器做主辅热备,后端可以采用多个realserver(随意扩展)

nginx master : 192.168.0.115    server1服务器

nginx backup : 192.168.0.110    server2服务器

        vip : 192.168.0.222    

结构图如下:

二、关于服务器

1、服务器IP存活检测:

  服务器IP存活检测是由Keepalived自己本身完成的,将2台服务器配置成Keepalived互为主辅关系,任意一方机器故障对方都能够将IP接管过去。

  2、服务器应用服务存活检测:

  一个正常的业务服务,除了保证服务器的状态存活之外,还需要应用业务的存活。在Nginx的进程模型下,可以认为只要Nginx进程存活状态,服务就是正常的,于是只需要做到检测进程存活就能够做到检测服务的存活。Slave进程的健康状态由Nginx自身的Master进程去完成,Master进程的存活可以通过服务器上的专用脚本进行监测,一旦发现Nginx Master进程异常退出,则立即重新启动Nginx进程,如果启动不了,就切换到备机上。

  3、服务器在线维护:

  Keepalived的服务IP通过其配置文件进行管理,依靠其自身的进程去确定服务器的存活状态,如果在需要对服务器进程在线维护的情况下,只需要停掉被维护机器的Keepalived服务进程,另外一台服务器就能够接管该台服务器的所有应用。

三、安装步骤

环境

Server 1  :  ubuntu-server 12.04          192.168.0.115

Server 2  :  userver-server 12.04          192.168.0.110

也可以随便修改网卡地址:# ifconfig  eth0 192.168.0.115  netmask  255.255.255.0

修改eth0地址为192.168.0.115   子网掩码:255.255.255.0

软件包:

pcre-8.32.tar.gz

nginx-1.4.0.tar.gz

bzip2-1.0.6.tar.gz

zlib-1.2.5.tar.gz

安装编译器工具:

apt-get install  gcc  g++  

安装openssl

apt-get  install openssl  libssl-dev libpopt-dev libnl-dev

安装:

1、在两台服务器上安装zlibbzip2 、libtoolpcrenginx

(1)安装libtool

# apt-get install libtool

(2)安装zlib

# tar  -zxvf  zlib-1.2.5.tar.gz

# cd  zlib-1.2.5

# ./configure

# make && make install

(3) 安装bzip

# tar -zxvf  bzip2-1.0.6.tar.gz

# cd bzip2-1.0.6

# mak && mak install

(4)安装pcre

# tar  -zxvf  pcre-8.32.tar.gz

# cd  pcre-8.32

#./configure  --prefix=/usr --enable-utf8  --enable-pcregrep-libbz2  --enable-pcregrep-libz

# make && make install 

(5) 安装nginx

  # tar  -zxvf  nginx-1.4.0.tar.gz

# cd  nginx-1.4.0

# ./configure  --prefix=/usr/local/nginx --with-pcre --user=www --group=www --with-file-aio  --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module  --with-http_stub_status_module  --with-cc-opt=' -O3' 

make && make install

2、安装keepalived-1.2.7

# apt-get  install  keepalived

3、添加操作系统的用户和组

# groupadd  -f  www

# useradd  -g www  www

4、分别在两台机器上编写配置文件

# vim /usr/local/nginx/conf/nginx.conf

内容如下:

user  www www;

worker_processes    1;                    ##启动进程,通常设置为cpu核数
error_log    logs/error.log    notice;         #全局错误日志及PID文件
pid                logs/nginx.pid;
events  {
        worker_connections    1024;  #单个后台worker process进程的最大并发链接数
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
  http  {
        include             mime.types; #设定mime类型,类型由mime.type文件定义
        default_type    application/octet-stream; 

####sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于####普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off####以平衡磁盘与网络I/O处理速度,降低系统的uptime.
        sendfile            on;
        tcp_nopush         on;
        keepalive_timeout    65;  #连接超时时间
        gzip    on;  #开启gzip压缩
        server  {
                listen             80;
                server_name    localhost;

location / {

root  html; ###定义服务器的默认网站根目录位置

index  index.html index.htm; #定义首页索引文件的名称

}
    error_page     500 502 503 504    /50x.html;
    location = /50x.html  {
        root     html;

                 index     index.html index.htm;
                }
        }
}

5、分别在两台机器上创建测试文件

如果在/usr/local/ng inx/conf/nginx.conf不改的话,nginx的默认root/usr/local/nginx/html/

echo "192.168.0.115" > /usr/local/nginx/html/index.html  ##server1

# echo "192.168.0.110" > /usr/local/nginx/html/index.html  ##server2

6、server1 上编辑配置文件(每一个“{”的前面都有一个空格)

#vim  /etc/keepalived.conf      

内容如下(内容中的路径必须都为绝对路径):

vrrp_script chk_http_port  {
                script "/opt/nginx_pid.sh"         ###监控脚本
                interval 2                      ###监控时间
                weight 2                       ###权值
}

global_defs  {

notification_email  {

123@163.com                    #警告邮箱

}

notification_email_from  123@163.com

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id  LVS_Twioo

}
vrrp_instance VI_1  {
        state MASTER                        ### 设置为主
        interface eth0                         ### 监控网卡    
        virtual_router_id 51                    ### 这个两台服务器必须一样
        priority 101                    ### 权重值 MASTRE 一定要高于 BAUCKUP

                                  ###有的地方说是至少要比BACKUP50
        authentication  {
                 auth_type PASS             ### 加密
                 auth_pass eric              ### 加密的密码,两台服务器一定要一样, 不然会出错
        }
        track_script  {
                chk_http_port                   ### 执行监控的服务
        }
        virtual_ipaddress  {
             192.168.0.222                      ###    VIP 地址
        }
}

7、server2 上编辑配置文件(每一个“{”的前面都有一个空格)

vrrp_script chk_http_port  {
                script "/opt/nginx_pid.sh"         ###监控脚本
                interval 2                      ###监控时间
                weight 2                       ###目前搞不清楚
}

global_defs  {

notification_email  {

123@163.com                    ###警告邮箱

}

notification_emali_from  123@163.com

smtp_server  127.0.0.1

smtp_connect_timeout  30

router_id  LVS_twioo

}
vrrp_instance VI_1  {
        state BACKUP                        ### 设置为备机
        interface eth0                         ### 监控网卡    
        virtual_router_id 51                    ### 这个两台服务器必须一样
        priority 50                   ### 权重值BAUCKUP一定要低于MASTER 
        authentication  {
              auth_type PASS             ### 加密
              auth_pass eric               ### 加密的密码,两台服务器一定要 一样,不然会出错
        }
        track_script  {
             chk_http_port                   ### 执行监控的服务
        }
        virtual_ipaddress  {
             192.168.0.222                      ###    VIP 地址
        }
}

8、编辑nginx监控脚本

# vim  /opt/nginx_pid.sh

内容如下:

#!/bin/bash 

# varsion 1.0

A=`ps -C nginx --no-header |wc -l`              
if [ $A -eq 0 ];then                      

       /usr/local/nginx/sbin/nginx

       sleep 3

       if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

             killall keepalived          

       fi
fi 

注意:在写脚本的时候,[  ]的前后都要有空格

   
   然后给脚本加上执行权限:# chmod  755  nginx_pid.sh

这是一个监控脚本,放在Nginx机器上(因为目前主要是由它提供服务),每隔3秒执行一次,用ps -C 命令来收集nginxPID值到底是否为0,如果是0的话(即Nginx进程死掉了),尝试启动nginx进程;如果继续为0,即nginx启动失改,则关闭本机的Keeplaived进程,VIP地址则会由备机接管,当然了,整个网站就会由备机的Nginx来提供服务了,这样保证Nginx进程的高可用。

9、分别在两个服务器启动nginx 和 keepalived

# /usr/local/nginx/sbin/nginx            ##启动nginx

# /etc/init.d/keepalived  restart /stop/start   ##重启keepalived/停止/启动

# service  keepalived  restart/stop/start

10、测试

服务都起来之后,在MASTERBACKUP上分别指向如下命令:

#  ip  a 

就会看到虚拟ipMASTER上绑定了

浏览器中可以访问192.168.0.222,就可以访问到MASTER的页面

然后停掉主机的nginx# killall nginx),如果nginx没有故障,keepalived就会尝试重新启动nginx服务。如果nginx发生故障,无法重新启动,那么就会自动切换到备机上,如果在用192.168.0.222访问的话,就会访问到BACKUP的页面(用# ip a  命令查看,虚拟ip绑定在BACKUP上)

MASTER修复好后,需要手动启动keepalived 程序,以便将服务重新切换回主服务器。

操作中出现的错误:

1、缺少pcre 

./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. 

详细出处参考:http://www.jb51.net/os/Ubuntu/64596.html需要安装pcre库,按照上面方法安装即可

2、缺少openssl

./configure: error: the HTTP cache module requires md5 functions 

from OpenSSL library. You can either disable the module by using 

--without-http-cache option, or install the OpenSSL library into the system, 

or build the OpenSSL library statically from the source with nginx by using 

--with-http_ssl_module --with-openssl=<path> options. 

详细出处参考:http://www.jb51.net/os/Ubuntu/64596.html需要安装openssl包

3、缺少用户

nginx: [emerg] getpwnam(“www”) failed

错误的原因是没有创建www这个用户,应该在服务器系统中添加www用户组和用户www

4、缺少编译器错误

libtool: compile: unrecognized option `-DHAVE_CONFIG_H' 

libtool: compile: Try `libtool --help' for more information. 

make[1]: *** [pcrecpp.lo] Error 1 

make[1]: Leaving directory `/usr/local/src//pcre-8.31' 

make: *** [all] Error 2root@wolfdog-virtual-machine:~/work/pcre-8.12$ libtool -help -DHAVE_CONFIG_H 

The program 'libtool' is currently not installed. You can install it by typing: 

sudo apt-get install libtool  

缺少g++编译器、缺少libtool

5、缺少zlib库,需要安装zlib

./configure: error: the HTTP gzip module requires the zlib library. 

You can either disable the module by using --without-http_gzip_module 

option, or install the zlib library into the system, or build the zlib library 

statically from the source with nginx by using --with-zlib=<path> option.  

参考文章:

http://deidara.blog.51cto.com/400447/302402/

http://hi.baidu.com/cong_rong520/item/ca18c07d6f4413356f29f65c

http://baobeituping.iteye.com/blog/911509

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值