Keepalived

echo net.ipv4.ip_nonlocal_bind=1 >> /etc/sysctl.conf
sysctl -w net.ipv4.ip_nonlocal_bind=1
echo "1 secondgw" >> /etc/iproute2/rt_tables
/etc/keepalived/keepalived.conf
vrrp_script check_asterisk {
    script "killall -0 asterisk"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    interface eth0

    state MASTER
    priority 101

    ## second node
    # state BACKUP
    # priority 100

    virtual_router_id 55
    advert_int 1

    virtual_ipaddress {
        192.168.0.10 label eth0:vip
    }
    virtual_ipaddress {
        192.168.0.55/24 label eth0:vip
	10.10.10.55/24 dev eth1
    }
    virtual_routes {
        10.10.10.0/24 dev eth1 src 10.10.10.55 table secondgw
        default via 10.10.10.1 dev eth1 table secondgw
    }
    virtual_rules {
        from 10.10.10.55/32 table secondgw
        to 10.10.10.55/32 table secondgw
    }

    track_script {
        check_asterisk
    }

    authentication {
        auth_type PASS
        auth_pass [email protected]
    }
    notify "/etc/keepalived/notify.sh"
    notify_stop "/etc/keepalived/notify.sh 1 1 FAULT"
}
/etc/keepalived/notify.sh
#!/bin/bash

ENDSTATE=$3
NAME=$2
TYPE=$1
host=`hostname`
current_date=`/bin/date +"%b %d %H:%M:%S"`

case $ENDSTATE in
    "MASTER")
        logger "$current_date Keepalived: $host is now Master server with VIP"
        #asterisk -rx "reload"
        docker exec -it asterisk asterisk -rx sip reload
        exit 0
    ;;
    "BACKUP")
        logger "$current_date Keepalived: $host is now Backup server"
        exit 0
    ;;
    "FAULT")
        logger "$current_date Keepalived: $host server fault."
        exit 0
    ;;
    *)
        logger "$current_date Keepalived: $host Unknown state ${ENDSTATE} for VRRP ${TYPE} ${NAME}"
        exit 1
    ;;
esac