ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Nginx+Keepalived实现简单的服务高可用

2021-04-01 08:01:52  阅读:203  来源: 互联网

标签:loc nginx firewall Keepalived 可用 keepalived 192.168 Nginx vrrp


Nginx+Keepalived实现简单的服务高可用

 

一般情况下,如果我们做小型项目,前端用一个nginx做反向代理即可,大概是这样的

  image.png

 

但是,作为互联网项目,纯2C的话必然需要做高可用,不仅后端的Server有N个,Nginx同样需要有N个,一主N备,当有一个服务器挂掉的时候,服务能瞬间切换到其他服务器,大概是这样的

 

  image.png

下面就以上图为例,说明一下如何实现server的高可用。

1、准备

虚拟机两台,同样安装nginx,keepalived,最简单的安装方法yum -y install nginx,yum -y install keepalived。如果找不到安装到哪儿了,可以使用whereis nginx查看,这里不再赘述。
网络划分如下

名称IP虚拟IP操作系统
虚拟机1(VM1) 192.168.136.2 192.168.136.99 centos7.6
虚拟机2(VM2) 192.168.136.4 192.168.136.99 centos7.6

2、关闭防火墙,修改nginx首页,启动nginx

  • 关闭防火墙
systemctl stop firewalld.service #临时关闭,重启失效
systemctl disable firewalld.service  #禁止开机启动
  • 简单起见,我们认为每个nginx都是代理一个服务,只用nginx默认带的静态页作为测试,分别修改页面内容为"Welcome to 192.168.136.4"和“Welcome to 192.168.136.2”
  • 启动nginx
systemctl start nginx

3、修改keepalived的配置文件

主配置如下(默认配置文件:/etc/keepalived/keepalived.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.200.1
 #  smtp_connect_timeout 30
   router_id LVS_DEVEL
 #  vrrp_skip_check_adv_addr
 #  vrrp_strict
 #  vrrp_garp_interval 0
 #  vrrp_gna_interval 0
}
vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh"
        interval 2
        weight -20
}
vrrp_instance VI_1 {
    state MASTER # 标识为主服务
    interface ens33 #绑定虚拟机的IP
    virtual_router_id 51 # 虚拟路由id,和从机保持一致
    #mcast_src_ip 192.168.126.2  #本机ip
    priority 100 #权重,需要高于从机
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
   track_script {
                chk_nginx ## 执行 Nginx 监控的服务
        }
    virtual_ipaddress {
        192.168.136.99 #/32 brd 255.255.255.0 dev ens33 label ens33:vip #虚拟IP地址
#        192.168.200.17
#        192.168.200.18
    }
}

从机配置(默认配置文件:/etc/keepalived/keepalived.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.200.1
#   smtp_connect_timeout 30
   router_id dreamer1
#   vrrp_skip_check_adv_addr
#   vrrp_strict
#   vrrp_garp_interval 0
#   vrrp_gna_interval 0
}
vrrp_script chk_nginx {
        script "/etc/keepalived/nginx_check.sh" ## 检测 nginx 状态的脚本路径
        interval 2 ## 检测时间间隔
        weight -20 ## 如果条件成立,权重-20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    #mcast_src_ip 192.168.136.4 ## 本机 IP 地址 
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
        track_script {
                chk_nginx ## 执行 Nginx 监控的服务
        }
    virtual_ipaddress {
        192.168.136.99
        #192.168.200.17
        #192.168.200.18
    }
}

3、编写监测心跳脚本

上面配置中可以看到有一个脚本文件:/etc/keepalived/nginx_check.sh
查看nginx是否启动,如果没启动则启动,如果启动不起来,停掉keepalived服务,此时心跳断掉,服务转向另一个nginx。

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
    fi
fi

4、测试

  • 启动192.168.136.2上的nginx和keepalive
  • 启动192.168.136.4上的nginx和keepalive
  • 访问虚拟IP:http://192.168.136.99


      image.png
  • 停掉192.168.136.2上的keepalive


      image.png
  • 重新启动192.168.136.2上的keepalive,又会回到 Welcome to 192.168.136.2
  • 停掉192.168.136.2上的nginx,系统会自动调回Welcome to 192.168.136.4


      image.png

就是这样。

标签:loc,nginx,firewall,Keepalived,可用,keepalived,192.168,Nginx,vrrp
来源: https://www.cnblogs.com/xiexun/p/14604650.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有