@ruoli
2017-11-22T14:46:51.000000Z
字数 2472
阅读 2347
其他开源框架
Linux主机:10.122.1.35
Linux主机:10.122.1.36
虚拟IP:10.122.1.30
两台Linux主机都需要安装Keepalived
下载至目录:/home/zz/user/keepalived
tar -zxvf keepalived-1.3.8.tar.gz
./configure
此步骤如果出现错误:libnfnetlink 库找不到,则需要执行:
cp -R usr/ /
然后在当前目录
make
make install
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 \
--in-interface enp0s31f6 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 \
--out-interface enp0s31f6 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
注意:【enp0s31f6】为网卡名称。
cp /home/zz/user/keepalived/keepalived-1.3.8/keepalived/etc/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
配置文件路径为:
/etc/keepalived/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 SERVER_1
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface enp0s25
virtual_router_id 51
priority 100
protocol TCP
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.122.1.30/24
}
}
virtual_server 10.122.1.30 3306 {
delay_loop 2
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 10.122.1.35 3306 {
weight 1
MISC_CHECK {
connect_timeout 60
nb_get_retry 3
delay_before_retry 3
connect_port 3306
misc_path "/home/zz/user/status_check/check_mysql.sh 10.122.1.35"
misc_timeout 60
misc_dynamic
}
}
real_server 10.122.1.36 3306 {
weight 1
MISC_CHECK {
connect_timeout 60
nb_get_retry 3
delay_before_retry 3
connect_port 3306
misc_path "/home/zz/user/status_check/check_mysql.sh 10.122.1.36"
misc_timeout 60
misc_dynamic
}
}
}
从机配置与主机配置大致一致,主要不同的配置项如下
router_id SERVER_2
state BACKUP
priority 50
check_mysql.sh
#!/bin/bash
MYSQL_PING=`/usr/local/mysql/bin/mysqladmin -u root -h $1 ping`
MYSQL_OK="mysqld is alive"
if [[ ${MYSQL_PING} == ${MYSQL_OK} ]]; then
echo "yes"
exit 0
else
echo "no"
exit 1
fi
service keepalived start
查看日志:
tail -f /var/log/messages
在主从服务器上都执行虚拟IP查看,如果虚拟IP都在自己身上,则发生了脑裂
ip addr|grep 10.122.1.30
发生脑裂主要原因为两台服务器之间无法进行心跳通讯,可检查防火墙或者进行串口通讯。