当前位置: 代码网 > it编程>数据库>Mysql > Nginx搭建高可用的实现

Nginx搭建高可用的实现

2024年08月31日 Mysql 我要评论
1、高可用概念一台nginx宕机了,还可以切换到另一台nginx上继续工作,让用户继续访问后台服务器2、准备工作(1)需要两台 nginx 服务器(2)需要keepalived(3)需要虚拟 ip3、

1、高可用概念

一台nginx宕机了,还可以切换到另一台nginx上继续工作,让用户继续访问后台服务器

2、准备工作

  • (1)需要两台 nginx 服务器
  • (2)需要 keepalived
  • (3)需要虚拟 ip

 3、高可用准备工作

(1)需要两台服务器 192.168.17.129 和 192.168.17.131

(2)在两台服务器安装 nginx

​ 这里如果不想再去安装一遍 nginx ,可以直接克隆 centos 。

(3)在两台服务器安装 keepalived

使用命令安装:yum install keepalived –y

安装之后,在 /etc 里面生成目录 keepalived,有文件 keepalived.conf

4、完成高可用配置(主从配置) 

(1)主机 nginx 修改/etc/keepalived/keepalivec.conf 配置文件

! configuration file for keepalived

global_defs {

    notification_email {

        acassen@firewall.loc

        failover@firewall.loc

        sysadmin@firewall.loc
     }

        notification_email_from alexandre.cassen@firewall.loc

        smtp_server 192.168.17.129

        smtp_connect_timeout 30

        router_id lvs_devel # 主机名字
}


vrrp_script chk_http_port {
        script "/usr/local/src/nginx_check.sh"
        interval 2 #(检测脚本执行的间隔)
        weight 2 # 权重
   }
        vrrp_instance vi_1 {
        state master # 备份服务器上将 master 改为 backup
        interface ens33 # 网卡
        virtual_router_id 51  # 主、备机的 virtual_router_id 必须相同
        priority 100  # 主、备机取不同的优先级,主机值较大,备份机值较小
        advert_int 1
        authentication {
                auth_type pass
                auth_pass 1111
        }

        virtual_ipaddress {
             192.168.77.50 # vrrp h 虚拟地址
        }

}

(2)主机 /usr/local/src添加检测脚本:nginx_check.sh

#!/bin/bash
a=`ps -c nginx –no-header | wc -l`
if [ $a -eq 0 ];then
        /usr/local/nginx/sbin/nginx
        sleep 2
        if [ `ps -c nginx --no-header |wc -l` -eq 0 ];then
            killall keepalived
        fi
fi

(3) 从机 nginx修改/etc/keepalived/keepalivec.conf 配置文件

! configuration file for keepalived

global_defs {

    notification_email {

        acassen@firewall.loc

        failover@firewall.loc

        sysadmin@firewall.loc 

     }

        notification_email_from alexandre.cassen@firewall.loc

        smtp_server 192.168.17.129

        smtp_connect_timeout 30

        router_id lvs_devel 
}


vrrp_script chk_http_port {
        script "/usr/local/src/nginx_check.sh"
        interval 2
        weight 2
   }
        vrrp_instance vi_1 {
        state backup # 修改为从机 backup
        interface ens33 # 修改为从机 ip
        virtual_router_id 51
        priority 90 # 优先级比主机低
        advert_int 1
        authentication {
            auth_type pass
            auth_pass 1111
        }

        virtual_ipaddress {
            192.168.77.50
        }

}

 (4)从机 /usr/local/src添加检测脚本:nginx_check.sh

#!/bin/bash
a=`ps -c nginx –no-header | wc -l`
if [ $a -eq 0 ];then
        /usr/local/nginx/sbin/nginx
        sleep 2
        if [ `ps -c nginx --no-header |wc -l` -eq 0 ];then
            killall keepalived
        fi
fi

(5)、启动 nginx

分别启动主从 nginx ,切换到/usr/local/nginx/sbin/,执行./nginx

(6)、主从都启动 keepalived

centos6启动命令

service keepalived start

centos7启动命令

systemctl start keepalived.service

(7)、主从都测试 keepalived 是否启动成功 

ps -ef | grep keepalived

5、最终测试 

此时便可以通过虚拟 ip 地址访问到 nginx 。

 6、将从机nginx和keepalived停掉,继续访问虚拟ip,依旧可以方位到nginx,高可用至此完成。

到此这篇关于nginx搭建高可用的实现的文章就介绍到这了,更多相关nginx搭建高可用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com