Freeswitch uses keepalived for active/standby switching high-availability deployment

Reprint

https://blog.51cto.com/908405/2176392

There are two high-availability deployment methods for reeSWITCH: active/standby switching and load balancing. The official document describes the deployment of active/standby switching using Corosync & Pacemaker, and load balancing uses pre-opensips. However, there is no introduction to the high-availability method of using keepalived for active and standby switching, and there is no introduction to this deployment method on the Internet.

I am not familiar with Corosync & Pacemaker. Most of my current company web applications use keepalived+haproxy, so I am a little familiar with keepalived, so I tried to use keepalived for freeswitch for high availability deployment of active/standby switching.

The high availability deployment of using keepalived for freeswitch to switch between active and standby is relatively simple. The deployment scheme introduced in this article has two highlights (I think): 1. The master node does not preempt the VIP, otherwise it may cause the VIP switch after the master node is restored and cause the interaction. SIP signaling processing failed; 2. FS availability detection script and traffic takeover script after master/backup switch.

 

1. Prerequisite

1. Both freeswicth and keepalived can be started through the service;-install freeswitch and keepalived through apt to start through the service

2. Two nodes freeswitch connect to the same pgsql or mysql database; ——Of course it can also be other external databases

 

2. Environment

os : debian 8

IP of node A: 172.16.100.10

Node B IP: 172.16.100.11

VIP:172.16.100.12

freeswitch domain name: sofia.superpipi.cn-the domain name can also use VIP directly

 

Three, configuration

3.1 Allow applications to bind non-native IP

Execute the following commands on both nodes:

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

 

3.2 Configure freeswitch

Modify "/usr/local/freeswitch/conf/vars.xml"-the specific path depends on the actual situation

Modify the value of "local_ip_v4" to VIP: "172.16.100.12"

Modify the value of "domain" to: "sofia.superpipi.cn"

 

3.3 Configure keepalived

Keepalived default configuration file path "/etc/keepalived/keepalived.conf"

 

A node keepalived configuration:

! 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 node keepalived configuration:

! 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 Detection script

Detection script path:

mkdir -p /etc/keepalived/script/

Check availability 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

Recover call script after switching to master node

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

Guess you like

Origin blog.csdn.net/gredn/article/details/88696896