Keepalived如何实现Nginx高可用

 更新时间:2022年10月31日 10:10:07   作者:z.haoui  
这篇文章主要介绍了Keepalived如何实现Nginx高可用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Keepalived实现Nginx高可用

Keepalived安装可参考Mysql+Keepalived实现双主热备

Master上的keepalived.conf

global_defs {
    router_id LVS_LEVEL1    #主服务器名称
}
 
vrrp_script check_run {
   script "/usr/local/src/check_nginx.sh"
   interval 5                #5秒执行一次脚本
}
 
vrrp_instance VI_1 {
    state MASTER            #主服务器
    interface eth0            #承载VIP地址到物理接口
    virtual_router_id 51    #虚拟路由器ID号,每个热播组保持一致
    priority 100            #优先级,数值越大优先级越高
    advert_int 1            #检查间隔,默认为1s
    authentication {        #认证信息,每个热播组保持一致
        auth_type PASS      #认证类型
        auth_pass 1111        #密码字串
    }
    virtual_ipaddress {
        192.168.0.200        #VIP地址(内网地址)
    }
    track_script {
        check_run
    }
}

Backup上的keepalived.conf

global_defs {
    router_id LVS_LEVEL2    #备份服务器名称
}
vrrp_script check_run {
    script "/usr/local/src/check_nginx.sh"
    interval 5                #5秒执行一次脚本
}
vrrp_instance VI_1 {
    state BACKUP            #备份服务器
    interface eth0            #承载VIP地址到物理接口
    virtual_router_id 51    #虚拟路由器ID号,每个热播组保持一致
    priority 50                #优先级,数值越大优先级越高
    advert_int 1            #检查间隔,默认为1s
    authentication {        #认证信息,每个热播组保持一致
        auth_type PASS      #认证类型
        auth_pass 1111        #密码字串
    }
    virtual_ipaddress {
        192.168.0.200       #VIP地址(和主服务器设置一样)
    }
    track_script {
        check_run
    }
}

Nginx检测脚本check_nginx.sh

#!/bin/bash
 
A=`ps -C nginx --no-header |wc -l`
#判断nginx是否宕机,如果宕机,尝试重启
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx
    #等待一会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
    sleep 5
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
        killall keepalived
    fi
fi
chmod +x /etc/keepalived/nginx_check.sh

Keepalived+Nginx高可用集群

实验环境

准备2台设备

设备1 192.168.217.11 nginx +keepalived

设备2 192.168.217.12 nginx +keepalived

虚拟ip 192.168.217.3

设备1、2 安装nginx keepalived

(此处设备已安装nginx)

我们在此基础上直接利用yum安装keepalived

更新网络yum源

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2022-06-11 17:56:31--  http://mirrors.aliyun.com/repo/epel-7.repo

安装keepalived

[root@localhost ~]# yum -y install keepalived.x86_64 

启动nginx

[root@localhost ~]# cd /usr/src/nginx-1.12.2/
[root@localhost nginx-1.12.2]# killall -9 nginx
[root@localhost nginx-1.12.2]# nginx

修改keepalived配置文件

[root@localhost nginx-1.12.2]# vim /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {   
    state BACKUP            #主调度器的初始角色(本实验主备MASTER 从BACKUP)
    interface ens33            #修改网卡名称
    virtual_router_id 52      #主id 与从id  不要重复     
    priority 90                 #主调度器的选举优先级   (本实验  主备100  从90  数据越大 优先级越高)
    advert_int 1            
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.3                      #虚拟ip   (本实验需设置  同网段  主从一样)
    }
}

查看ip

[root@localhost ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:c2:15:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.11/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.217.3/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::ac8:77ad:9154:7983/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

重启keepalived

[root@localhost ~]# systemctl start keepalived.service
[root@localhost ~]# systemctl restart keepalived.service

关闭防火墙 内核

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

验证

[root@localhost ~]# curl 192.168.217.11
‘nginx1'
[root@localhost ~]# curl 192.168.217.12
‘nginx2'
[root@localhost ~]# curl 192.168.217.3
‘nginx1'
[root@localhost ~]# curl 192.168.217.3
‘nginx1'

实验环境 

准备2台设备 双主keepalived

设备1 192.168.217.11 nginx +keepalived

设备2 192.168.217.12 nginx +keepalived

虚拟ip 192.168.217.3

虚拟ip 192.168.217.6

在以上实验基础上

设备1

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {     #修改模块名字
    state MASTER     #主调度器的初始角色(本实验主备MASTER 从BACKUP)
    interface ens33      #修改网卡名称
    virtual_router_id 51       #主id 与从id  不要重复     
    priority 100           #主调度器的选举优先级   (本实验  主备100  从90  数据越大 优先级越高)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.3            #虚拟ip   
    }
}


vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 53
    priority 90	
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.6              
    }
I}

设备2

[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.3
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 53
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.217.6
    }
}

xshell同时开启 命令模式 重启keepalived

[root@localhost ~]# systemctl start keepalived.service
[root@localhost ~]# systemctl restart keepalived.service

查看ip

设备1 飘逸Ip正常

[root@localhost ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:c2:15:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.11/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.217.3/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::ac8:77ad:9154:7983/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

设备2

[root@localhost ~]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:49:b3:a1 brd ff:ff:ff:ff:ff:ff
    inet 192.168.217.12/24 brd 192.168.217.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.217.6/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::1e6f:d3ee:5554:1f34/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

设备1、设备2验证 (此问题暂未解决)

[root@localhost ~]# curl 192.168.217.11
curl: (7) Failed connect to 192.168.217.11:80; 拒绝连接
[root@localhost ~]# curl 192.168.217.12
‘nginx2'
[root@localhost ~]# curl 192.168.217.3
curl: (7) Failed connect to 192.168.217.3:80; 连接超时
[root@localhost ~]# curl 192.168.217.6
curl: (7) Failed connect to 192.168.217.6:80; 连接超时

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Nginx Session共享问题解决方案解析

    Nginx Session共享问题解决方案解析

    这篇文章主要介绍了Nginx Session共享问题解决方案解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Centos7.3 安装部署Nginx并配置https的方法步骤

    Centos7.3 安装部署Nginx并配置https的方法步骤

    这篇文章主要介绍了Centos7.3 安装部署Nginx并配置https的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • shell脚本实战之部署nginx脚本实例

    shell脚本实战之部署nginx脚本实例

    最近自己编写的Linux一键部署脚本,可以一键部署Nginx,分享给大家,这篇文章主要给大家介绍了关于shell脚本实战之部署nginx脚本的相关资料,需要的朋友可以参考下
    2022-12-12
  • nginx如何通过proxy_pass设置反向代理,隐藏端口号

    nginx如何通过proxy_pass设置反向代理,隐藏端口号

    这篇文章主要介绍了nginx如何通过proxy_pass设置反向代理,隐藏端口号方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • nginx 基本入门教程

    nginx 基本入门教程

    本篇文章主要介绍了详解nginx 基本入门,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 详解Nginx之Location配置(Location匹配顺序)

    详解Nginx之Location配置(Location匹配顺序)

    这篇文章主要介绍了详解Nginx之Location配置(Location匹配顺序),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Nginx 转发匹配规则的实现

    Nginx 转发匹配规则的实现

    这篇文章主要介绍了Nginx 转发匹配规则的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Nginx层面配置基础用户验证的完整步骤

    Nginx层面配置基础用户验证的完整步骤

    这篇文章主要给大家介绍了关于Nginx层面配置基础用户验证的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07
  • Nginx记录分析响应慢的请求及替换网站响应内容的配置

    Nginx记录分析响应慢的请求及替换网站响应内容的配置

    这篇文章主要介绍了Nginx记录分析响应慢的请求及替换网站响应内容的配置,分别用到了ngx_http_log_request_speed模块和ngx_http_sub_module模块,需要的朋友可以参考下
    2016-01-01
  • 部署Nginx+Apache动静分离的实例详解

    部署Nginx+Apache动静分离的实例详解

    Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能,这篇文章主要介绍了部署Nginx+Apache动静分离的实例代码,需要的朋友可以参考下
    2019-12-12

最新评论