nginx high availability cluster solution based on keepalived

nginx high availability solution based on keepalived

Keepalived is a high-performance server high-availability or hot-standby solution, which can be used to prevent the occurrence of a single point of failure of the server. By configuring Nginx, the high availability of web front-end services can be achieved.

Note: It seems that Alibaba Cloud's ECS server cannot use keepalived (VIP cannot be obtained). In this case, it is recommended to use Alibaba Cloud's SLB, which is simple and worry-free.

Related articles:
Example project:  http://wosyingjun.iteye.com/blog/2312553 
Teach you how to know and build Nginx: http://wosyingjun.iteye.com/blog/2252941
Basic configuration and optimization of Nginx: http:/ /wosyingjun.iteye.com/blog/2294512

1. Install and configure nginx

See related article above.

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

1. Upload or download keepalived to the /usr/local/src directory
2. Unzip the installation
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. Install keepalived as a Linux system service (because the default path of keepalived is not installed (default is /usr/local), after the installation is complete, some work needs to be done)
#复制默认配置文件到默认路径  
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. Set the keepalived service to start at boot
chkconfig keepalived on  
5. Modify the Keepalived configuration file
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. Write the Nginx status detection script /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. Give the script execution permission
chmod +x /etc/keepalived/nginx_check.sh
8. Start Keepalived
service keepalived start
Starting keepalived: [ OK ]
9. High availability test of 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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326761115&siteId=291194637