Nginx-主备集群搭建

搭建高可用的Nginx服务集群

环境准备

1.两台Nginx服务器(192.168.10.231,192.168.10.232)
2.安装KeepAlived软件
3.需要一个虚拟IP

首先在一台虚拟机上安装Nginx

1.安装相关依赖

yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

2.下载Nginx安装文件,并解压(这里将文件下载到opt目录下)

cd  /opt
# 安装下载工具
yum install -y wget
# 下载
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 解压
tar -xvf nginx-1.18.0.tar.gz

3.安装

# 进入到解压文件中
cd nginx-1.19.1
# 执行命令
./configure
# 执行命令
make && make install

4.启动Nginx

# nginx默认安装到了/usr/local/目录下
cd /usr/local/nginx/sbin
# 启动Nginx
./nginx

5.访问Nginx


Nginx-主备集群搭建_第1张图片
image.png

在服务器上安装keepalived

yum install -y keepalived

查看是否安装成功

rpm -a -q keepalived

安装位置:/etc/keepalived

在此虚拟机的基础上克隆一台新机器,修改IP为:192.168.10.232

进行keepalived配置

1.首先在192.168.10.231上配置
配置主机名

vi /etc/hosts
127.0.0.1 LVS_DEVEL
cd /etc/keepalived
vim keepalived.conf

将以下内容替换到keepalived.conf中

! Configuration File for keepalived
# 全局定义的参数
global_defs {
    notification_email {
        [email protected]
        [email protected]
        [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.10.231
    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 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.230 // VRRP H 虚拟地址
    }
}

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
  1. 在192.168.10.231上配置
    只需要更改keepalived.conf中的部分值即可
! Configuration File for keepalived

# 全局定义的参数
global_defs {
    notification_email {
        [email protected]
        [email protected]
        [email protected]
    }
    notification_email_from [email protected]
    smtp_server 192.168.10.232
    smtp_connect_timeout 30
    router_id LVS_DEVEL # host的名称
}
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 80 # 主、备机取不同的优先级,主机值较大,备份机值较小
    advert_int 1  # 心跳的间隔时间
    authentication { # 权限验证
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.10.230 // VRRP H 虚拟地址
    }
}

测试

分别在两台机器上启动Nginx和KeepAlived

# nginx默认安装到了/usr/local/目录下
cd /usr/local/nginx/sbin
# 启动Nginx
./nginx
# 启动keepalived
systemctl start keepalived
# 查看启动是否成功
ps -ef | grep keepalived

访问:http://192.168.10.230/


Nginx-主备集群搭建_第2张图片
image.png

说明启动成功
停掉192.168.10.231上的nginx和keepalived

# 停止keepalived
systemctl stop keepalived
# 停止nginx
cd /usr/local/nginx/sbin
./nginx -s stop

再次访问http://192.168.10.230/,发现仍然能够访问到nginx,说明从机生效
通过ip a命令查看网卡信息

image.png

可以看到192.168.10.230和192.168.10.232绑定了

你可能感兴趣的:(Nginx-主备集群搭建)