环境条件
192.168.10.141 | sv10-141 | master1 |
192.168.10.144 | sv11-144 | master2 |
192.168.10.145 | sv12-145 | slave1 |
192.168.10.146 | sv13-146 | slave2 |
192.168.10.143 | sv14-143 | haproxy |
根据博主的文章mysql数据库主从安装 ,先把master1,master2.slave1,slave2的主从复制做好,其中slave1,slave2从master1,master1和master2互为主从。
安装步骤
1) 首先安装keepalived ,使用yum install keepalived -y
2)将配置修改成本文提供的文本。注意文本内容里面的提示信息。
二台机器都需要配置,采用下文,其中提示修改的必须自己修改。这里的#号拷贝的是要注意,换成!, #在这个文件中是非法的。
cat <<EOF > /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
# 删除这里的vrrp_strict ,否则虚拟IP不能提供服务。
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
# state 服务器之一为MASTER,另外一个为BACKUP,或者均为BACKUP
state MASTER
#这里需要通过ifconfig看自己的网卡名称,绑定自己的网卡
interface eth0
#这里需要改成和另外一台keepalived的数值一样
virtual_router_id 52
#优先级要二台机器的配置文件不一样,keepalived会考虑优先级高的先分配虚拟IP
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
#虚拟IP。用于漂移对外提供统一服务。
192.168.10.189
}
}
EOF
3)分别启动master1,master2的机器,可以看到机器master1上有漂移的VIP
[root@sv10-141 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:65:68:30 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.141/24 brd 192.168.10.255 scope global noprefixroute dynamic eth0
valid_lft 52936sec preferred_lft 52936sec
inet 192.168.10.189/32 scope global eth0
在master1上 执行systemctl stop keepalived,到master2机器上查看,会发现网卡上绑定了VIP,漂移成功。
[root@sv11-144 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:65:68:31 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.144/24 brd 192.168.10.255 scope global noprefixroute dynamic eth0
valid_lft 53010sec preferred_lft 53010sec
inet 192.168.10.189/32 scope global eth0
测试mysql数据,从vip 192.168.10.189上测试
[root@sv11-144 ~]# mysql -uslave -p -h192.168.10.189
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
高可用测试成功。
增加集群功能
将如下代码添加到keepalived的尾部,关联虚拟服务器和真实服务器的功能
cat <<EOF >> /etc/keepalived/keepalived.conf
virtual_server 192.168.10.189 3306 {
delay_loop 6
protocol TCP
lb_algo rr
lb_kind DR
real_server 192.168.10.141 3306 {
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
connect_port 3306
delay_before_retry 3
}
}
real_server 192.168.10.144 3306 {
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
connect_port 3306
delay_before_retry 3
}
}
}
EOF
由于我们使用了DR模式,因此需要阻止eth0上的arp回馈消息,编辑如下代码作为启用或者停止arp相应的脚本
mkdir -p ~/bin
cat <<EOF >~/bin/arpctl.sh
#!/bin/bash
VIP=192.168.10.189
. /etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
EOF
chmod 755 ~/bin/arpctl.sh
在二台机器上执行arpctl start
重启keepalived
~/bin/arpctl.sh start
systemctl restart keepalived
使用mysql测试,使用select @@hostname 获取当前的数据库的主机信息以便区分是否再切换不同的mysql服务器
[root@sv11-144 bin]# mysql -uslave -p -h192.168.10.189
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2464
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select host from information_schema.processlist;
+----------------------+
| host |
+----------------------+
| 192.168.10.189:55314 |
| sv10-141:45664 |
+----------------------+
2 rows in set (0.01 sec)
MariaDB [(none)]> exit
Bye
成功