Nginx+Keepalived实现高可用部署

本篇主要介绍通过Keepalived实现Nginx高可用部署,本篇所介绍的内容依赖前面几篇搭建的Nginx反向代理负载均衡环境。如果你还没有这个环境,可以先看前面几篇博文:

VMware Workstation安装Redhat6.4全过程附图(一)

VMware Workstation安装Redhat6.4全过程附图(二)

Linux环境JDK1.8安装

Linux环境Nginx安装

Linux环境Nginx反向代理实现负载均衡

如果已经有linux虚拟机,可以跳过第一,第二篇。

 

下面开始Keepalived高可用搭建

1、Keepalived下载

下载地址:https://www.keepalived.org/software/keepalived-2.0.20.tar.gz

2、Keepalived安装

进入keepalived下载目录/usr/local/soft,执行解压命令

[root@test1 soft]# tar -zxvf keepalived-2.0.20.tar.gz

进入/usr/local/soft/keepalived-2.0.20目录,依次执行下面3条命令进行安装

[root@test1 keepalived-2.0.20]# ./configure --prefix=/usr/local/keepalived
[root@test1 keepalived-2.0.20]# make
[root@test1 keepalived-2.0.20]# make install

 

3、Keepalived配置及启动

安装完成后,一般会将Keepalived注册为系统服务,设置为开机启动,防止服务挂掉。

进入安装目录的etc目录下,将keepalived相应的配置文件拷贝到系统相应的目录当中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件,如果没有找到则使用默认的配置。/etc/keepalived目录安装时默认是没有创建的,需要手动创建。

[root@test1 keepalived-2.0.20]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@test1 keepalived-2.0.20]# cp /usr/local/soft/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
[root@test1 keepalived-2.0.20]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
[root@test1 keepalived-2.0.20]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

增加nginx状态检测脚本:

[root@test1  ~]# cd /usr/local/keepalived/
[root@test1 keepalived]# touch check_nginx.sh
[root@test1 keepalived]# chmod 755 check_nginx.sh
[root@test1 keepalived]# vim check_nginx.sh

check_nginx.sh文件内容如下:

#!/bin/bash
COUNT=$(ps -C nginx --no-header |wc -l)
echo $COUNT
#判断Nginx 是否都挂掉了
if [ $COUNT -eq 0 ]
then
    #nginx安装地址,如果挂掉了,就启动nginx(nginx命令的地址要写对)
    /usr/local/nginx/sbin/nginx
    echo "重启nginx"
    #等5秒钟后,再次查看是否 启动成功
    sleep 5
    #如果nginx没有启动起来,就直接干掉keepalived
    COUNT=$(ps -C nginx --no-header |wc -l)
    if [ $COUNT -eq 0 ]
    then
            echo "干掉keepalived",使得从服务器能够接管服务。
            #如果killall命令不能使用,就需要安装psmisc工具了
            #yum install -y psmisc
            killall keepalived
    fi
fi

主节点配置:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    #唯一标识,一般为用户名
    router_id username1
}
#监控服务.NGINX mysql等
vrrp_script chk_nginx {
    script "/usr/local/keepalived/check_nginx.sh"
    #每2s检查一次
    interval 2
    #每次检查-20
    weight -20
}
vrrp_instance VI_1 {
    ##主从设置 MASTER/BACKUP  
    state MASTER
    #网卡名称
    interface eth0
    #同一个集群下这个 router_id是一样的
    virtual_router_id 51
    #本机的ip,需要修改
    mcast_src_ip 172.16.43.103
    #优先级,从节点 配置,需要小于主节点
    priority 100
    #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
    advert_int 1
    #认证的密码
    authentication {
        auth_type PASS
        #设定授权密码,密码相同的为一个集群
        auth_pass 1111
    }
    #触发的脚本
    track_script {
          chk_nginx  #检测脚本,上面配置的
    }
    #虚拟ip地址(同一个集群中的虚拟ip必须得相同,可配置多个)
    virtual_ipaddress {
        172.16.43.188
        172.16.43.199
    }
}

从节点配置:

vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived
global_defs {
    #唯一标识,一般为用户名
    router_id username2
}
#监控服务.NGINX mysql等
vrrp_script chk_nginx {
    script "/usr/local/keepalived/check_nginx.sh"
    #每2s检查一次
    interval 2
    #每次检查-20
    weight -20
}
vrrp_instance VI_1 {
    ##主从设置 MASTER/BACKUP  
    state BACKUP
    #网卡名称
    interface eth0
    #同一个集群下这个 router_id是一样的
    virtual_router_id 51
    #本机的ip,需要修改
    mcast_src_ip 172.16.43.104
    #优先级,从节点 配置,需要小于主节点
    priority 80
    #设定MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒
    advert_int 1
    #认证的密码
    authentication {
       auth_type PASS
       #设定授权密码,密码相同的为一个集群
       auth_pass 1111
    }
    #触发的脚本
    track_script {
       chk_nginx  #检测脚本,上面配置的
    }
    #虚拟ip地址
    virtual_ipaddress {
        172.16.43.188
        172.16.43.199
    }
}

设置keepalived服务开机启动

chmod 755 /etc/init.d/keepalived #权限
chkconfig keepalived on #开机启动
chkconfig --add keepalived  #加为系统服务
service keepalived start  #启动服务
service keepalived status  #查看服务状态

启动后查看keepalived启动状态:

 

 想要停止keepalived服务,执行service keepalived stop。

在103(真实IP)上可以通过ip a命令查看虚拟IP是否正常

 

在104(真实IP)上查看ip a

 

 

上一篇通过Nginx的真实IP访问测试服务:http://172.16.43.103/test/test.html,http://172.16.43.104/test/test.html

现在我们通过虚拟IP同样可以访问测试服务:

http://172.16.43.188/test/test.html,结果如下:

http://172.16.43.199/test/test.html,结果如下:

4、测试Nginx高可用

停掉103上的keepalived服务

[root@test1 sbin]# service keepalived stop
停止 keepalived: [确定]
[root@test1 sbin]# service keepalived status
keepalived 已停

刷新http://172.16.43.199/test/test.html,发现仍然可以访问。

在103,和104上输入ip a,查看虚拟IP状态,可以看到虚拟IP188,199已经飘到104服务器上了。

 

 

 

 为了证明高可用生效,刷新test.html页面的时候,大家可以把nginx日志打开,看得更清楚一些。

查看日志命令:

[root@test2 logs]# tail -f /usr/local/nginx/logs/access.log 

 

再将103上的keepalived服务启动,请求又回到103上了。

 

到此,nginx高可用演示完成。

posted @ 2021-03-24 17:58  shileishmily  阅读(131)  评论(0编辑  收藏  举报