Chinaunix首页 | 论坛 | 博客
  • 博客访问: 460931
  • 博文数量: 237
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-12 14:40
个人简介

  每个人生的当口,都是会有一个孤独的时刻,四顾无人,只有自己,于是不得不看明白自己的脆弱,自己的欲望,自己的念想,自己的界限,还有,自己真正的梦想。

文章分类

全部博文(237)

文章存档

2017年(3)

2016年(234)

分类: Mysql/postgreSQL

2016-05-11 11:15:19

利用LVS+Keepalived实现MySQL高可用

MySQL架构

MySQL的高可用,由一对互为主从的MySQL服务器组成,平时只有主库提供服务,备库不提供服务。当主库停止服务时,服务自动切换到备库。主备库通过双向复制,保证数据一致性。主备库以VIP对外统一服务接口

Keepalived

MySQL对外服务接口的VIP,由Keepalived来实现心跳检查和动态漂移。因此MySQLKeepalived部署在同一台服务器上,每台Keepalived配置的realserver即自身机器的MySQL服务。

环境安装

MySQL安装

两台服务器都安装MySQL,搭建主主复制架构。具体搭建略。

LVS+Keepalived安装

LVS源码安装
  1. ln -s /usr/src/kernels/2.6.32-279.14.1.el6.x86_64/ /usr/src/linux
  2. tar zxvf ipvsadm-1.24.tar.gz
  3. cd ipvsadm-1.24
  4. make && make install

Keepalived源码安装

安装依赖包

yum -y install popt-devel

安装Keepalived

点击(此处)折叠或打开

  1. tar zxvf keepalived-1.1.19.tar.gz
  2. cd keepalived-1.1.19
  3. ./configure --prefix=/usr/local/keepalived
  4. make && make install
  5. cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  6. cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  7. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
  8. mkdir /etc/keepalived

添加自启动

mysqldkeepalived自启动

点击(此处)折叠或打开

  1. chkconfig --add mysqld
  2. chkconfig --add keepalived
  3. chkconfig mysqld on
  4. chkconfig keepalived on

Keepalived配置

IP配置

这里的主备库IP配置如下

IP

服务VIP

10.0.0.77

10.0.0.76

10.0.0.78


Keepalived
配置文件keepalived配置文件/etc/keepalived/keepalived.conf,主库为例

点击(此处)折叠或打开

  1. global_defs {
  2.    router_id LVS_1
  3. }
  4. vrrp_sync_group test {
  5.    group {
  6.      mysql_ha
  7.    }
  8. }

  9. vrrp_instance mysql_ha {
  10.    state BACKUP ## 主备库都是BACKUP,防止主库重启后,抢夺VIP资源
  11.    interface eth0 ## HA地址网卡
  12.    virtual_router_id 61
  13.    priority 150 ## 优先级,主库比备库高
  14.    nopreempt ## 非抢夺,在主库上配置,表示不抢夺VIP
  15.    advert_int 1 ## 主备心跳检查间隔
  16.    authentication {
  17.      auth_type PASS
  18.      auth_pass 1111
  19.    }
  20.    virtual_ipaddress { ## VIP,主备库一样
  21.       10.0.0.76
  22.    }
  23.    ## 以下脚本是Keepalived改变状态时触发的动作,分别是变为backup、master、fault状态
  24.    ## 这是个简单的报警脚本而已
  25.    notify_backup "/usr/local/keepalived_script/keepalived_notify.sh BACKUP"
  26.    notify_master "/usr/local/keepalived_script/keepalived_notify.sh MASTER"
  27.    notify_fault "/usr/local/keepalived_script/keepalived_notify.sh FAULT"
  28.    notify "/usr/local/keepalived_script/keepalived_notify.sh "
  29. }
  30. virtual_server 10.0.0.76 3306 {
  31.    delay_loop 2
  32.    lb_algo rr
  33.    lb_kind DR
  34.    persistence_timeout 20
  35.    protocol TCP
  36.    real_server 10.0.0.77 3306 { ## keepalived的realserver,主备库都取自身的mysql服务
  37.      weight 3
  38.      notify_down "/usr/local/keepalived_script/keepalived_notify.sh MYSQL_DOWN" ## 检测到realserver服务不可用时的动作
  39.      TCP_CHECK {
  40.        connect_timeout 3
  41.        nb_get_retry 3
  42.        delay_before_retry 3
  43.        connect_port 3306
  44.      }
  45.    }
  46. }

测试

测试效果

从以下4个方面测试HA效果
    keepalived down

    mysqld down

    网络故障

    机器宕机


测试步骤和结果

keepalived down

步骤:kill master上的keepalived进程

结果:

>>slavekeepalived通知切换为master

mysqld down

步骤:kill master上的mysqld进程,或者提高master服务器的负载

结果:

>>masterkeepalived通知因为mysqld down而停止keepalived

>>slavekeepalived通知切换为master

网络故障

步骤:masterifconfig eth0 down,或者service network restart

结果:

>>masterkeepalived通知变为fault状态

>>slavekeepalived通知切换为master

 

机器宕机

步骤:master机器重启

结果:

>>slavekeepalived通知切换为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]: VRRP_Instance(mysql_ha) ignoring received advertisment...

6,端口转发

virtual_server配置的端口,与real_server配置的端口必须一样?


阅读(1452) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~