keepalived 2.0.20 主从配置

发布时间 2023-11-16 16:45:58作者: 海的味道

1、master配置

! Configuration File for keepalived
vrrp_script check_nginx {
   script "/home/spots/keepalived/check_nginx.sh"
   interval 2
   weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens192
    virtual_router_id 60
    priority 100
    advert_int 1
    unicast_src_ip 10.73.56.238
    unicast_peer {
      # 其他机器ip
      10.73.56.239
    }
    # 设置nopreempt防止抢占资源
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.73.56.251/24
    }
    track_script {
        check_nginx
    }
}

 

2、backup配置

! Configuration File for keepalived
vrrp_script check_nginx {
   script "/home/spots/keepalived/check_nginx.sh"
   interval 2
   weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens192
    virtual_router_id 60
    priority 90
    advert_int 1
    unicast_src_ip 10.73.56.239
    unicast_peer {
      # 其他机器ip
      10.73.56.238
    }
    # 设置nopreempt防止抢占资源
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       10.73.56.251/24
    }
    track_script {
        check_nginx
    }
}

  

3、检测脚本,使用docker-compose编排的web服务,nginx作为基础镜像源

#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]
then
 docker-compose -f /home/spots/docker-compose.yml restart spot-web-fujian
 sleep 1
 A2=`ps -C nginx --no-header |wc -l`
 if [ $A2 -eq 0 ]
 then
  systemctl stop keepalived
 fi
fi