在sip業(yè)務(wù)中鳄虱,kamailio算是比較重要的一個(gè)環(huán)節(jié)弟塞;kamailio高可用性建設(shè)也有很多中方案拙已,本文在centos7采用的是虛擬ip漂移keepalived加sipsak的方式進(jìn)行。這樣就可以保證一臺(tái)機(jī)器死掉以后另外一臺(tái)機(jī)器隨著虛擬IP的漂移能繼續(xù)處理業(yè)務(wù)倍踪;另一方面,如果機(jī)器兩臺(tái)機(jī)器都沒(méi)有宕機(jī)建车,而且只是kamailio進(jìn)行掛掉了,也可以通過(guò)sipsak的健康檢查潮罪,來(lái)使虛擬IP漂移,避免出現(xiàn)一臺(tái)只有kamailio掛掉凄杯,機(jī)器還在keepalived還在,然后虛擬ip 不漂移的情況屯碴;
使用的開(kāi)源庫(kù):keepalived + sipsak
Keepalived 地址:https://github.com/acassen/keepalived.git
sipsak地址:https://github.com/nils-ohlmeier/sipsak.git
keepalived 安裝編譯安裝腳本:
yum install automake autoconf
yum install gcc openssl-devel libnl3-devel pcre-devel -y
git clone -b v2.1.5 https://github.com/acassen/keepalived.git
cd keepalived/
./autogen.sh
./configure --prefix=/usr/local/keepalived
make && make install
mkdir /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /opt/keepalived/keepalived/keepalived.service /etc/systemd/system/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /opt/keepalived/keepalived/etc/init.d/keepalived /etc/init.d/
systemctl enable keepalived.service
Keepalived 一些相關(guān)操作:
systemctl daemon-reload #重新加載
systemctl enable keepalived.service #設(shè)置開(kāi)機(jī)自動(dòng)啟動(dòng)
systemctl disable keepalived.service #取消開(kāi)機(jī)自動(dòng)啟動(dòng)
systemctl start keepalived.service #啟動(dòng)
systemctl stop keepalived.service#停止
vim /lib/systemd/system/keepalived.service#打開(kāi)keepalived.service文件,該文件主要配置keepalived service的內(nèi)容如下:
[Unit]
Description=Keepalived
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
ExecStart=/usr/local/keepalived/sbin/keepalived -D
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
ip addr #可以用此看到虛擬ip
sipsak編譯安裝:
cd sipsak
autoreconf --install
./configure
make
make install
主用服務(wù)配置keepalived.conf膊存,該文件默認(rèn)在/etc/keepalived/目錄下:
! 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
script_user root
enable_script_security
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
}
vrrp_script check_sip {
script "/etc/keepalived/node01.sh" #sipsak 腳本
interval 10
}
vrrp_instance VI_1 {
state MASTER#主用標(biāo)識(shí)
interface eth0
virtual_router_id 51
priority 100#權(quán)重
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.74.200#虛擬ip
}
track_script {
check_sip#調(diào)用腳本函數(shù)
}
}
node01.sh 腳本內(nèi)容:
#!/bin/bash
node01=10.10.74.186#主用KA服務(wù)器的地址
node02=10.10.74.105#備用KA服務(wù)器的地址
return_code=0 # success
# check local instance
timeout 5 sipsak -s sip:$node01:5060
exit_status=$?
if [[ $exit_status -eq 0 ]]; then
echo "sip ping successful to node01 [$node01]"
exit $return_code
fi
# local instance failed, check remote
timeout 2 sipsak -s sip:$node02:5060
exit_status=$?
if [[ $exit_status -eq 0 ]]; then
echo "sip ping successful to node02 [$node02]"
return_code=1
fi
echo "return code [$return_code]"
exit $return_code
備用服務(wù)配置keepalived.conf隔崎,該文件默認(rèn)在/etc/keepalived/目錄下:
! 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
script_user root
enable_script_security
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
}
vrrp_script check_sip {
script "/etc/keepalived/node02.sh" #sipsak 腳本
interval 10
}
vrrp_instance VI_1 {
state BACKUP#備用標(biāo)識(shí)
interface eth0
virtual_router_id 51
priority 50#權(quán)重
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.74.200#虛擬ip
}
track_script {
check_sip#調(diào)用腳本函數(shù)
}
}
node02.sh 腳本內(nèi)容:
#!/bin/bash
node01=10.10.74.186#主用KA服務(wù)器的地址
node02=10.10.74.105#備用KA服務(wù)器的地址
return_code=1 # fail
# check local instance
timeout 5 sipsak -s sip:$node01:5060
exit_status=$?
if [[ $exit_status -eq 0 ]]; then
echo "sip ping successful to node01 [$node01]"
exit $return_code
fi
# local instance failed, check remote
timeout 2 sipsak -s sip:$node02:5060
exit_status=$?
if [[ $exit_status -eq 0 ]]; then
echo "sip ping successful to node02 [$node02]"
return_code=1
fi
echo "return code [$return_code]"
exit $return_code