1 下载需要的依赖

yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel

yum install -y keepalived

2 修改keepalived.conf 配置文件(etc/keepalived)

global_defs {

  # Keepalived process identifier

  router_id nginx

}

# Script to check whether Nginx is running or not

vrrp_script check_nginx {

  script "/etc/keepalived/check_nginx.sh"   ----脚本路径

  interval 2 --每几秒刷新

  weight -20

}

# Virtual interface - The priority specifies the order in which the assigned interface to take over in a failover

vrrp_instance VI_01 {

  state MASTER --主机标识,如果是从机就是BACKUP

  interface eth0

  virtual_router_id 151 --标识id,每个机器都不一样

  priority 110 --优先级,主机大,从机小

  # The virtual ip address shared between the two NGINX Web Server which will float

  virtual_ipaddress {

    172.16.16.103/24 --虚拟ip

  }

  track_script {

    check_nginx

  }

  authentication {

    auth_type AH

    auth_pass secret

  }

}

3 新建nginx监听脚本  check_nginx.sh

count=$(ps -ef |grep nginx |egrep -cv "grep|$$")

if [ "$count" -eq 0 ];then

    systemctl stop keepalived

fi

4 赋权 chmod 755 check_nginx.sh

5 启动systemctl start keepalived

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐