nginx:基于的keepalived高可用方案

keepalived的作用是检测服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。

二. 安装Keepalived(http://www.keepalived.org/download.html)

1、上传或下载 keepalived到 /usr/local/src 目录
2、解压安装
cd /usr/local/src  
tar -zxvf keepalived-1.2.18.tar.gz  
cd keepalived-1.2.18  
./configure --prefix=/usr/local/keepalived  
make && make install
3、将 keepalived 安装成 Linux 系统服务(因为没有使用 keepalived 的默认路径安装(默认是/usr/local),安装完成之后,需要做一些工作)
#复制默认配置文件到默认路径  
mkdir /etc/keepalived  
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/  
#复制 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/
4、设置 keepalived 服务开机启动
chkconfig keepalived on
5、修改 Keepalived 配置文件
MASTER 节点配置文件(192.168.1.11)  
vi /etc/keepalived/keepalived.conf

global_defs {
  ##keepalived自带的邮件提醒需要开启sendmail服务。建议用独立的监控或第三方SMTP
  ##标识本节点的字条串,通常为 hostname
  router_id 192.168.1.11
}
##keepalived会定时执行脚本并对脚本执行的结果进行分析,动态调整vrrp_instance
   的优先级。如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加。
   如果脚本执行结果非0,并且weight配置的值小于 0,则优先级相应的减少。其他情况,
   维持原本配置的优先级,即配置文件中priority对应的值。
vrrp_script chk_nginx {
   script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
   interval 2 ## 检测时间间隔
   weight -20 ## 如果条件成立,权重-20
}
## 定义虚拟路由,VI_1为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
   state MASTER ## 主节点为MASTER,对应的备份节点为BACKUP
   interface eth1 ## 绑定虚拟IP的网络接口,与本机IP地址所在的网络接口相同
   virtual_router_id 11 ## 虚拟路由的ID号,两个节点设置必须一样,建议用IP最后段
   mcast_src_ip 192.168.1.11 ## 本机 IP 地址
   priority 100 ## 节点优先级,值范围0-254,MASTER要比BACKUP高
   nopreempt ## 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题
   advert_int 1 ## 组播信息发送间隔,两个节点设置必须一样,默认 1s
   ## 设置验证信息,两个节点必须一致
   authentication {
      auth_type PASS
      auth_pass 1111
   }
   ## 将 track_script 块加入 instance 配置块
      track_script {
      chk_nginx ## 执行 Nginx 监控的服务
   }
   ## 虚拟 IP 池, 两个节点设置必须一样
   virtual_ipaddress {
      192.168.1.10  ## 虚拟 ip,可以定义多个
   }
}

BACKUP 节点配置文件(192.168.1.12)  
vi /etc/keepalived/keepalived.conf

global_defs {
  router_id 192.168.1.12
}
vrrp_script chk_nginx {
   script "/etc/keepalived/nginx_check.sh"
   interval 2 
   weight -20 
}
vrrp_instance VI_1 {
   state BACKUP
   interface eth1 
   virtual_router_id 11 
   mcast_src_ip 192.168.1.12
   priority 90 
   advert_int 1 
   authentication {
      auth_type PASS
      auth_pass 1111
   }
   track_script {
      chk_nginx 
   }
   virtual_ipaddress {
      192.168.1.10  
   }
}
6、编写 Nginx 状态检测脚本 /etc/keepalived/nginx_check.sh
脚本:如果nginx停止运行,尝试启动,如果无法启动则杀死本机的keepalived进程,
keepalied将虚拟ip绑定到 BACKUP 机器上。内容如下:  
vi /etc/keepalived/nginx_check.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
7、给脚本赋执行权限
chmod +x /etc/keepalived/nginx_check.sh
8、启动 Keepalived
service keepalived start
Starting keepalived: [ OK ]
9、Keepalived+Nginx的高可用测试
(1)关闭 192.168.1.11 中的 Nginx,Keepalived会将它重新启动  
 /usr/local/nginx/sbin/nginx -s stop
(2)关闭 192.168.1.11 中的 Keepalived,VIP 会切换到 192.168.1.12 中  
 service keepalived stop
(3)重新启动 192.168.1.11 中的 Keepalived,VIP 又会切回到 192.168.1.11 中来
 service keepalived start

附Keepalived 服务管理命令:
停止:service keepalived stop
启动:service keepalived start
重启:service keepalived restart
查看状态:service keepalived status

相关推荐