1.修改主机名,关闭selinux和iptables

sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
setenforce 0
主从
主:

hostnamectl set-hostname nginx-master

hostnamectl set-hostname nginx-slave

2.安装keepalived并配置

网卡地址和ip地址,虚拟ip
主机
-

yum install -y keepalived
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf_bak
vi /etc/keepalived/keepalived.conf

global_defs {
    router_id nginx-master  #hostname
}
vrrp_script chk_nginx {
    script "/etc/keepalived/scripts/nginx_check.sh"
    interval 2
    timeout 2
    fall 3
}
vrrp_instance nginx {
    state master 
    interface ens7 //这个地址要用网卡的真实名字
    virtual_router_id 167 //这个id不应该和其他的router_id冲突
    priority 100
    advert_int 1
    authentication { #all node must same
    auth_type PASS
    auth_pass 1111
   }
virtual_ipaddress {
    21.0.7.32 //定义虚拟IP
}
track_script {
    chk_nginx
}
}

nginx_check.sh

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
    /usr/local/nginx/sbin/nginx                #重启nginx
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败
        exit 1
    else
        exit 0
    fi
else
    exit 0
fi
最后修改:2025 年 05 月 19 日
如果觉得我的文章对你有用,请随意赞赏