keepalived 2.0.20 + 2.1.5 主从配置

一、2.0.20 版本配置

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

 二、2.1.5配置

   1、主配置

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx {
   script "/data/spots/check_nginx.sh"
   interval 2
   weight -20
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.168.2.137/24
    }
    track_script {
        check_nginx
    }
}

 2、备配置

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script check_nginx {
   script "/data/spots/check_nginx.sh"
   interval 2
   weight -20
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 50
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.168.2.137/24
    }
    track_script {
        check_nginx
    }
}

3、check_nginx.sh

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

  

posted @ 2023-11-16 16:39  漂渡  阅读(106)  评论(0编辑  收藏  举报