1
2
3
4
5
6
7
8
|
vim /etc/sysctl.conf
#.............前面部分省去
#######以下是文件末尾添加的部分######
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
##这4行的主要作用是本地arp不作回应##
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@mysql-ha1 ~]
# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@mysql-ha1 src]
# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[root@mysql-ha1 src]
# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[root@mysql-ha1 src]
# yum -y install kernel-devel make gcc openssl-devel libnl* popt*
[root@mysql-ha1 src]
# ln -s /usr/src/kernels/2.6.32-220.13.1.el6.x86_64/ /usr/src/linux
[root@mysql-ha1 src]
# tar -zxvf ipvsadm-1.26.tar.gz
[root@mysql-ha1 src]
# cd ipvsadm-1.26/
[root@mysql-ha1 ipvsadm-1.26]
# make
[root@mysql-ha1 ipvsadm-1.26]
# make install
[root@mysql-ha1 ipvsadm-1.26]
# cd ..
[root@mysql-ha1 src]
# tar -zxvf keepalived-1.2.2.tar.gz
[root@mysql-ha1 src]
# cd keepalived-1.2.2/
[root@mysql-ha1 keepalived-1.2.2]
# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/
[root@mysql-ha1 keepalived-1.2.2]
# make && make install
[root@mysql-ha1 keepalived-1.2.2]
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@mysql-ha1 keepalived-1.2.2]
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@mysql-ha1 keepalived-1.2.2]
# mkdir /etc/keepalived
[root@mysql-ha1 keepalived-1.2.2]
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@mysql-ha1 keepalived-1.2.2]
# cp /usr/local/sbin/keepalived /usr/sbin/
|
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
|
[root@mysql-ha1 ~]
# >/etc/keepalived/keepalived.conf
[root@mysql-ha1 ~]
# vim /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-ha1
#修改为自己的主机名
}
##################第一部分###################
vrrp_instance VI_1 {
state BACKUP
#都修改成BACKUP
interface eth0
virtual_router_id 60
#默认51 主从都修改为60
priority 100
#在mysql-ha2上LVS上修改成80
advert_int 1
nopreempt
#不抢占资源,意思就是它活了之后也不会再把主抢回来
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.55
}
}
##################第二部分###################
virtual_server 192.168.5.55 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.5.234 3306 {
weight 1
notify_down /usr/local/mysql/bin/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
connect_port 3306
}
}
}
|
1
2
3
4
|
[root@mysql-ha1 ~]
# vim /usr/local/mysql/bin/mysql.sh
#!/bin/bash
pkill keepalived
[root@mysql-ha1 ~]
# chmod +x /usr/local/mysql/bin/mysql.sh
|
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
|
[root@mysql-ha1 ~]
# ps aux | grep keepalived
root 2518 0.0 0.0 41796 936 ? Ss 19:35 0:00 keepalived -D
root 2519 0.0 0.1 43900 2152 ? S 19:35 0:00 keepalived -D
root 2520 0.0 0.0 43900 1572 ? S 19:35 0:00 keepalived -D
root 2610 0.0 0.0 103240 860 pts/0
S+ 23:07 0:00
grep
keepalived
[root@mysql-ha1 ~]
# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2237/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd
tcp 0 0 :::22 :::* LISTEN 1041/sshd
tcp 0 0 ::1:25 :::* LISTEN 1416/master
[root@mysql-ha1 ~]
# service mysqld stop
Shutting down MySQL... SUCCESS!
[root@mysql-ha1 ~]
# ps aux | grep keepalived
root 2640 0.0 0.0 103240 856 pts/0
S+ 23:07 0:00
grep
keepalived
[root@mysql-ha1 ~]
# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd
tcp 0 0 :::22 :::* LISTEN 1041/sshd
tcp 0 0 ::1:25 :::* LISTEN 1416/master
[root@mysql-ha1 ~]
#
|
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
|
[root@mysql-ha2 ~]
# vim /etc/keepalived/keepalived.conf
#! Configuration File for keepalived
global_defs {
router_id mysql-ha1
#修改为自己的主机名
}
##################第一部分###################
vrrp_instance VI_1 {
state BACKUP
#都修改成BACKUP
interface eth1
virtual_router_id 60
#默认51 主从都修改为60
priority 80
#在mysql-ha1上LVS上修改成100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.55
}
}
##################第二部分###################
virtual_server 192.168.5.55 3306 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.5.155 3306 {
weight 1
notify_down /usr/local/mysql/bin/mysql.sh
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
connect_port 3306
}
}
}
|
1
2
3
4
5
6
7
8
9
10
|
[root@mysql-ha1 ~]
# service mysqld start
Starting MySQL.. SUCCESS!
[root@mysql-ha1 ~]
# /etc/init.d/keepalived start
正在启动 keepalived:
[root@mysql-ha1 ~]
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.5.55:mysql wrr persistent 50
-> 192.168.5.234:mysql Local 1 0 0
|
1
2
3
4
5
6
7
8
9
10
|
[root@mysql-ha2 ~]
# service mysqld start
Starting MySQL.. SUCCESS!
[root@mysql-ha2 ~]
# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@mysql-ha2 ~]
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.5.55:3306 wrr persistent 50
-> 192.168.5.155:3306 Local 1 0 0
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@mysql-ha1 ~]
# ps aux | grep keepalived
root 2977 0.0 0.0 41796 936 ? Ss 23:14 0:00 keepalived -D
root 2978 0.0 0.1 43900 2156 ? S 23:14 0:00 keepalived -D
root 2979 0.0 0.0 43900 1576 ? S 23:14 0:00 keepalived -D
root 2982 0.0 0.0 103240 860 pts/0
S+ 23:16 0:00
grep
keepalived
[root@mysql-ha1 ~]
# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2917/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd
tcp 0 0 :::22 :::* LISTEN 1041/sshd
tcp 0 0 ::1:25 :::* LISTEN 1416/master
|
1
2
3
4
5
6
7
8
9
10
|
[root@mysql-ha2 ~]
# ps aux | grep keepalived
root 10576 0.0 0.0 5980 756 pts/4
S+ 15:24 0:00
grep
keepalived
[root@mysql-ha2 ~]
# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program
name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4776/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1101/master
tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1114/qpidd
tcp 0 0 :::22 :::* LISTEN 4776/sshd
tcp 0 0 ::1:25 :::* LISTEN 1101/master
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
C:\Users\Administrator>mysql -uduyunlong -p123456 -h192.168.5.55
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 33
Server version: 5.5.22-log Source distribution
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the buffer.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
|
test
|
+--------------------+
4 rows
in
set
(0.02 sec)
mysql>
|
1
2
|
[root@mysql-ha1 ~]
# service mysqld stop
Shutting down MySQL... SUCCESS!
|
1
2
3
4
5
|
[root@mysql-ha2 ~]
# service mysqld start
Starting MySQL.. SUCCESS!
[root@mysql-ha2 ~]
# /etc/init.d/keepalived start
正在启动 keepalived: [确定]
[root@mysql-ha2 ~]
#
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection
id
: 4
Current database: *** NONE ***
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
|
test
|
+--------------------+
4 rows
in
set
(0.01 sec)
mysql>
|
利用LVS+Keepalived实现MySQL高可用
MySQL架构
Keepalived
环境安装
MySQL安装
两台服务器都安装MySQL,搭建主主复制架构。具体搭建略。
LVS+Keepalived安装
- ln -s /usr/src/kernels/2.6.32-279.14.1.el6.x86_64/ /usr/src/linux
- tar zxvf ipvsadm-1.24.tar.gz
- cd ipvsadm-1.24
- make && make install
Keepalived源码安装
安装依赖包
yum -y install popt-devel
点击(此处)折叠或打开
- tar zxvf keepalived-1.1.19.tar.gz
- cd keepalived-1.1.19
- ./configure --prefix=/usr/local/keepalived
- make && make install
- cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
- cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
- cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
- mkdir /etc/keepalived
添加自启动
mysqld和keepalived自启动
点击(此处)折叠或打开
- chkconfig --add mysqld
- chkconfig --add keepalived
- chkconfig mysqld on
- chkconfig keepalived on
Keepalived配置
IP配置
库 |
IP |
服务VIP |
主 |
10.0.0.77 |
10.0.0.76 |
备 |
10.0.0.78 |
点击(此处)折叠或打开
- global_defs {
- router_id LVS_1
- }
- vrrp_sync_group test {
- group {
- mysql_ha
- }
- }
- vrrp_instance mysql_ha {
- state BACKUP ## 主备库都是BACKUP,防止主库重启后,抢夺VIP资源
- interface eth0 ## HA地址网卡
- virtual_router_id 61
- priority 150 ## 优先级,主库比备库高
- nopreempt ## 非抢夺,在主库上配置,表示不抢夺VIP
- advert_int 1 ## 主备心跳检查间隔
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress { ## VIP,主备库一样
- 10.0.0.76
- }
- ## 以下脚本是Keepalived改变状态时触发的动作,分别是变为backup、master、fault状态
- ## 这是个简单的报警脚本而已
- notify_backup "/usr/local/keepalived_script/keepalived_notify.sh BACKUP"
- notify_master "/usr/local/keepalived_script/keepalived_notify.sh MASTER"
- notify_fault "/usr/local/keepalived_script/keepalived_notify.sh FAULT"
- notify "/usr/local/keepalived_script/keepalived_notify.sh "
- }
- virtual_server 10.0.0.76 3306 {
- delay_loop 2
- lb_algo rr
- lb_kind DR
- persistence_timeout 20
- protocol TCP
- real_server 10.0.0.77 3306 { ## keepalived的realserver,主备库都取自身的mysql服务
- weight 3
- notify_down "/usr/local/keepalived_script/keepalived_notify.sh MYSQL_DOWN" ## 检测到realserver服务不可用时的动作
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- }
测试
测试效果
从以下4个方面测试HA效果
keepalived down
mysqld down
网络故障
机器宕机
测试步骤和结果
keepalived down
步骤:kill master上的keepalived进程
结果:
>>slave的keepalived通知切换为master
mysqld down
步骤:kill master上的mysqld进程,或者提高master服务器的负载
结果:
>>master的keepalived通知因为mysqld down而停止keepalived
>>slave的keepalived通知切换为master
网络故障
步骤:master上ifconfig eth0 down,或者service network restart
结果:
>>master的keepalived通知变为fault状态
>>slave的keepalived通知切换为master
机器宕机
步骤:master机器重启
结果:
>>slave的keepalived通知切换为master
问题分析
1,keepalived切换为master时,arping失败
有的环境测试,keepalived切换为master时,没有成功发送arping,导致VIP不同。所以给出了arping脚本
2,重启网卡,master变为fault状态,但没继续变为backup状态。
检查日志发现有错误信息
Keepalived_vrrp: Netlink: filter function error
Keepalived_healthcheckers: Netlink: filter function error
查找网络资料得知,keepalive不支持网络设备热插拔。ip link可以看出设备id变化了。
此时需要reload keepalived或者直接restart。
办法:网卡发生重启后,需检查keepalived是否正常
3,keepalived异常终止,VIP没有解除绑定,导致及时重启keepalived后,这个VIP也不能正常绑定和解除绑定
猜测原因是异常终止keepalived后,VIP没解除绑定,重启以后keepalived不识别未解除绑定的VIP。
办法:机器发生重启后需检查keepalived是否正常
4,ipvsadm使用yum安装,keepalived使用源码安装,keepalived不能启动healthchecker进程
keepalived安装时指定kernel目录:./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-308.1.1.el5-x86_64
5,关于virtual_router_id
virtual_router_id用来标识一对keepalived主从机的身份,不同keepalived对的这个id不能一样。取值范围1~255。如果不同组keepalived配置了同一个id,则会有如下报错:
Apr 7 14:31:11 localhost Keepalived_vrrp[22980]: ip address associated with VRID not present in received packet : 10.0.0.94 Apr 7 14:31:11 localhostKeepalived_vrrp[22980]: one or more VIP associated with VRID mismatch actual MASTER advert Apr 7 14:31:11 localhost Keepalived_vrrp[22980]: bogus VRRP packet received on eth0.181 !!! Apr 7 14:31:11 localhost Keepalived_vrrp[22980]: -------------
2013-09-12 15:11:59
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://liuping0906.blog.51cto.com/2516248/1295092
- VRRP_Instance(mysql_ha) ignoring received advertisment... |
6,端口转发