Nginx配置高可用的集群

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

  1)需要两台服务器  192.168.17.130 和 192.168.17.131

  2)在两台服务器中都安装Nginx

  3)在两台服务器中都安装keepalived

2、在两台服务器安装keepalived

  1)使用yum命令进行安装

    yum install keepalived -y

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

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

  1)keepalived.conf文件详解:

    启动keepalived的方式: systemctl start keepalived.service

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.130

  smtp_connect_timeout 30

  router_id LVS_DEVELBACK    # vi /etc/hosts   中添加(本地的话)127.0.0.1 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    # 网卡  --  使用ipconfig  从中取网卡

  virtual_router_id 51    # 主、备机的 virtual_router_id 必须相同

  priority 100    # 主、备机取不同的优先级,主机值较大,备份机值较小

  advert_int 1    # 每隔多久发送一次心跳,检测主机是否还活着,单位是秒

  authentication {

    auth_type PASS

    auth_pass 1111

  }

  virtual_ipaddress {

    192.168.17.50    #VRRP   请求的虚拟地址

  }

}

  

  2)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

原文地址:https://www.cnblogs.com/taosheng-yijiu/p/13729989.html