Keepalived+LVS+Nginx负载均衡之高可用

上一篇写了nginx负载均衡,此篇实现高可用(HA)。系统总体设计是采用Nginx作负载均衡,若出现Nginx单机故障,则致使整个系统没法正常运行。针对系统架构设计的高可用要求,咱们须要解决Nginx负载均衡出现单机故障时,系统正常运行的需求。因此系统架构引入Keepalived组件,实现系统高可用。nginx

  1、Keepalived介绍web

   Keepalived是分布式部署系统解决系统高可用的软件,结合LVS(Linux Virtual Server)使用,其功能相似于heartbeat,解决单机宕机的问题。shell

  2、Keepalived技术原理vim

   keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。经过VRRP协议结合LVS,对组群服务器监控状况,若master出现宕机状况,则将VIP漂移到backup机上。实现了分布式系统高可用。能够理解为:keepalived是LVS的管理软件,根据监控状况,将宕机服务器从ipvsadm移除掉。bash

  3、Keepalived+LVS+Nginx实现系统高可用服务器

  

 

服务器 IP地址 说明
虚拟IP 192.168.1.120:80  
主机 192.168.1.104:80  
备机 192.168.1.103:80  
Web站点A 192.168.1.101:8081 不一样端口
Web站点B 192.168.1.101:8082 不一样端口

一、安装ipvsadm,CentOS7自带安装包,经过yum进行安装。实现系统支持LVS架构

yum install ipvsadm

 二、安装Keepalived软件,并将keepalived设置开机启动负载均衡

yum install Keepalived

 

systemctl enable keepalived

三、进行Keepalived.conf配置,若是是MASTER机,将state BACKUP改成state MASTER。分布式

 

vim /etc/keepalived/keepalived.conf

#配置的内容

! Configuration File for keepalived



global_defs {

   notification_email {

     xxx@126.com #收到通知的邮件地址

   }

   notification_email_from XX@126.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}



vrrp_script monitor_nginx{

   script "/usr/local/etc/keepalived/script/monitor_nginx.sh"

   interval 1

   weight -15

}



vrrp_instance VI_1 {

    state BACKUP

    interface eno16777736

    virtual_router_id 51

    priority 80

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.120

    }

    track_script {

        monitor_nginx

    }

}



virtual_server 192.168.1.120 80 {

    delay_loop 6

    lb_algo wrr

    lb_kind DR

    persistence_timeout 50

    protocol TCP



    real_server 192.168.1.103 80 {

        weight 1

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }



    real_server 192.168.1.104 80 {

        weight 5

        TCP_CHECK {

            connect_timeout 10

            nb_get_retry 3

            delay_before_retry 3

            connect_port 80

        }

    }

}

 

  四、配置监控shell脚本oop

(1)建立:vim /usr/local/etc/keepalived/script/monitor_nginx.sh<br><br>(2)SHELL文件内容<br>
#!/bin/bash 

if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]

then 

    systemclt start nginx.service

    sleep 5   

  if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ] 

  then  

    killall keepalived 

  fi 

fi

 

  以上完成相关配置,nginx和web服务以上一篇博客内容一致。以下对功能进行验证测试。

 

 

  4、实现测试展现

   一、访问系统状况:经过VIP(192.168.1.120)访问系统页面。由于设置了轮询调度,因此刷新页面访问不一样站点。

        

 

  二、将 MASTER(192.168.1.104)关机先后,查看相关VLS状况:

  (1)关机前:

  (2)关机后:

  咱们看到将104服务器从 LVS移除掉。此时则将后续请求转发到103服务器。

 

  三、关机后,BACKUP服务器 keepalived日志显示没法链接104,并移除掉

  

  五、开机后,将自动检测到服务器正常,并加入LVS中。

  

相关文章
相关标签/搜索