重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
10.1.1.174:23316 M1
10.1.1.237:23316 M2
[root@master1 ~]# cd /usr/local/src/
[root@master1 src]# wget https://www.keepalived.org/software/keepalived-2.0.17.tar.gz
[root@master1 src]# tar -xf keepalived-2.0.17.tar.gz
[root@master1 src]# cd keepalived-2.0.17
[root@master1 keepalived-2.0.17]# yum install openssl* libnl‐dev* gcc-c++
[root@master1 keepalived-2.0.17]# ./configure --prefix=/usr/local/keepalived
[root@master1 keepalived-2.0.17]# make
[root@master1 keepalived-2.0.17]# make install
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
echo "/etc/init.d/keepalived start" >> /etc/rc.local
mkdir -p /etc/keepalived/
mv /etc/keepalived/keepalived.conf /root/keepalived.conf.bak
M1:keepalive配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script chk_mysql_port { #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等
script "/etc/keepalived/chk_mysql.sh" #这里通过脚本监测
interval 2 #脚本执行间隔,每2s检测一次
weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise 1 #检测1次成功就算成功。但不修改优先级
}
vrrp_instance VI_1 {
state MASTER
interface ens192 #指定虚拟ip的网卡接口,改成你自己的 eth0或者其它什么的
# mcast_src_ip 10.1.1.174
virtual_router_id 51 #路由器标识,MASTER和BACKUP必须是一致的
priority 101 #定义优先级,数字越大,优先级越高,在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。这样MASTER故障恢复后,就可以将VIP资源再次抢回来
unicast_src_ip 10.1.1.174 ##(本地IP地址)
unicast_peer {
10.1.1.237 ##(对端IP地址)此地址一定不能忘记
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.111
}
track_script {
chk_mysql_port
}
}
[root@crm-db01 ~]# cat /etc/keepalived/chk_mysql.sh #####注意可执行权限
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "23316"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
M2:keepalived配置文件:
[root@#localhost keepalived]# cat keepalived.conf
! Configuration File for keepalived
vrrp_script chk_mysql_port {
script "/etc/keepalived/chk_mysql.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens192 #指定虚拟ip的网卡接口,改成你自己的 eth0或者其它什么的
#mcast_src_ip 10.1.1.237
virtual_router_id 51
priority 90
unicast_src_ip 10.1.1.237 ##(本地IP地址)
unicast_peer {
10.1.1.174 ##(对端IP地址)此地址一定不能忘记
}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.1.1.111
}
track_script {
chk_mysql_port
}
}
[root@#localhost keepalived]# cat chk_mysql.sh #####注意可执行权限
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "23316"|wc -l)
if [ "${counter}" -eq 0 ]; then
/etc/init.d/keepalived stop
fi
启动keepalived服务
[root@master1 ~]# /etc/init.d/keepalived start
常用命令:
/etc/init.d/keepalived restart|start|stop
/etc/init.d/mysqld restart|start|stop
查看keepalive状态:
systemctl status keepalived.service
查看日志:
tail -f /var/log/messages
1.启动两个keepalived ,两边都有VIP。
我这个配置文件已经修改过,应该不会出现这个情况。
2.VIP起来后,无法通过VIP访问MySQL数据库。
关闭防火墙或者开放相关端口。
查看你的MySQL配置文件,看看有没有这个参数#bind-address = 10.1.1.237,有的话注释掉,重启数据库。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。