首页 > 代码库 > mysql-----HA (keepalived篇)
mysql-----HA (keepalived篇)
5.21 DB-01
5.22 DB-02
5.24 虚拟ip(不能是实际的ip) ++选取vip
keepalived安装及配置
安装keepalived
在5.21上:
wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
tar -xvzf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
./configure --prefix=/usr/local/keepadlived --sysconf=/etc
make
make install
配置keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 anbaoyu@leadtone.com
6 }
7 notification_email_from anbaoyu@leadtone.com
8 smtp_server 127.0.0.1
9 smtp_connect_timeout 30
10 router_id Mysql-ha
11 }
12
13 vrrp_instance VI_1 {
14 state BACKUP ++++#两台配置此处均是BACKUP
15 interface eth0 +++++# 本机网卡
16 virtual_router_id 51
17 priority 100 +++++#优先级,另一台为90
18 advert_int 1
19 nopreempt +++++#不抢占,只在优先级高的配置,低的不配置
20 authentication {
21 auth_type PASS
22 auth_pass 1111
23 }
24 virtual_ipaddress {
25 192.168.5.24 ++++++#虚拟ip地址
26 }
27 }
28
29 virtual_server 192.168.5.24 3306 { ++++++#虚拟ip下的服务端口
30 delay_loop 3 +++++#每隔3秒检查一次real_server状态
31 lb_algo wrr +++++#LVS算法
32 lb_kind DR +++++#LVS模式
33 nat_mask 255.255.255.0
34 persistence_timeout 50 ++++#会话保持时间
35 protocol TCP
36
37 real_server 192.168.5.21 3306 { ++++#检查需要单节点的主机服务端口
38 weight 3
39 notify_down /usr/local/mysql/bin/mysql.sh
40 TCP_CHECK {
41 connect_timeout 3 ++++链接超时的时间
42 nb_get_retry 3 重连次数
43 delay_before_retry 3 重连间隔时间
44 connect_port 3306 ##健康检查端口
45 }
46 }
47 }
编写检测服务down后所要执行的脚本
vim /usr/local/mysql/bin/mysql.sh
#!/bin/sh
pkill keepalived
# chmod a+x /usr/local/mysql/bin/mysql.sh
# /usr/local/keepalived/sbin/keepalived -D //启动keepalived服务
# ps -ef |grep keepalived //查看keepalived进程
注意:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本,删除了自己的进程(pkill keepalived),从而实现了mysql故障自动转移
每台keepalived提供vip + real_server的ip 当real_server发生故障时候,keepalived杀死自身进程,另一台keepalived的得到别人喊vip,接过来,实现HA
测试:
找一台局域网pc,ping mysql的vip(5.24),这个时候是可以ping通的
停止mysql服务,看keepalived健康检查程序是否会触发我们写的脚本,查看keepalived的进程是否存在
第二台5.22安装keepalived及配置
# tar -xvzf keepalived-1.2.13.tar.gz
# cd keepalived-1.2.13
# ./configure --prefix=/usr/local/keepalived --sysconf=/etc/ && make && make install
配置keepalived
这台配置和上面的基本一样,有三个地方不同:优先级为90,没有抢占设置(#nopreempt),real_server为本机的ip
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
anbaoyu@leadtone.com
}
notification_email_from anbaoyu@leadtone.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Mysql-ha
}
vrrp_instance VI_1 {
state BACKUP
interface eth1 ##因为5.22的网卡为eth1 ,所以要调整
virtual_router_id 51
priority 90 ##优先级为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.5.24 ##设置虚拟vip
}
}
virtual_server 192.168.5.24 3306 {
delay_loop 3
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.5.22 3306 { ##real_server为本机的ip地址
weight 3
notify_down /usr/local/mysql/bin/mysql.sh
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 3306
}
}
}
编写检测服务down后要用的脚本
# vim /usr/local/mysql/bin/mysql.sh
#!/bin/sh
pkill keepalived
#chmod a+x /usr/local/mysql/bin/mysql.sh
启动keepalived
# /usr/local/keepalived/sbin/keepalived -D
# ps -elf |grep keepalived
测试:
停止mysql服务,看keepalived健康检查程序是否会触发我们编写的脚本
三 测试
mysql远程登录测试
在两台(5.21和5.22)上分别授权允许远程登录
mysql>grant all privileges on *.* to ‘root‘@‘%‘ identified by ‘123456‘;
mysql>flush privileges;
使用客户端远程进行登录
mysql -uroot -p123456 -h192.168.5.24 -P 3306
mysql>
mysql>
可以登录
把两台主机的服务 : mysql都启动 keepalived也启动
ip a 查看 vip在5.22上
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: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:2c:54:7b brd ff:ff:ff:ff:ff:ff
inet 192.168.5.22/24 brd 192.168.5.255 scope global eth1
((((inet 192.168.5.24/32 scope global eth1))))) ##虚ip
inet6 fe80::5054:ff:fe2c:547b/64 scope link
valid_lft forever preferred_lft forever
mysql>show master status; ##也可以查看vip在哪台机器上
停止5.22上mysqld,keepalived也会关掉 ,我们在5.23上ping 192.168.5.24 看是否能ping通,
正常是可以通的,用mysql -uroot -p123456 -h 192.168.5.24 -P 3306 ,正常是可以登录的,登录到5.21上了。这时候vip在5.21上,我们重启5.22上的mysqld和keepalived,ps -ef|grep keepalived 停止5.21的mysqld,这时keepalived也停止了,vip到了5.22,用mysql远程登录,查看show master status ;
注意:state BACKUP 两台状态都是backup
优先级高的 设置nopreempt(不抢占)
当优先级高的恢复运行状态时,不从优先级低的服务器上抢vip
mysql-----HA (keepalived篇)