7.Nginx_Keepalived高可用配置

时间:2022-12-19 11:09:14

2. 利用keepalived实现高可靠配置(HA  

2.1. 高可靠概念

  HA(High Available):高可用性集群,是保证业务连续性的有效解决方案,一般有两个或两个以上的节点,且分为活动节点及备用节点。

 7.Nginx_Keepalived高可用配置

2.2. 高可靠软件keepalived

  keepalive是一款可以实现高可靠的软件,通常部署在2台服务器上,分为一主一备。Keepalived可以对本机上的进程进行检测,一旦Master检测出某个进程出

现问题,将自己切换成Backup状态,然后通知另外一个节点切换成Master状态。

2.3. keepalived安装

  下载keepalived官网:http://keepalived.org

   将keepalived解压到/usr/local/src目录下:tar -zxvf  keepalived-1.2.19.tar.gz -C /usr/local/src

   进入到/usr/local/src/keepalived-1.2.19目录:cd /usr/local/src/keepalived-1.2.19

   开始configure(检查安装环境,并指定将来要安装的路径):./configure --prefix=/usr/local/keepalived    

   #编译并安装:make && make install

2.4. 将keepalived添加到系统服务中  

  拷贝执行文件cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

  将init.d文件拷贝到etc下,加入开机启动项:cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived 

  将keepalived文件拷贝到etc下:cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 

  创建keepalived文件夹:mkdir -p /etc/keepalived

  将keepalived配置文件拷贝到etc下:cp /usr/local/keepalived/etc/keepalived/keepalived.conf  /etc/keepalived/keepalived.conf 

  添加可执行权限:chmod +x /etc/init.d/keepalived  

  添加keepalived到开机启动:chkconfig --add keepalived        chkconfig keepalived on

2.4. 配置keepalived虚拟IP

1.基本服务器宕机的主从切换配置

  7.Nginx_Keepalived高可用配置

  

机器IP

安装软件

角色

虚拟ip

描述

192.168.232.201

Nginx、keepalived

Nginx主机

192.168.232.203

反向代理到tomcat1和tomcat2

192.168.232.205

Nginx、keepalived

Nginx备机

主机挂了切换虚拟ip 192.168.232.203

反向代理到tomcat1和tomcat2

192.168.232.200

Tomcat

Tomcat1

 

192.168.232.204

Tomcat

Tomcat2

 

  两台nginx服务器,分别安装keepalived,配置/etc/keepalived/keepalived.conf

主从配置:

  主nginx修改配置文件: /etc/keepalived/keepalived.conf

  #MASTER节点

#全局配置
global_defs {
   notification_email {   #指定keepalived在发生切换时需要发送email到的对象,一行一个
     XXX@XXX.com
   }
  notification_email_from Alexandre.Cassen@firewall.loc   #指定发件人
   smtp_server 192.168.200.1  #指定smtp服务器地址
   smtp_connect_timeout 30    #指定smtp连接超时时间
   router_id LVS_DEVEL  #运行keepalived机器的一个标识
}
vrrp_instance VI_1 {
    state MASTER     #指定A节点为主节点 备分机上设置为BACKUP即可 interface eth0   #设置实例绑定的网卡 virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组(同一实例下virtual_router_id必须相同) priority 100     #主节点的优先级(1-254之间),备用节点必须比主节点优先级低 advert_int 1      #MASTERBACKUP负载均衡器之间同步检查的时间间隔,单位是秒 authentication { #设置验证信息,两个节点必须一致 auth_type PASS  #主从服务器验证方式 auth_pass 1111 } virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样 192.168.232.203/24 #如果两个nginx的ip分别是192.168.232.201,,...205,则此处的虚拟ip跟它俩同一个网段即可,24子网掩码(255.255.255.0),可以多个虚拟IP,换行即可 } }

备nginx

修改备nginx下/etc/keepalived/keepalived.conf文件

配置备nginx时需要注意:需要修改state为BACKUP , priority比MASTER低,virtual_router_id和master的值一致

#BACKUP节点

global_defs {
}
vrrp_instance VI_1 {
    state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.203/24 } }

#分别启动两台机器上的keepalived:tail  /var/log/message 查看日志。

service keepalived start 

用ip addr查看主从:

主机:

7.Nginx_Keepalived高可用配置

备机:

7.Nginx_Keepalived高可用配置

此时如果主机宕机则(service keepalived stop)备机会顶替主机获得虚拟ip

首先将主机的keepalived关闭:

7.Nginx_Keepalived高可用配置

再次查看主机:ip addr

7.Nginx_Keepalived高可用配置

备机:

7.Nginx_Keepalived高可用配置

至此,基本服务器宕机的主从切换配置完毕

访问虚拟ip是不是还能轮询访问tomcat

7.Nginx_Keepalived高可用配置

 

2.基于nginx进程死掉主从切换配置 

  keepalived是通过检测keepalived进程是否存在判断服务器是否宕机,如果keepalived进程在但是nginx进程不在了那么keepalived是不会做主备切换,所以我们需要写

个脚本来监控nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉。

  在主nginx上需要编写nginx进程检测脚本(check_nginx.sh),判断nginx进程是否存在,如果nginx不存在就将keepalived进程杀掉,check_nginx.sh内容如下:

#!/bin/bash
# 如果进程中没有nginx则将keepalived进程kill掉
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A if [ $A -eq 0 ];then ## 如果没有进程值得为 零 service keepalived stop ## 则结束 keepalived 进程 fi

  将check_nginx.sh拷贝至/etc/keepalived下,

  注意修改/etc/keepalived/check_nginx.sh的可执行权限

  修改主nginx的keepalived.conf,添加脚本定义检测:

  注意下边红色标识地方:

#全局配置
global_defs {
   notification_email {  #指定keepalived在发生切换时需要发送email到的对象,一行一个
     XXX@XXX.com
   }
   notification_email_from miaoruntu@itcast.cn  #指定发件人
   #smtp_server XXX.smtp.com                             #指定smtp服务器地址
   #smtp_connect_timeout 30 #指定smtp连接超时时间 router_id LVS_DEVEL #运行keepalived机器的一个标识 } vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" ##监控脚本 interval 2 ##时间间隔,2秒 weight 2 ##权重 } vrrp_instance VI_1 { state MASTER #标示状态为MASTER 备份机为BACKUP interface eth0 #设置实例绑定的网卡 virtual_router_id 51 #同一实例下virtual_router_id必须相同 priority 100 #MASTER权重要高于BACKUP 比如BACKUP为99 advert_int 1 #MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒 authentication { #设置认证 auth_type PASS #主从服务器验证方式 auth_pass 8888 } track_script { check_nginx #监控脚本 } virtual_ipaddress { #设置vip 192.168.101.100 #可以多个虚拟IP,换行即可 } }

修改后重启keepalived