1、两台服务器主备
10.199.12.31 主
10.199.12.36 备
注:主服务器安装
yum install -y keepalived
2、keepalived 主服务器配置 /etc/keepalived/keepalived.conf
global_defs {
router_id prod-master
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" # 此配置为检测进程脚本
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance prod-nginx-master01 {
state MASTER
priority 110
interface ens192
virtual_router_id 151
advert_int 1
authentication {
auth_type PASS
auth_pass 1qaz@WSX
}
track_script {
check_nginx
}
virtual_ipaddress {
10.199.11.20/24
}
}
3、keepalived 从服务器配置 /etc/keepalived/keepalived.conf
global_defs {
router_id prod-backup
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" # 此配置为检测进程脚本
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance prod-nginx-master01 {
state BACKUP
priority 90
interface ens192
virtual_router_id 151
advert_int 1
authentication {
auth_type PASS
auth_pass 1qaz@WSX
}
track_script {
check_nginx
}
virtual_ipaddress {
10.199.11.20/24
}
}
4、nginx进程检测脚本 /etc/keepalived/check_nginx.sh
#!/bin/bash
cur_day=`date +%Y-%m-%d`
cur_time=`date +%Y-%m-%d-%H:%M:%S`
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
echo "[${cur_time}] [nginx process is not found and starting nginx process]" >> /etc/keepalived/logs/${cur_day}.log
/data/nginx/sbin/nginx
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
echo "[${cur_time}] nginx process boot failed and stop keepalived" >> /etc/keepalived/logs/${cur_day}.log
systemctl stop keepalived
fi
fi
5、keepalived服务启动
systemctl start keepalived
systemctl enable keepalived
6、nginx部署
版本:nginx-1.16.1.tar.gz
部署路径:/data/nginx
6.1、编译安装
注意:在安装过程中,如果报错,根据实际情况安装一些依赖
[root@web-1 nginx-1.16.1]#yum install -y openssl-devel gcc-c++
[root@web-1 nginx-1.16.1]#./configure --add-module=/data/ngx_cache_purge-2.3 --prefix=/data/nginx --with-http_stub_status_module --with-pcre
[root@web-1 nginx-1.16.1]#make && make install
6.2、nginx注册为系统服务
[root@web-1 nginx-1.16.1]# vi /etc/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/data/nginx/sbin/nginx
ExecReload=/data/nginx/sbin/nginx -s reload
ExecStop=/data/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6.3、服务启动
systemctl enable nginx
systemctl start nginx