1. 程式人生 > >FreeSWITCH折騰筆記7——使用keepalived進行主備切換高可用部署

FreeSWITCH折騰筆記7——使用keepalived進行主備切換高可用部署

freeSWITCH的高可用部署方式有兩種:主備切換和負載均衡,官方文件介紹的主備切換部署是採用Corosync & Pacemaker,負載均衡採用前置opensips。但對使用keepalived進行主備切換的高可用方式沒有介紹,同時網上對該種部署方式也沒有介紹。

本人對Corosync & Pacemaker不熟悉,目前在職的公司web應用大部分採用keepalived+haproxy,所以對keepalived稍微熟悉一點,因此嘗試使用keepalived進行freeswitch進行主備切換的高可用部署。

使用keepalived進行freeswitch進行主備切換的高可用部署比較簡單,本文介紹的部署方案有兩個亮點(自以為):1、主節點不搶佔VIP,否則可能導致主節點恢復後VIP切換導致正在互動的SIP信令處理失敗;2、FS可用性檢測指令碼和主備切換後話務接管指令碼。

一、前提

1、freeswicth和keepalived均能通過服務啟動;                       ——通過apt安裝freeswitch和keepalived即可通過服務啟動

2、兩個節點freeswitch連線同一個pgsql或者mysql資料庫;      ——當然也可以是其他外部資料庫

二、環境

os:debian 8

A節點IP:172.16.100.10

B節點IP:172.16.100.11

VIP:172.16.100.12

freeswitch域名:sofia.superpipi.cn  ——域名也可以直接使用VIP

三、配置

3.1 允許應用繫結非本機IP

在兩個節點均執行以下命令:

echo 'net.ipv4.ip_nonlocal_bind=1' >> /etc/sysctl.conf
sysctl -p

3.2 配置freeswitch

修改“/usr/local/freeswitch/conf/vars.xml ” ——具體路徑視實際情況

將“local_ip_v4”的值修改為VIP:“172.16.100.12”

將“domain”的值修改為:“sofia.superpipi.cn”

3.3 配置keepalived

keepalived預設配置檔案路徑“/etc/keepalived/keepalived.conf ”

A節點keepalived配置:

! Configuration File for keepalived
global_defs {
   notification_email {
     acassen
   }
   notification_email_from [email protected]
   smtp_server 172.16.100.251
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_fs {
    script "/etc/keepalived/script/check_fs.sh"
    interval 1
    weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    garp_master_delay 10
    smtp_alert
    virtual_router_id 51
    priority 100
    nopreempt  #主節點不搶佔VIP
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_fs
    }
    virtual_ipaddress {
        172.16.100.12/24
        172.16.100.12/24 label eth0:1
    }
    notify_master "/etc/keepalived/script/fs_recover.sh"
}

B節點keepalived配置:

! Configuration File for keepalived
global_defs {
   notification_email {
     acassen
   }
   notification_email_from [email protected]
   smtp_server 172.16.100.251
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script check_fs {
    script "/etc/keepalived/script/check_fs.sh"
    interval 1
    weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    garp_master_delay 10
    smtp_alert
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
    track_script {
        check_fs
    }
    virtual_ipaddress {
        172.16.100.12/24
        172.16.100.12/24 label eth0:1
    }
    notify_master "/etc/keepalived/script/fs_recover.sh"
}

3.4 檢測指令碼

檢測指令碼路徑:

mkdir -p /etc/keepalived/script/

檢測可用性指令碼

vim /etc/keepalived/script/check_fs.sh
#!/bin/sh

FS_CLI_PROG='/usr/local/freeswitch/bin/fs_cli'
FS_CLI_HOST='127.0.0.1'
FS_CLI_PORT='8021'
FS_CLI_PASS='ClueCon'
PROFILES='sofia.superpipi.cn'
VIP='172.16.100.12'

fs_cli() {
  $FS_CLI_PROG -H $FS_CLI_HOST -P $FS_CLI_PORT -p $FS_CLI_PASS -x "$1"
}

sofia_profile_started() {
  fs_cli "sofia xmlstatus" | grep "<name>$1</name>" | wc -l
}

save_log(){
count=1
str_tmp="`date +%Y-%m-%d_%H:%M:%S` "
while [ $# -ge 1 ];do
str_tmp="$str_tmp $1"
count=count+1
shift
done
echo $str_tmp >>/var/log/check_fs_`date +%Y-%m-%d`.log
}

check_vrrp(){
ip a|grep $VIP|wc -l
}

check_fs_service(){
ps -ef |grep freeswitch.service|grep -v 'grep'|wc -l
}

#     fs_cli "sofia recover"
for p in $PROFILES; do
   if [ `sofia_profile_started "$p"` -eq 0 ]; then
     # echo "$p DOWN"
     log_str="$p DOWN"
     save_log $log_str
     if [ `check_vrrp` -eq 1 ];then
         save_log "本機已經繫結VRRP,即將重啟keepalived和FreeSWITCH。"
         service keepalived restart
         save_log "vrrp切換完成!"
         if [ `check_fs_service` -eq 1 ];then
             save_log "freeswitch服務正在操作中。"
         else
            service freeswitch restart
            save_log "freeswitch重啟成功!"
         fi
     else
         if [ `check_fs_service` -eq 1 ];then
             save_log "freeswitch服務正在操作中。 "
         else
             save_log "本機沒有繫結VRRP,重啟FreeSWITCH。"
             service freeswitch restart
             save_log "freeswitch重啟成功!"
         fi
     fi
     exit 1
  fi
done
save_log "freeswitch狀態檢測:OK!"
#echo "OK"
exit 0

切換為主節點後恢復通話指令碼

vim /etc/keepalived/script/fs_recover.sh
#!/bin/sh
 
FS_CLI_PROG='/usr/local/freeswitch/bin/fs_cli'
FS_CLI_HOST='127.0.0.1'
FS_CLI_PORT='8021'
FS_CLI_PASS='ClueCon'
PROFILES='sofia.superpipi.cn'
VIP='172.16.100.12'
 
fs_cli() {
  $FS_CLI_PROG -H $FS_CLI_HOST -P $FS_CLI_PORT -p $FS_CLI_PASS -x "$1"
}
 
save_log(){
count=1
str_tmp="`date +%Y-%m-%d_%H:%M:%S` "
while [ $# -ge 1 ];do
str_tmp="$str_tmp $1"
count=count+1
shift
done
echo $str_tmp >>/var/log/check_fs_`date +%Y-%m-%d`.log
}
save_log "本節點切換為主用狀態,開始接管切換前的通話。"
fs_cli "sofia recover"
fs_cli "raloadxml"
exit 0