当前位置: > Linux服务器 > nginx >

nginx高可用集群的实现过程

时间:2019-12-17 14:32来源:linux.it.net.cn 作者:IT
这篇文章主要介绍了nginx高可用集群的实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、配置:

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

2、配置高可用的准备工作

(1)需要两台服务器192.168.180.113和192.168.180.112 (2)在两台服务器安装nginx (3)在两台服务器安装keepalived

3、在两台服务器安装keepalived

(1)使用yum命令进行安装

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



[root@it.net.cn dev]# yum install keepalived -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: ftp.riken.jp
* extras: mirrors.cn99.com
* updates: mirror.lzu.edu.cn
gitlab_gitlab-ce/x86_64/signature                                         | 836 B 00:00:00
gitlab_gitlab-ce/x86_64/signature                                         | 1.0 kB 00:00:00 !!!
gitlab_gitlab-ce-source/signature                                         | 836 B 00:00:00
gitlab_gitlab-ce-source/signature                                         | 951 B 00:00:00 !!!
正在解决依赖关系
--> 正在检查事务
---> 软件包 keepalived.x86_64.0.1.3.5-16.el7 将被 安装
--> 正在处理依赖关系 libnetsnmpmibs.so.31()(64bit),它被软件包 keepalived-1.3.5-16.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmpagent.so.31()(64bit),它被软件包 keepalived-1.3.5-16.el7.x86_64 需要
--> 正在检查事务
---> 软件包 net-snmp-agent-libs.x86_64.1.5.7.2-43.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

==========================================================================================================================================
Package                 架构            版本                源             大小
==========================================================================================================================================
正在安装:
keepalived                x86_64           1.3.5-16.el7            base           331 k
为依赖而安装:
net-snmp-agent-libs           x86_64           1:5.7.2-43.el7           base           706 k

事务概要
==========================================================================================================================================
安装 1 软件包 (+1 依赖软件包)

总下载量:1.0 M
安装大小:3.0 M
Downloading packages:
(1/2): net-snmp-agent-libs-5.7.2-43.el7.x86_64.rpm                                 | 706 kB 00:00:00
(2/2): keepalived-1.3.5-16.el7.x86_64.rpm                                     | 331 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------------
总计                                                   1.8 MB/s | 1.0 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装  : 1:net-snmp-agent-libs-5.7.2-43.el7.x86_64                                       1/2
正在安装  : keepalived-1.3.5-16.el7.x86_64                                            2/2
验证中   : keepalived-1.3.5-16.el7.x86_64                                            1/2
验证中   : 1:net-snmp-agent-libs-5.7.2-43.el7.x86_64                                       2/2

已安装:
keepalived.x86_64 0:1.3.5-16.el7

作为依赖被安装:
net-snmp-agent-libs.x86_64 1:5.7.2-43.el7

完毕!



4 、修改配置文件

2台都要换,一个主机,一个从机



[root@it.net.cn keepalived]# cat keepalived.conf
global_defs {
  notification_email {
  acassen@firewall.loc
  failover@firewall.loc
  sysadmin@firewall.loc
  }
  notification_email_from Alexandre.Cassen@firewall.loc
  smtp_server 192.168.180.113
  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 # 备份服务器上将 MASTER 改为 BACKUP
   interface ens33 //网卡
   virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
   priority 40 # 主、备机取不同的优先级,主机值较大,备份机值较小
   advert_int 1
  authentication {
   auth_type PASS
   auth_pass 1111
  }
  virtual_ipaddress {
  192.168.180.114 // VRRP H 虚拟地址
  }
}


脚本:


[root@it.net.cn src]# cat nginx_check.sh
#!/bin/bash
A=`ps -C nginx ▒Cno-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[root@it.net.cn src]#
把两台服务器上nginx和keepalived启动

启动nginx:./nginx 由于我112上安装了gitlab自带nginx,就不要启动了 启动keepalived:systemctl start keepalived.service

5、测试



把113的nginx和keepalived关掉,发现会切换到112上

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容