Keepalived + Nginx实现高可用 Web负载均衡

Keepalived+Nginx实现

Keepalived简要介绍

Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived可以用来防止服务器单点故

障的发生,通过配合 Nginx可以实现 web前端服务的高可用。

Keepalived  VRRP 协议为实现基础,用 VRRP 协议来实现高可用性(HA)VRRP(Virtual Router

Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个

设备,对外提供虚拟路由器 IP(一个或多个),而在路由器组内部,如果实际拥有这个对外 IP 的路由器如

果工作正常的话就是 MASTER,或者是通过算法选举产生,MASTER实现针对虚拟路由器 IP的各种网络功能,

 ARP请求,ICMP,以及数据的转发等;其他设备不拥有该虚拟 IP,状态是 BACKUP,除了接收 MASTER

VRRP状态通告信息外,不执行对外的网络功能。当主机失效时,BACKUP将接管原先 MASTER的网络功能。

VRRP协议使用多播数据来传输 VRRP数据,VRRP数据使用特殊的虚拟源 MAC地址发送数据而不是自身

网卡的 MAC地址,VRRP 运行时只有 MASTER 路由器定时发送 VRRP 通告信息,表示 MASTER 工作正常以及虚

拟路由器 IP()BACKUP 只接收 VRRP 数据,不发送数据,如果一定时间内没有接收到 MASTER 的通告信

息,各 BACKUP将宣告自己成为 MASTER,发送通告信息,重新进行 MASTER选举状态。


开始搭建:

节点: 安装

Cluster1 192.168.152.11     Keepalived  Nginx          CentOs6.8
Cluster1 192.168.152.12 Keepalived  Nginx          CentOs6.8

 下载 Keepalived v1.2.18 和Nginx 1.10.2

点击这里下载Keepalived   点击这里下载Nginx

首先 安装需要的环境

yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel
openssl openssl-devel
执行 下载 编译 Nginx 所需的依赖包

上传到Linux Nginxtar包 

tar -zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2
./configure --prefix=/soft/install/nginx
make && make install
执行上面代码 进行编译


成功以后 开始配置Nginx

vim /soft/install/nginx/conf/nginx.conf
user  root;

将用户改为 root

Keepalived + Nginx实现高可用 Web负载均衡_第1张图片


修改html下的index.html页面 

Keepalived + Nginx实现高可用 Web负载均衡_第2张图片

Cluster2 机器同理

启动 Nginx
/usr/local/nginx/sbin/nginx
重启 Nginx
/usr/local/nginx/sbin/nginx -s reload

访问

Keepalived + Nginx实现高可用 Web负载均衡_第3张图片

Nginx安装成功


安装Keepalived

上传并编译

tar -zxvf keepalived-1.2.18.tar.gz
cd keepalived-1.2.18
./configure --prefix=/soft/install/keepalived
make && make install

 keepalived安装成 Linux系统服务:

因为没有使用 keepalived的默认路径安装(默认是/usr/local,安装完成之后,需要做一些工作

复制默认配置文件到默认路径

mkdir /etc/keepalived
cp /soft/install/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

复制 keepalived服务脚本到默认的地址

cp /soft/install/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /soft/install/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /soft/install/sbin/keepalived /usr/sbin/
ln -s /soft/install/keepalived/sbin/keepalived /sbin/

设置 keepalived服务开机启动

chkconfig keepalived on


修改 Keepalived (Master)192.168.152.11配置文件

vi /etc/keepalived/keepalived.conf
内容为:

! Configuration File for keepalived

global_defs {
   router_id cluster1
}

vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh"
        interval 60
        weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.152.11
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.152.66
    }
}

vi /etc/keepalived/keepalived.conf

配置BACKUP (192.168.152.12)

! Configuration File for keepalived

global_defs {
   router_id cluster2
}

vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh"
        interval 2
        weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    mcast_src_ip 192.168.152.12
    priority 90
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.152.66
    }
}

编辑检查脚本

/etc/keepalived/nginx_check.sh

#!/bin/bash

cd /soft/data/
failcount=0
runlog=nginx_runlog.txt

for((i=1;i<=6;i++));
do
        result=`curl http://127.0.0.1/isok.html`

        time=`date +%F\ %k:%M:%S`
        error=false
        if [ "$result" != "ok" ] ; then
                echo "=================$time====================" >> $runlog
                                failcount=`expr $failcount + 1`
                                echo "nginx doesn't work!" >> $runlog
                echo " ================================" >> $runlog
        else
                break
        fi

        if test $failcount -gt 5 ; then
                echo "\n==================$time=====================\n\n" >> $runlog
                echo "restart nignx..." >> $runlog
                echo "     ================================" >> $runlog

                        #关闭
                        /soft/install/nginx/sbin/nginx -s stop

                        if [ -s /soft/install/nginx/logs/nginx.pid ] ; then
                                pid=`cat /soft/install/nginx/logs/nginx.pid`
                                kill -quit $pid
                        fi

                        #启动
                        /soft/install/nginx/sbin/nginx

                        sleep 10

                        result=`curl http://127.0.0.1/isok.html`

                        if [ "$result" != "ok" ] ; then
                                 killall keepalived
                        fi
        fi
done


添加isok.html

vim /soft/install/nginx/html/isok.html 
ok


给脚本权限

chmod +x /etc/keepalived/nginx_check.sh
启动 Keepalived
service keepalived start
Starting keepalived: [ OK ]

Keepalived服务管理命令:

停止:service keepalived stop

启动:service keepalived start

重启:service keepalived restart

查看状态:service keepalived status


Ok 配置已经结束了 随便检测

Keepalived + Nginx实现高可用 Web负载均衡_第4张图片

到此结束。。。Keepalived + Nginx双机热备就OK了

Keepalived + Nginx实现高可用 Web负载均衡_第5张图片


你可能感兴趣的:(服务器)