一、环境准备
操作系统:CentOS Linux release 7.5.1804 (Core)
nginx版本:nginx-1.18.0.tar.gz
keepalived版本:keepalived-2.2.0.tar.gz
IP分配:
master:nginx+keepalived 10.10.10.11
backup:nginx+keepalived 10.10.10.12
vip:10.10.10.10
二、nginx安装
主备都安装nginx
软件放置目录:/soft ( 可通过 rz 命令或者 xftp 上传到soft目录)
nginx安装目录:/application/nginx
1.安装依赖:yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel
2.解压:tar -zxvf nginx-1.18.0.tar.gz
3.(可选拓展)追加第三方模块:sticky模块、check模块、vts监控模块
模块的用法就自己百度,下面说怎么追加到nginx
下载解压(我移动到/application/nginx/module下并重命名为nginx-sticky、nginx-check、nginx-vts)
其中:
check模块需要手动打补丁,步骤如下:
yum install -y patch
cd /soft/nginx-1.18.0/
patch -p0</application/nginx/module/nginx-check/check_1.11.5+.patch
#check版本对nginx的版本有要求,1.12以上版本的nginx补丁为check_1.11.5+.patch。-p0:当前路径,-p1:上一级路径
vts监控模块需要修改 nginx-sticky/ngx_http_sticky_misc.c文件(不修改在编译时会报错)
vim /application/nginx/module/nginx-sticky/ngx_http_sticky_misc.c
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <ngx_sha1.h>
#include "ngx_http_sticky_misc.h"
#追加以下这两句
#include <openssl/sha.h>
#include <openssl/md5.h>
..........
..........
4.编译安装nginx:
./configure --prefix=/application/nginx --with-http_gzip_static_module --with-http_stub_status_module --add-module=/application/nginx/module/nginx-sticky --add-module=/application/nginx/module/nginx-check --add-module=/application/nginx/module/nginx-vts
make
make install
中途不报错的话,执行/application/nginx/sbin/nginx
然后在网页访问会出现nginx的欢迎界面。
三、keepalived安装
主备都一样的安装
tar -zxvf keepalived-2.2.0.tar.gz
cd keepalived-2.2.0/
./configure
make
make install
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /etc/init.d/
ln -s /usr/local/sbin/keepalived /usr/sbin
systemctl enable keepalived.service
修改master(10.10.10.11)的keepalived.conf配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
router_id nginx_master # 设置nginx master的id,在一个网络应该是唯一的
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/etc/keepalived/scripts/check_nginx.sh"
interval 2 # 检测脚本执行的间隔,单位是秒
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
interface eth0 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
virtual_router_id 53 # 虚拟路由编号,主从要一直,除备机局域网内ID唯一
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port # 调用检测脚本
}
virtual_ipaddress {
10.10.10.10 # 定义虚拟ip(VIP),可多设,每行一个
}
}
修改backup(10.10.10.12)的keepalived.conf配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
router_id nginx_backup # 设置nginx master的id,在一个网络应该是唯一的
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/etc/keepalived/scripts/check_nginx.sh"
interval 2 # 检测脚本执行的间隔,单位是秒
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备
interface eth0 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
virtual_router_id 53 # 虚拟路由编号,主从要一直,除备机局域网内ID唯一
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port # 调用检测脚本
}
virtual_ipaddress {
10.10.10.10 # 定义虚拟ip(VIP),可多设,每行一个
}
}
检测脚本:
vim /etc/keepalived/scripts/check_nginx.sh
#!/bin/bash
A=$(ps -C nginx --no-header |wc -l)
if [ $A -eq 0 ];then
/application/nginx/sbin/nginx
sleep 2
if [ $A -eq 0 ];then
systemctl stop keepalived
exit 1
else
exit 0
fi
else
exit 0
fi
查看vip:ip a | grep eth0
启动nginx:/application/nginx/sbin/nginx
启动keepalive:systemctl start keepalived.service