Keepalived+Nginx 实现web高可用

keepalived高可用集群

高可用集群,即HA集群,也叫做双机热备

VRRP协议是实现路由高可用的一种通信协议,这个协议里会将多台功能相同的路由器组成一个小组,这个小组里会有一个master角色和N个backup角色。工作时master会通过组播的形式向各个backup发痛VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时需要根据各个backup的优先级来决定谁成为新的master。

keepalived是采用的VRRP协议(虚拟路由冗余协议)实现的高可用。keepalived主要有三个模块,分别是core、check和vrrp

core模块为keepalived的核心,主要负责主进程的启动、维护以及全局配置文件的加载和解析。

check模块负责健康检查。

vrrp模块用来实现VRRP协议

安装keepalived

yum install -y keepalived

环境:

master:192.168.153.134 安装keepalived+nginx

backup:192.168.153.135 安装keepalived+nginx

VIP:192.168.153.100

VIP  “Virtual IP”即虚拟IP,也叫做浮动IP。这个IP是由服务器配置上的,服务器靠这个VIP对外提供服务,当master机器宕机,VIP会被分配到backup上,这样用户看来是无感的。

 

编辑master的keepalived配置文件

vim /etc/keepalived/keepalived.conf

global_defs {

   notification_email {

    txwdwyq@163.com       //定义接收告警的人

   }

   notification_email_from 645321784@qq.com //定义发邮件地址(实际上没用)

   smtp_server 127.0.0.1           //定义发邮件地址,若为127.0.0.1则使用本机自带邮件服务器发送

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_scriptchk_nginx {            //chk_nginx为自定义名字

    script "/usr/local/sbin/check_ng.sh"   //自定义脚本,为监控nginx服务的脚本

    interval 3   //每隔三秒执行一次该脚本

}

vrrp_instance VI_1{

    state MASTER          //角色为master

    interfaceens33      //针对哪个网卡监听VIP

    virtual_router_id 51 

    priority 100          //权重为100,master要比backup大

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123456   //自定义密码

    }

   virtual_ipaddress {

        192.168.153.100  //定义VIP

    }

    track_script {

        chk_nginx   //定义监控脚本,和上面的vrr_script后面的字符串保持一致

    }

}


定义监控nginx监控服务的脚本

vim /usr/local/sbin/check_ng.sh

#!/bin/bash

#时间变量,用于记录日志

d=`date --datetoday +%Y%m%d_%H:%M:%S`

#计算nginx进程数量

n=`ps -C nginx--no-heading|wc -l`

#如果进程为0,则启动nginx,并且再次检测nginx进程数量,

#如果还为0,说明nginx无法启动,此时需要关闭keepalived

if [  $n  -eq  "0"  ]; then

        /etc/init.d/nginx start

        n2=`ps -C nginx --no-heading|wc -l`

        if [  $n2 -eq "0"  ]; then

                echo "$d nginxdown,keepalived will stop" >> /var/log/check_ng.log

                systemctl stop keepalived

        fi

fi


chmod a+x /usr/local/sbin/check_ng.sh  需要赋予x的权限不然keepalived无法调用

systemctl start keepalived

启动keepalived

ip addr

inet192.168.153.134/24 brd 192.168.153.255 scope global ens33

       valid_lft forever preferred_lft forever

inet192.168.153.100/32 scope global ens33

       valid_lft forever preferred_lft forever

 

可以看到master伤已经自动配置了192.168.153.100这个IP

 

配置backup的配置文件

vim /etc/keepalived/keepalived.conf 

内容和Master大致相同,state和priority有变化

global_defs {

   notification_email {

    txwdwyq@163.com

   }

   notification_email_from 645321784@qq.com

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_nginx {

    script "/usr/local/sbin/check_ng.sh"

    interval 3

}

vrrp_instance VI_1{

    state BACKUP

    interface ens33

    virtual_router_id 51

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 123456

}

virtual_ipaddress {

       192.168.153.100

    }

    track_script {

       chk_nginx

    }

}

 

vim /usr/local/sbin/check_ng.sh

#!/bin/bash

#时间变量,用于记录日志

d=`date --date today +%Y%m%d_%H:%M:%S`

#计算nginx进程数量

n=`ps -C nginx --no-heading|wc -l`

#如果进程为0,则启动nginx,并且再次检测nginx进程数量,

#如果还为0,说明nginx无法启动,此时需要关闭keepalived

if [ $n -eq "0" ]; then

       systemctl start nginx

        n2=`ps-C nginx --no-heading|wc -l`

        if [ $n2-eq "0"  ]; then

               echo "$d nginx down,keepalived will stop" >>/var/log/check_ng.log

               systemctl stop keepalived

        fi

fi

 chmod a+x /usr/local/sbin/check_ng.sh

 为了区分版本,backup使用yum安装nginx

yum install -y epel-release

yum install -y nginx

systemctl start keepalived

systemctl start nginx

153.134下的nginx是编译安装的

curl -I 192.168.153.134

HTTP/1.1 200 OK

Server: nginx/1.6.3

Date: Sat, 20 Jan 2018 09:40:23 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Sat, 20 Jan 2018 08:59:26 GMT

Connection: keep-alive

ETag: "5a6304ee-264"

Accept-Ranges: bytes

153.135下的nginx是yum安装的

curl -I 192.168.153.135

HTTP/1.1 200 OK

Server: nginx/1.12.2

Date: Sat, 20 Jan 2018 09:41:36 GMT

Content-Type: text/html

Content-Length: 3700

Last-Modified: Wed, 18 Oct 2017 08:08:18 GMT

Connection: keep-alive

ETag: "59e70bf2-e74"

Accept-Ranges: bytes

在master和backup上 curl-I 192.168.153.100结果都是在master上:

HTTP/1.1 200 OK

Server: nginx/1.6.3

Date: Sat, 20 Jan 2018 09:41:43 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Sat, 20 Jan 2018 08:59:26 GMT

Connection: keep-alive

ETag: "5a6304ee-264"

Accept-Ranges: bytes

由于版本和Last_modified都不一样,所以可以用来作为标记。

把master上的keepalived服务关掉模拟master宕机

systemctl stop keepalived

然后访问VIP

curl -I 192.168.153.100

HTTP/1.1 200 OK

Server: nginx/1.12.2

Date: Sat, 20 Jan 2018 10:16:52 GMT

Content-Type: text/html

Content-Length: 3700

Last-Modified: Wed, 18 Oct 2017 08:08:18 GMT

Connection: keep-alive

ETag: "59e70bf2-e74"

Accept-Ranges: bytes

可以看出来VIP已经到了backup上

再开启master的keepalived服务

systemctl start keepalived

可以看出VIP又回到master上。

curl -I 192.168.153.100

HTTP/1.1 200 OK

Server: nginx/1.6.3

Date: Sat, 20 Jan 2018 10:18:17 GMT

Content-Type: text/html

Content-Length: 612

Last-Modified: Sat, 20 Jan 2018 08:59:26 GMT

Connection: keep-alive

ETag: "5a6304ee-264"

Accept-Ranges: bytes

 

总结:重要的事情说三遍!一定要关闭防火墙!一定要关闭防火墙!一定要关闭防火墙!

配完以后启动keepalived服务怎么也不会启动nginx脚本,curl VIP也都是各自机器,一度怀疑是keepalived配置文件出错。最后才发现是防火墙忘记关。对selinux和firewalld 简直深恶痛绝。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值