源码安装
wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
tar -zxvf keepalived-1.3.5.tar.gz
安装openssl openssl-devel
yum -y install openssl openssl-devel
./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/
make && make install
yum安装
keepalived yum安装
预先安装好epel-release源
yum list installed|grep epel-release
查找可用安装的keepalived源
yum search keepalived
命令进行安装
yum install keepalived -y
启动keepalived服务
systemctl start keepalived
使用yum安装的会有一个默认配置文件模板
路径为/etc/keepalived/keepalived.conf
yum install ipvsadm -y
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
#简单的头部,这里主要可以做邮件通知报警等的设置,此处就暂不配置了;
global_defs {
#notificationd LVS_DEVEL
}
#预先定义一个脚本,方便后面调用,也可以定义多个,方便选择;
vrrp_script chk_haproxy {
script "/etc/keepalived/check_mysql.sh" #具体脚本路径
interval 2 #脚本循环运行间隔
}
#VRRP虚拟路由冗余协议配置
vrrp_instance VI_1 { #VI_1 是自定义的名称;
state BACKUP #MASTER表示是一台主设备,BACKUP表示为备用设备【我们这里因为设置为开启不抢占,所以都设置为备用】
nopreempt #开启不抢占
interface eth0 #指定VIP需要绑定的物理网卡
virtual_router_id 11 #VRID虚拟路由标识,也叫做分组名称,该组内的设备需要相同
priority 129 #定义这台设备的优先级 1-254;开启了不抢占,所以此处优先级必须高于另一台
advert_int 1 #生存检测时的组播信息发送间隔,组内一致
authentication { #设置验证信息,组内一致
auth_type PASS #有PASS 和 AH 两种,常用 PASS
auth_pass asd #密码
}
virtual_ipaddress {
192.168.0.200 #指定VIP地址,组内一致,可以设置多个IP
}
track_script { #使用在这个域中使用预先定义的脚本,上面定义的
chk_haproxy
}
#notify_backup "/etc/init.d/haproxy restart" #表示当切换到backup状态时,要执行的脚本
#notify_fault "/etc/init.d/haproxy stop" #故障时执行的脚本
}
检测mysql是否存活脚本check_mysql.sh
mysql -h 192.168.0.11 -u root -p123456 -P 3312 -e "show status;" >/dev/null 2>&1
if [ $? == 0 ]
then
echo " $host mysql login successfully "
exit 0
else
echo " mysql login faild"
killall keepalived
exit 2
fi
如果启动失败,给配置文件加权限
chmod +x /etc/keepalived/check_mysql.sh
chmod 644 keepalived.conf
查看keepalived日志
tail -f /var/log/messages
查看IP漂移
ip a
结论
主节点(权限高)挂掉后,ip会漂移到从,主节点重新启动后,ip又会漂移回主节点