Keepalived for Linuxhttps://www.keepalived.org/download.html本文使用 v.2.1.5 keepalived
可以使用sftp工具,具体目录自行定义(本文上传/usr/src/keepalived(自己创建的目录))
# tar -zxf keepalived-2.1.5.tar.gz
# cd keepalived-2.1.5
# ./configure --prefix=/usr/local/keepalived
# make && make install
# echo $?
0
#
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp ./keepalived/etc/init.d/keepalived /etc/init.d/ #还停留在keepalived-2.1.5目录下
# mkdir /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
# chkconfig keepalived on ##添加keepalived到自启动服务中
/etc/keepalived/keepalived.conf 文件 master/slave 都要配置 只不过 mcast_src_ip/real_server/router_id/priority 需要修改
! Configuration File for keepalived
global_defs {
router_id dhamecha@DB236 ## master/slave 自定义
}
vrrp_script check_run_mysql
{
script "/etc/keepalived/mysql.sh" ##检查mysql状态脚本
interval 2 ##每两秒检测一下
weight -20 ## 数据库master出现问题降级10
}
vrrp_instance VI_1 {
state BACKUP ##可以是 MASTER 或 BACKUP,不过当其他节点keepalived 启动时
interface eth0 ##本机物理网卡,节点固有 IP(非 VIP)的网卡,用来发 VRRP包。
virtual_router_id 236 ##虚拟路由,主从机必需一致。取值在 0-255 之间,值不能重复,否则会出错。
mcast_src_ip 198.244.179.236 ##本机ip
priority 90 #用来选举 master该项取值范围是 1-255在此范围之外会被识别成默认值 100)。
advert_int 1 #发 VRRP 包的时间间隔进行一次master选举(默认1秒)。
nopreempt ##优先级高的设置 解决服务回复之后再次抢占的问题
authentication { #认证区域,保持master和backup一致
auth_type PASS
auth_pass 1111
}
track_script {
check_run_mysql ##执行mysql监控服务脚本
}
virtual_ipaddress {
198.244.179.234 ##主要由优先级来决定的,和 state 设置的值没有多大关系,可以设置多个 IP 地址。
}
}
virtual_server 198.244.179.234 3306 { #LVS配置,VIP
delay_loop 5 #服务轮询的时间间隔
lb_algo wrr #LVS 调度算法
lb_kind DR #LVS 集群算法
persistence_timeout 50 #同一IP的链接50秒内被分配到同一台realserver
protocol TCP #用TCP协议检查realserver状态
real_server 198.244.179.236 3306 { #实际服务器的IP和端口
weight 100
notify_down /etc/keepalived/keepalived.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
# vi /etc/keepalived/keepalived.sh
内容:
service keepalived stop
# vi /etc/keepalived/mysql.sh
内容:
mysql -uroot -puw7!23!Qw2 -e "select version();" >/dev/null 2>&1
if [ $? -ne 0 ] ;then
#echo $(date)". mysql error!" >>/etc/keepalived/action.log
sudo service keepalived stop
if [ $? -ne 0 ] ;then
echo $(date +'%Y-%m-%d %H:%M:%S')". mysql error.but ACTION keepalived-stop failed." >>/etc/keepalived/action.log
else
echo $(date +'%Y-%m-%d %H:%M:%S')". mysql error.ACTION keepalived-stop success." >>/etc/keepalived/action.log
fi
else
keepalivedcheck=`ps -C keepalived --no-header | wc -l`
if [ $keepalivedcheck -eq 0 ] ;then
sudo service keepalived start
if [ $? -ne 0 ] ;then
echo $(date +'%Y-%m-%d %H:%M:%S')". mysql normal,keepalived already stopped.but ACTION keepalived-start failed" >>/etc/keepalived/action.log
else
echo $(date +'%Y-%m-%d %H:%M:%S')". mysql normal,keepalived already stopped.ACTION keepalived-start success" >>/etc/keepalived/action.log
fi
###test code
#else
#echo $(date)". keepalived is running!" >>/etc/keepalived/action.log
##test code
fi
#echo $(date)". mysql running!" >>/etc/keepalived/action.log
fi
最后授权执行脚本:chmod +x /etc/keepalived/XXXX.sh
# service keepalived start
# ps -ef | grep keep
root 2063 1 0 16:35 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 2064 2063 0 16:35 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 2065 2063 0 16:35 ? 00:00:00 /usr/local/keepalived/sbin/keepalived -D
root 14999 17440 0 17:53 pts/0 00:00:00 grep --color=auto keep
root@ubuntu:/# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether fe:fc:fe:d1:fb:17 brd ff:ff:ff:ff:ff:ff
inet 172.16.127.49/24 brd 172.16.127.255 scope global ens18
valid_lft forever preferred_lft forever
inet 198.244.179.234 /32 scope global ens18
valid_lft forever preferred_lft forever
inet6 fe80::fcfc:feff:fed1:fb17/64 scope link
valid_lft forever preferred_lft forever
inet 198.244.179.234 /32 出现在ip里面
success了;
可以参考这两个链接:
https://www.cnblogs.com/itliyh/p/13821855.html 操作步骤比较详细
https://www.cnblogs.com/zhangyinhua/p/7567629.html 原理写的很清楚