当我们使用双机热备的时候,有时候只是监控的机器,而不是具体的服务,当我们希望监控具体的业务时候,比如我的机器上跑着kamailio和http api的服务,我们该如何让keepalived监控这两个服务呢,当着两个服务挂掉后,也希望VIP能漂移
需求背景大概是上面描述的,一般情况我们都是监控业务的监听端口,是不是还在监听中,例如kamailio是5060,我的https api 监听的端口是80
我这里只示例了keepalived的配置文件监控的脚本
测试kamailio的端口连通性,我们用到了一个工具sipsak
下载编译方式:
git clone https://github.com/nils-ohlmeier/sipsak.git
yum install automake autoconf git -y
cd sipsak/
autoreconf --install
./configure
make
make install
先安装好sipsak,具体用的时候指定具体的路径用就可以了
master:172.24.0.74
slave:172.24.0.75
VIP:172.24.0.109
非抢占模式
master的keepalived.conf如下:
! 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
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_server {
script "/usr/cloudland/keepalived/keepalived_check_server.sh 172.24.0.74"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 109
priority 100
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.24.0.74
unicast_peer {
172.24.0.75
}
virtual_ipaddress {
172.24.0.109/24 dev ens192
}
virtual_ipaddress_excluded {
#ffff:ffff:ffff:ffff/64 dev ens192
}
track_script {
check_server
}
}
virtual_server 172.24.0.109 3306 {
delay_loop 6
lb_algo wrr
lb_kind Dr
persistence_timeout 60
protocol TCP
real_server 172.24.0.74 3306 {
weight 3
#notify_down /root/shutdown.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
注意上面的是我调用的监控具体服务的脚本
vrrp_script check_server {
script "/usr/cloudland/keepalived/keepalived_check_server.sh 172.24.0.74"
interval 3
}
master上的keepalived_check_server.sh
#!/bin/bash
host=$1
sipsak -s sip:$host:5060
sip_exit_status=$?
core_http_status=`curl -I -s http://$host:80 |grep HTTP|awk '{print $2}'`
if [[ $exit_status -eq 0 ]] && [[ $core_http_status -eq 200 ]]; then
exit 0
else
exit 1
fi
slave的keepalived.conf
! 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
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_server {
script "/usr/cloudland/keepalived/keepalived_check_server.sh 172.24.0.75"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 109
priority 90
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
unicast_src_ip 172.24.0.75
unicast_peer {
172.24.0.74
}
virtual_ipaddress {
172.24.0.109/24 dev ens192
}
virtual_ipaddress_excluded {
#ffff:ffff:ffff:ffff/64 dev ens192
}
track_script {
check_server
}
}
virtual_server 172.24.0.109 3306 {
delay_loop 6
lb_algo wrr
lb_kind Dr
persistence_timeout 60
protocol TCP
real_server 172.24.0.75 3306 {
weight 3
#notify_down /root/shutdown.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
slave上的keepalived_check_server.sh
#!/bin/bash
host=$1
sipsak -s sip:$host:5060
sip_exit_status=$?
core_http_status=`curl -I -s http://$host:80 |grep HTTP|awk '{print $2}'`
if [[ $exit_status -eq 0 ]] && [[ $core_http_status -eq 200 ]]; then
exit 0
else
exit 1
fi
上面的配置就可以实现keepalived对kamailio和http api的两个端口5060和80监控了
当这两个服务挂掉后,VIP会漂移
另外具体调试keepalived的时候也经常用到的抓包看看
tcpdump -i eth0 vrrp -n



