MySQL · 配置 Keepalived 实现双机热备

简介

Keepalived 是集群管理中保证集群高可用的一个服务软件,其功能类似于 heartbeat,用来防止单点故障。

Keepalived 是以 VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议)协议为实现基础的,这个协议可以认为是实现了路由器高可用的协议,将多台提供相同功能的路由器组成一个路由器组。

  • 这里面有一个 MASTER 和多个 BACKUP;
  • MASTER 上面有一个对外提供服务的 Virtual IP (VIP);
  • MASTER 会发组播,当 BACKUP 收不到 VRRP 包时就认为 MASTER 宕机
  • 这时需要根据 VRRP 优先级来选举一个 BACKUP 为 MASTER,这样就保证路由器的正常使用了。

步骤

安装 Keepalived

Keepalived 可以使用 yum 直接安装,在 master 服务器和 backup 服务器执行:

1
$ yum install keepalived

配置 Master 服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
[root@hop02 keepalived]# pwd
/etc/keepalived

[root@hop02 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
wangsen@haier.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}

vrrp_script check_run {
script "/etc/keepalived/check_mysql.sh"
interval 5
}

vrrp_instance VI_1 {
state MASTER
interface bond0
virtual_router_id 51
priority 100
advert_int 1
mcast_src_ip 10.135.22.70

authentication {
auth_type PASS
auth_pass 7777
}

track_script {
check_run
}

virtual_ipaddress {
10.135.22.69
}
}

[root@hop02 keepalived]# cat check_mysql.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD='aea&Lch6bs27zSyT'
CHECK_TIME=3

#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1

function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done

配置 BACKUP 服务器

注意以下几点变动:

  • state 角色为 BACKUP
  • interface 为网卡的 ID,要根据机器确认
  • virtual_route_id 要与 MASTER 一致,默认为 51
  • priority 要比 MASTER 小
  • 设置 vrrp_strict 选项

配置并启动服务

配置 IP 转发,需要修改配置文件 /etc/sysctl.conf,默认只有 root 可以修改

1
2
3
4
5
$ su - root
Password:
# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
# sysctl -p
# exit

防火墙添加规则,因为 VRRP 使用 224.0.0.18 这个组播地址

1
2
3
4
5
6
$ sudo firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
success
$ sudo firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
success
$ sudo firewall-cmd --reload
success

可以查看一下这两条规则

1
2
3
4
$ sudo firewall-cmd --direct --get-rules ipv4 filter INPUT
0 --in-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
$ sudo firewall-cmd --direct --get-rules ipv4 filter OUTPUT
0 --out-interface em1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT

启动 MASTER 和 BACKUP 的 keepalived 服务,并设置开机启动

1
2
3
4
5
6
7
[root@hop02 etc]# cat /etc/redhat-release
CentOS release 6.5 (Final)

[root@hop02 etc]# service keepalived start
Starting keepalived: [ OK ]

$ systemctl enable keepalived
1
2
3
4
5
[root@hop02 etc]# ps -ef | grep keepalived
root 17660 1 0 09:42 ? 00:00:00 /usr/sbin/keepalived -D
root 17661 17660 0 09:42 ? 00:00:00 /usr/sbin/keepalived -D
root 17662 17660 0 09:42 ? 00:00:00 /usr/sbin/keepalived -D
root 17703 15307 0 09:42 pts/2 00:00:00 grep keepalived

查看 MASTER 网卡,可以发现 MASTER 服务器的 bond0 网卡上多了 10.135.22.69 这个虚拟 IP 地址。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@hop02 keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: em1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
link/ether c8:1f:66:f2:09:35 brd ff:ff:ff:ff:ff:ff
3: em2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP qlen 1000
link/ether c8:1f:66:f2:09:35 brd ff:ff:ff:ff:ff:ff
4: em3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether c8:1f:66:f2:09:37 brd ff:ff:ff:ff:ff:ff
inet 172.25.178.22/24 brd 172.25.178.255 scope global em3
inet6 fe80::ca1f:66ff:fef2:937/64 scope link
valid_lft forever preferred_lft forever
5: em4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether c8:1f:66:f2:09:38 brd ff:ff:ff:ff:ff:ff
inet 172.23.178.183/24 brd 172.23.178.255 scope global em4
inet6 fe80::ca1f:66ff:fef2:938/64 scope link
valid_lft forever preferred_lft forever
6: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether c8:1f:66:f2:09:35 brd ff:ff:ff:ff:ff:ff
inet 10.135.22.70/23 brd 10.135.23.255 scope global bond0
inet 10.135.22.69/32 scope global bond0
inet6 fe80::ca1f:66ff:fef2:935/64 scope link
valid_lft forever preferred_lft forever
7: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
link/ether 52:54:00:20:68:13 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
8: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 500
link/ether 52:54:00:20:68:13 brd ff:ff:ff:ff:ff:ff

漂移规则如下:

  • 默认使用 MASTER 服务器 10.135.22.70,虚拟 IP 为 10.135.22.69,此时 MASTER 服务器会有 2 个 IP。
  • 当 MASTER 出问题时,IP 会漂移到 BACKUP 服务器(10.135.22.71),此时 BACKUP 服务器会有 2 个 IP。
  • 当 MASTER 重新启动后,虚拟 IP 又会漂移回 MASTER 服务器。

附录

配置文件说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
! Configuration File for keepalived

global_defs {
notification_email {
# email 接收方
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
# email 发送方
notification_email_from Alexandre.Cassen@firewall.loc
# 邮件服务器, smtp 协议
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id app2
vrrp_skip_check_adv_addr
# 使用 unicast_src_ip 需要注释 vrrp_strict,而且也可以进行 ping 测试
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

# vrrp实例
vrrp_instance VI_1 {
# 指定 keepalived 的角色,MASTER 表示此主机是主服务器,BACKUP 表示此主机是备用服务器
state MASTER

# 指定网卡
interface em1

# 虚拟路由标识,这个标识是一个数字,同一个vrrp实例使用唯一的标识。
# 即同一vrrp_instance下,MASTER和BACKUP必须是一致的
virtual_router_id 51

# 定义优先级,数字越大,优先级越高(0-255)。
# 在同一个vrrp_instance下,MASTER 的优先级必须大于 BACKUP 的优先级
priority 100

# 设定 MASTER 与 BACKUP 负载均衡器之间同步检查的时间间隔,单位是秒
advert_int 1

# 如果两节点的上联交换机禁用了组播,则采用 vrrp 单播通告的方式
unicast_src_ip 10.0.0.11
unicast_peer {
10.0.0.12
}

# 设置验证类型和密码
authentication {
#设置验证类型,主要有PASS和AH两种
auth_type PASS
#设置验证密码,在同一个vrrp_instance下,MASTER与BACKUP必须使用相同的密码才能正常通信
auth_pass 1111
}

#设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个
virtual_ipaddress {
# 虚拟 IP
10.0.0.10/24 brd 10.0.0.255
}
}

# 虚拟服务器端口配置
virtual_server 10.0.0.10 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP

real_server 10.0.0.11 80 {
weight 1
}
}

参考