标签:虚拟服务器 检测 val global rac etc int ack ping
https://www.cnblogs.com/huningfei/p/12758980.html 在这篇文章中只用了一个harpoxy,如果它挂掉之后,后端的数据库也不能访问了,这还是存在单点故障的,所以接下来我准备使用keepalived(双主)+haproxy 去实现
debian系统
keepalived_master1 +haproxy 192.168.7.32
keepalived_master1 +haproxy 192.168.9.52
mysql_master 192.168.6.123
mysql_slave1 192.168.4.21
mysql_slave2 192.168.9.53
vip1:192.168.8.102
vip2:192.168.8.103
master1既是一个主,又是另一个主的从,扮演了两个角色
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAproxy237
}
vrrp_script chk_haproxy { #HAproxy 服务监控脚本
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.8.102
}
notify_master "/etc/keepalived/clean_arp.sh 192.168.8.102"
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.8.103
}
notify_master "/etc/keepalived/clean_arp.sh 192.168.8.103"
}
master2
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id HAproxy237
}
vrrp_script chk_haproxy { #HAproxy 服务监控脚本
script "/etc/keepalived/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP # 这个要跟另一台主机相反
interface eth0
virtual_router_id 51
priority 90 #
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_haproxy
}
virtual_ipaddress {
192.168.8.102
}
notify_master "/etc/keepalived/clean_arp.sh 192.168.8.102"
}
vrrp_instance VI_2 {
state MASTER # 这个也要跟另一台主机的相反
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.8.103
}
notify_master "/etc/keepalived/clean_arp.sh 192.168.8.103"
}
检测haproxy的服务是否正常 (两个keepalved上面都需要有这个脚本)
#!/bin/bash
A=`ps -C haproxy --no-header | wc -l`
if [ $A -eq 0 ];then
sudo /etc/init.d/haproxy start
sleep 3
if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ];then
sudo /etc/init.d/keepalived stop
fi
fi
#!/bin/sh
VIP=$1
GATEWAY=192.168.11.254 #这个是本机的网卡的网关地址
/sbin/arping -I eth0 -c 5 -s $VIP $GATEWAY &>/dev/null
master1
master2
1 更改配置文件
2 设置启动脚本
3 启动
在slave1和slave2上分别给两个haproxy机器授权:如果还是报错,再尝试给vip授权
grant all privileges on *.* to ‘yx1‘@‘192.168.7.%‘ identified by ‘123456‘;
grant all privileges on *.* to ‘yx1‘@‘192.168.9.%‘ identified by ‘123456‘;
> flush privileges;
>
分别用vip 102和103访问
#用的8.102
yx@es-2:~$ mysql -h192.168.8.102 -P3306 -uyx1 -p123456 -e "use test;show tables;"
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
+----------------+
yx@es-2:~$ mysql -h192.168.8.102 -P3306 -uyx1 -p123456 -e "use test;show tables;"
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
| tb2 |
+----------------+
#用 8.103去测试
yx@es-2:~$ mysql -h192.168.8.103 -P3306 -uyx1 -p123456 -e "use test;show tables;"
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
+----------------+
yx@es-2:~$ mysql -h192.168.8.103 -P3306 -uyx1 -p123456 -e "use test;show tables;"
+----------------+
| Tables_in_test |
+----------------+
| tb1 |
| tb2 |
+----------------+
keepalived(双主模式)+haproxy+mysql_slave
标签:虚拟服务器 检测 val global rac etc int ack ping
原文地址:https://www.cnblogs.com/huningfei/p/12759560.html