keepalived如何手动切换主备
概述
主备部署中使用keepalived可以很方便的实现,安装维护简单,功能稳定。
最近在使用过程中有小的发现,记录一下。
环境
CentOS Linux release 7.9.2009 (Core)
keepalived.x86_64 1.3.5-19.el7
安装配置
centos7自带的keepalived版本为1.3.5,直接使用yum安装
sudo yum install keepalived
修改keepalived.conf配置文件。
sudo vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
router_id LVS_DEVEL
}
vrrp_script chk_fs_proxy {
script "/etc/keepalived/check_fs_proxy.sh"
interval 2
weight -5
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface ens160
mcast_src_ip 10.55.55.137
virtual_router_id 140
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.55.55.140
}
track_script {
chk_fs_proxy
}
}
新增check_fs_proxy.sh
vi /etc/keepalived/check_fs_proxy.sh
#!/bin/bash
counter=$(ps -C freeswitch --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
exit 1
else
exit 0
fi
修改文件权限
sudo chmod 777 check_fs_proxy.sh
sudo chmod 644 keepalived.conf
系统配置
防火墙配置。
sudo firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --protocol vrrp -j ACCEPT
sudo firewall-cmd --reload
开机启动
sudo chkconfig keepalived on
sudo systemctl enable keepalived
手动切换
以前的切换测试都是针对监控的程序,比如kill掉fs进程,观察ip自动切换到备机。
这样的切换过程会慢一点,有没有影响更小的切换方法呢。
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 10.55.55.137/24
inet 10.55.55.140/32
sudo systemctl restart keepalived
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
inet 10.55.55.137/24
重启keepalived服务,可以更快速的实现主备切换。
在需要主动切换主备的情况下,这种方法更简单快速。
总结
keepalived功能稳定,配置简单,使用方便。
空空如常
求真得真