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 Alexandre.Cassen@firewall.loc 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 Alexandre.Cassen@firewall.loc 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